blob: 26da9e49223e9384b6d2dc3760f58216998cd4b7 [file] [log] [blame]
David Tolnay0c033e32020-11-01 15:15:48 -08001use crate::gen::block::Block;
David Tolnayf7b81fb2020-11-01 22:39:04 -08002use crate::gen::nested::NamespaceEntries;
David Tolnay8810a542020-10-31 21:39:22 -07003use crate::gen::out::OutFile;
David Tolnay942cffd2020-11-03 18:27:10 -08004use crate::gen::{builtin, include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04005use crate::syntax::atom::Atom::{self, *};
David Tolnay891061b2020-04-19 22:42:33 -07006use crate::syntax::symbol::Symbol;
Adrian Taylorc8713432020-10-21 18:20:55 -07007use crate::syntax::{
David Tolnayb960ed22020-11-27 14:34:30 -08008 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, ResolvableName, Signature, Struct,
9 Trait, Type, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070010};
David Tolnay7db73692019-10-20 14:51:12 -040011use proc_macro2::Ident;
David Tolnay5439fa12020-11-03 18:45:01 -080012use std::collections::{HashMap, HashSet};
David Tolnay7db73692019-10-20 14:51:12 -040013
David Tolnayac7188c2020-11-01 16:58:16 -080014pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080015 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040016 let out = &mut out_file;
17
David Tolnaydfb82d72020-11-02 00:10:10 -080018 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070019 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040020
David Tolnay7b0e5102020-11-01 23:22:12 -080021 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080022 write_data_structures(out, apis);
23 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080024 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040025
David Tolnay3374d8d2020-10-31 22:18:45 -070026 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070027 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070028
David Tolnayac7188c2020-11-01 16:58:16 -080029 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070030}
31
David Tolnay7b0e5102020-11-01 23:22:12 -080032fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
33 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080034 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080035 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080036 _ => false,
37 };
Adrian Taylorc8713432020-10-21 18:20:55 -070038
David Tolnay7b0e5102020-11-01 23:22:12 -080039 let apis_by_namespace =
40 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
41
David Tolnayd920be52020-11-01 23:34:30 -080042 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080043
David Tolnayd920be52020-11-01 23:34:30 -080044 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080045 let apis = ns_entries.direct_content();
46
47 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080048 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080049 match api {
David Tolnay17a934c2020-11-02 00:40:04 -080050 Api::Struct(strct) => write_struct_decl(out, &strct.name.cxx),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080051 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080052 Api::CxxType(ety) => write_struct_using(out, &ety.name),
53 Api::RustType(ety) => write_struct_decl(out, &ety.name.cxx),
David Tolnay7b0e5102020-11-01 23:22:12 -080054 _ => unreachable!(),
55 }
David Tolnay7db73692019-10-20 14:51:12 -040056 }
David Tolnay7db73692019-10-20 14:51:12 -040057
David Tolnay7b0e5102020-11-01 23:22:12 -080058 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080059 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
60 write(out, nested_ns_entries, indent + 2);
61 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080062 }
Adrian Taylorf9213622020-10-31 22:25:42 -070063 }
64}
65
David Tolnaye1109d92020-11-01 20:51:56 -080066fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayf94bef12020-04-17 14:46:42 -070067 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070068 for api in apis {
David Tolnay102c7ea2020-11-08 18:58:09 -080069 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayf94bef12020-04-17 14:46:42 -070070 if let Some(receiver) = &efn.sig.receiver {
71 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070072 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070073 .or_insert_with(Vec::new)
74 .push(efn);
75 }
76 }
77 }
Joel Galenson968738f2020-04-15 14:19:33 -070078
David Tolnay5439fa12020-11-03 18:45:01 -080079 let mut structs_written = HashSet::new();
80 let mut toposorted_structs = out.types.toposorted_structs.iter();
David Tolnay9622b462020-11-02 21:34:42 -080081 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070082 match api {
David Tolnay5439fa12020-11-03 18:45:01 -080083 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
84 for next in &mut toposorted_structs {
85 if !out.types.cxx.contains(&strct.name.rust) {
86 out.next_section();
David Tolnay102c7ea2020-11-08 18:58:09 -080087 let methods = methods_for_type
88 .get(&strct.name.rust)
89 .map(Vec::as_slice)
90 .unwrap_or_default();
91 write_struct(out, next, methods);
David Tolnay5439fa12020-11-03 18:45:01 -080092 }
93 structs_written.insert(&next.name.rust);
94 if next.name.rust == strct.name.rust {
95 break;
96 }
97 }
98 }
Joel Galensonc03402a2020-04-23 17:31:09 -070099 Api::Enum(enm) => {
100 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800101 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700102 check_enum(out, enm);
103 } else {
104 write_enum(out, enm);
105 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700106 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700107 Api::RustType(ety) => {
David Tolnay17a934c2020-11-02 00:40:04 -0800108 if let Some(methods) = methods_for_type.get(&ety.name.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -0700109 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700110 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700111 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700112 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700113 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400114 }
115 }
116
David Tolnayfabca772020-10-03 21:25:41 -0700117 out.next_section();
118 for api in apis {
119 if let Api::TypeAlias(ety) = api {
David Tolnay17a934c2020-11-02 00:40:04 -0800120 if out.types.required_trivial.contains_key(&ety.name.rust) {
121 check_trivial_extern_type(out, &ety.name)
David Tolnayfabca772020-10-03 21:25:41 -0700122 }
123 }
124 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800125}
126
David Tolnaye1109d92020-11-01 20:51:56 -0800127fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700128 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400129 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800130 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800131 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay04770742020-11-01 13:50:50 -0800132 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
133 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
134 _ => {}
135 }
David Tolnay7db73692019-10-20 14:51:12 -0400136 }
David Tolnay7da38202020-11-27 17:36:16 -0800137
138 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400139 }
140
141 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800142 match api {
143 Api::Struct(strct) => write_struct_operators(out, strct),
144 Api::RustFunction(efn) => {
145 out.next_section();
146 write_rust_function_shim(out, efn);
147 }
148 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400149 }
150 }
David Tolnay7db73692019-10-20 14:51:12 -0400151}
152
David Tolnay7da38202020-11-27 17:36:16 -0800153fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
154 out.set_namespace(Default::default());
155 out.begin_block(Block::Namespace("std"));
156
157 for api in apis {
158 if let Api::Struct(strct) = api {
159 if derive::contains(&strct.derives, Trait::Hash) {
160 out.next_section();
161 let qualified = strct.name.to_fully_qualified();
162 writeln!(out, "template <> struct hash<{}> {{", qualified);
163 writeln!(
164 out,
165 " size_t operator()(const {} &self) const noexcept {{",
166 qualified,
167 );
168 let link_name = mangle::operator(&strct.name, "hash");
169 write!(out, " return ::");
170 for name in &strct.name.namespace {
171 write!(out, "{}::", name);
172 }
173 writeln!(out, "{}(self);", link_name);
174 writeln!(out, " }}");
175 writeln!(out, "}};");
176 }
177 }
178 }
179
180 out.end_block(Block::Namespace("std"));
181}
182
David Tolnaydfb82d72020-11-02 00:10:10 -0800183fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
184 for api in apis {
185 if let Api::Include(include) = api {
186 out.include.insert(include);
187 }
188 }
189
David Tolnaya7c2ea12020-10-30 21:32:53 -0700190 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400191 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700192 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700193 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
194 | Some(I64) => out.include.cstdint = true,
195 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800196 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700197 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800198 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800199 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700200 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800201 Type::RustBox(_) => out.builtin.rust_box = true,
202 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700203 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb3b24a12020-12-01 15:27:43 -0800204 Type::SharedPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800205 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700206 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800207 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800208 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800209 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800210 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400211 }
212 }
David Tolnayec66d112020-10-31 21:00:26 -0700213}
David Tolnayf51447e2020-03-06 14:14:27 -0800214
David Tolnay102c7ea2020-11-08 18:58:09 -0800215fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800216 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800217 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800218
David Tolnay17a934c2020-11-02 00:40:04 -0800219 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800220 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700221 writeln!(out, "#ifndef {}", guard);
222 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400223 for line in strct.doc.to_string().lines() {
224 writeln!(out, "//{}", line);
225 }
David Tolnay17a934c2020-11-02 00:40:04 -0800226 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800227
David Tolnay7db73692019-10-20 14:51:12 -0400228 for field in &strct.fields {
229 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700230 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400231 writeln!(out, "{};", field.ident);
232 }
David Tolnay84389352020-11-27 17:12:10 -0800233
234 if !methods.is_empty() || operator_eq || operator_ord {
David Tolnay102c7ea2020-11-08 18:58:09 -0800235 writeln!(out);
236 }
David Tolnay84389352020-11-27 17:12:10 -0800237
David Tolnay102c7ea2020-11-08 18:58:09 -0800238 for method in methods {
239 write!(out, " ");
240 let sig = &method.sig;
241 let local_name = method.name.cxx.to_string();
242 write_rust_function_shim_decl(out, &local_name, sig, false);
243 writeln!(out, ";");
244 }
David Tolnay84389352020-11-27 17:12:10 -0800245
David Tolnayb960ed22020-11-27 14:34:30 -0800246 if operator_eq {
247 writeln!(
248 out,
249 " bool operator==(const {} &) const noexcept;",
250 strct.name.cxx,
251 );
252 writeln!(
253 out,
254 " bool operator!=(const {} &) const noexcept;",
255 strct.name.cxx,
256 );
257 }
David Tolnay84389352020-11-27 17:12:10 -0800258
259 if operator_ord {
260 writeln!(
261 out,
262 " bool operator<(const {} &) const noexcept;",
263 strct.name.cxx,
264 );
265 writeln!(
266 out,
267 " bool operator<=(const {} &) const noexcept;",
268 strct.name.cxx,
269 );
270 writeln!(
271 out,
272 " bool operator>(const {} &) const noexcept;",
273 strct.name.cxx,
274 );
275 writeln!(
276 out,
277 " bool operator>=(const {} &) const noexcept;",
278 strct.name.cxx,
279 );
280 }
281
David Tolnay7db73692019-10-20 14:51:12 -0400282 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700283 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400284}
285
286fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
287 writeln!(out, "struct {};", ident);
288}
289
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800290fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800291 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800292 write_atom(out, enm.repr);
293 writeln!(out, ";");
294}
295
David Tolnay8faec772020-11-02 00:18:19 -0800296fn write_struct_using(out: &mut OutFile, ident: &Pair) {
297 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800298}
299
David Tolnay0b9b9f82020-11-01 20:41:00 -0800300fn write_struct_with_methods<'a>(
301 out: &mut OutFile<'a>,
302 ety: &'a ExternType,
303 methods: &[&ExternFn],
304) {
David Tolnay17a934c2020-11-02 00:40:04 -0800305 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800306 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700307 writeln!(out, "#ifndef {}", guard);
308 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700309 for line in ety.doc.to_string().lines() {
310 writeln!(out, "//{}", line);
311 }
David Tolnay365fc7c2020-11-25 16:08:13 -0800312 out.builtin.opaque = true;
David Tolnay7c06b862020-11-25 16:59:09 -0800313 writeln!(
314 out,
315 "struct {} final : public ::rust::Opaque {{",
316 ety.name.cxx,
317 );
Joel Galenson968738f2020-04-15 14:19:33 -0700318 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700319 write!(out, " ");
320 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800321 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700322 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700323 writeln!(out, ";");
324 }
325 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700326 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700327}
328
David Tolnay0b9b9f82020-11-01 20:41:00 -0800329fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800330 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800331 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700332 writeln!(out, "#ifndef {}", guard);
333 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700334 for line in enm.doc.to_string().lines() {
335 writeln!(out, "//{}", line);
336 }
David Tolnay17a934c2020-11-02 00:40:04 -0800337 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700338 write_atom(out, enm.repr);
339 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700340 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700341 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700342 }
343 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700344 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700345}
346
David Tolnay0b9b9f82020-11-01 20:41:00 -0800347fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800348 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800349 out.include.type_traits = true;
350 writeln!(
351 out,
352 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
353 enm.name.cxx,
354 );
David Tolnay17a934c2020-11-02 00:40:04 -0800355 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700356 write_atom(out, enm.repr);
357 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700358 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700359 write!(out, "static_assert(static_cast<");
360 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700361 writeln!(
362 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700363 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnay17a934c2020-11-02 00:40:04 -0800364 enm.name.cxx, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700365 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700366 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700367}
368
David Tolnay8faec772020-11-02 00:18:19 -0800369fn check_trivial_extern_type(out: &mut OutFile, id: &Pair) {
David Tolnay174bd952020-11-02 09:23:12 -0800370 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700371 // necessary for soundness. That's because triviality is always declared by
372 // the user in the form of an unsafe impl of cxx::ExternType:
373 //
374 // unsafe impl ExternType for MyType {
375 // type Id = cxx::type_id!("...");
376 // type Kind = cxx::kind::Trivial;
377 // }
378 //
379 // Since the user went on the record with their unsafe impl to unsafely
380 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800381 // them if that were wrong. However, in practice correctly reasoning about
382 // the relocatability of C++ types is challenging, particularly if the type
383 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700384 //
David Tolnay174bd952020-11-02 09:23:12 -0800385 // There may be legitimate reasons to opt out of this assertion for support
386 // of types that the programmer knows are soundly Rust-movable despite not
387 // being recognized as such by the C++ type system due to a move constructor
388 // or destructor. To opt out of the relocatability check, they need to do
389 // one of the following things in any header used by `include!` in their
390 // bridge.
391 //
392 // --- if they define the type:
393 // struct MyType {
394 // ...
395 // + using IsRelocatable = std::true_type;
396 // };
397 //
398 // --- otherwise:
399 // + template <>
400 // + struct rust::IsRelocatable<MyType> : std::true_type {};
401 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700402
David Tolnay174bd952020-11-02 09:23:12 -0800403 let id = id.to_fully_qualified();
404 out.builtin.relocatable = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700405 writeln!(out, "static_assert(");
David Tolnay174bd952020-11-02 09:23:12 -0800406 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700407 writeln!(
408 out,
David Tolnay174bd952020-11-02 09:23:12 -0800409 " \"type {} marked as Trivial in Rust is not trivially move constructible and trivially destructible in C++\");",
David Tolnay7426cc12020-10-03 19:04:04 -0700410 id,
411 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700412}
413
David Tolnayb960ed22020-11-27 14:34:30 -0800414fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
415 out.set_namespace(&strct.name.namespace);
416 out.begin_block(Block::ExternC);
417
418 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800419 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800420 writeln!(
421 out,
422 "bool {}(const {1} &, const {1} &) noexcept;",
423 link_name, strct.name.cxx,
424 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800425
426 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800427 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800428 writeln!(
429 out,
430 "bool {}(const {1} &, const {1} &) noexcept;",
431 link_name, strct.name.cxx,
432 );
433 }
David Tolnayb960ed22020-11-27 14:34:30 -0800434 }
435
David Tolnay84389352020-11-27 17:12:10 -0800436 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800437 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800438 writeln!(
439 out,
440 "bool {}(const {1} &, const {1} &) noexcept;",
441 link_name, strct.name.cxx,
442 );
443
David Tolnaya05f9402020-11-27 17:44:05 -0800444 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800445 writeln!(
446 out,
447 "bool {}(const {1} &, const {1} &) noexcept;",
448 link_name, strct.name.cxx,
449 );
450
451 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800452 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800453 writeln!(
454 out,
455 "bool {}(const {1} &, const {1} &) noexcept;",
456 link_name, strct.name.cxx,
457 );
458
David Tolnaya05f9402020-11-27 17:44:05 -0800459 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800460 writeln!(
461 out,
462 "bool {}(const {1} &, const {1} &) noexcept;",
463 link_name, strct.name.cxx,
464 );
465 }
466 }
467
David Tolnay7da38202020-11-27 17:36:16 -0800468 if derive::contains(&strct.derives, Trait::Hash) {
469 let link_name = mangle::operator(&strct.name, "hash");
470 writeln!(
471 out,
472 "size_t {}(const {} &) noexcept;",
473 link_name, strct.name.cxx,
474 );
475 }
476
David Tolnayb960ed22020-11-27 14:34:30 -0800477 out.end_block(Block::ExternC);
478}
479
480fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
481 if out.header {
482 return;
483 }
484
485 out.set_namespace(&strct.name.namespace);
486
487 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800488 out.next_section();
489 writeln!(
490 out,
491 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
492 strct.name.cxx,
493 );
David Tolnaya05f9402020-11-27 17:44:05 -0800494 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800495 writeln!(out, " return {}(*this, rhs);", link_name);
496 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800497
David Tolnayb960ed22020-11-27 14:34:30 -0800498 out.next_section();
499 writeln!(
500 out,
501 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
502 strct.name.cxx,
503 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800504 if derive::contains(&strct.derives, Trait::Eq) {
505 writeln!(out, " return !(*this == rhs);");
506 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800507 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800508 writeln!(out, " return {}(*this, rhs);", link_name);
509 }
David Tolnayb960ed22020-11-27 14:34:30 -0800510 writeln!(out, "}}");
511 }
David Tolnay84389352020-11-27 17:12:10 -0800512
513 if derive::contains(&strct.derives, Trait::PartialOrd) {
514 out.next_section();
515 writeln!(
516 out,
517 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
518 strct.name.cxx,
519 );
David Tolnaya05f9402020-11-27 17:44:05 -0800520 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800521 writeln!(out, " return {}(*this, rhs);", link_name);
522 writeln!(out, "}}");
523
524 out.next_section();
525 writeln!(
526 out,
527 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
528 strct.name.cxx,
529 );
David Tolnaya05f9402020-11-27 17:44:05 -0800530 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800531 writeln!(out, " return {}(*this, rhs);", link_name);
532 writeln!(out, "}}");
533
534 out.next_section();
535 writeln!(
536 out,
537 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
538 strct.name.cxx,
539 );
540 if derive::contains(&strct.derives, Trait::Ord) {
541 writeln!(out, " return !(*this <= rhs);");
542 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800543 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800544 writeln!(out, " return {}(*this, rhs);", link_name);
545 }
546 writeln!(out, "}}");
547
548 out.next_section();
549 writeln!(
550 out,
551 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
552 strct.name.cxx,
553 );
554 if derive::contains(&strct.derives, Trait::Ord) {
555 writeln!(out, " return !(*this < rhs);");
556 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800557 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800558 writeln!(out, " return {}(*this, rhs);", link_name);
559 }
560 writeln!(out, "}}");
561 }
David Tolnayb960ed22020-11-27 14:34:30 -0800562}
563
David Tolnay0b9b9f82020-11-01 20:41:00 -0800564fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800565 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800566 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800567 out.begin_block(Block::ExternC);
David Tolnaye1476af2020-11-01 13:47:25 -0800568 if let Some(annotation) = &out.opt.cxx_impl_annotations {
David Tolnaycc1ae762020-10-31 15:53:50 -0700569 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700570 }
David Tolnayebef4a22020-03-17 15:33:47 -0700571 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700572 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700573 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700574 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700575 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700576 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700577 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700578 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700579 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800580 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700581 write!(out, "const ");
582 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700583 write!(
584 out,
585 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800586 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700587 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700588 }
David Tolnay7db73692019-10-20 14:51:12 -0400589 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700590 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400591 write!(out, ", ");
592 }
David Tolnaya46a2372020-03-06 10:03:48 -0800593 if arg.ty == RustString {
594 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700595 } else if let Type::RustVec(_) = arg.ty {
596 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800597 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700598 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400599 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700600 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400601 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700602 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400603 write!(out, ", ");
604 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700605 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400606 write!(out, "*return$");
607 }
608 writeln!(out, ") noexcept {{");
609 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700610 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700611 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800612 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700613 Some(receiver) => write!(
614 out,
615 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700616 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800617 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700618 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700619 }
David Tolnay7db73692019-10-20 14:51:12 -0400620 for (i, arg) in efn.args.iter().enumerate() {
621 if i > 0 {
622 write!(out, ", ");
623 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700624 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400625 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700626 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700627 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800628 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700629 write!(out, " const");
630 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700631 }
632 write!(out, " = ");
633 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800634 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700635 Some(receiver) => write!(
636 out,
637 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700638 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800639 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700640 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700641 }
642 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400643 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700644 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700645 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700646 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700647 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700648 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700649 writeln!(out, " [&] {{");
650 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700651 }
David Tolnay7db73692019-10-20 14:51:12 -0400652 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700653 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400654 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700655 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400656 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700657 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400658 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700659 }
660 match &efn.ret {
661 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700662 Some(Type::Str(_)) if !indirect_return => {
663 out.builtin.rust_str_repr = true;
664 write!(out, "::rust::impl<::rust::Str>::repr(");
665 }
David Tolnay73b72642020-11-25 17:44:05 -0800666 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700667 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800668 write!(out, "::rust::impl<");
669 write_type(out, ty);
670 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700671 }
David Tolnay99642622020-03-25 13:07:35 -0700672 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400673 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700674 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800675 None => write!(out, "{}$(", efn.name.rust),
676 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700677 }
David Tolnay7db73692019-10-20 14:51:12 -0400678 for (i, arg) in efn.args.iter().enumerate() {
679 if i > 0 {
680 write!(out, ", ");
681 }
682 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700683 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400684 write!(out, "::from_raw({})", arg.ident);
685 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700686 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400687 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700688 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700689 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700690 write!(
691 out,
692 "::rust::impl<::rust::Str>::new_unchecked({})",
693 arg.ident,
694 );
David Tolnaya46a2372020-03-06 10:03:48 -0800695 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700696 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800697 write!(
698 out,
699 "::rust::String(::rust::unsafe_bitcopy, *{})",
700 arg.ident,
701 );
David Tolnay313b10e2020-04-25 16:30:51 -0700702 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700703 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700704 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700705 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800706 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800707 write_type(out, &arg.ty);
708 write!(out, "(static_cast<");
709 if slice.mutability.is_none() {
710 write!(out, "const ");
711 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800712 write_type_space(out, &slice.inner);
713 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700714 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700715 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800716 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400717 } else {
718 write!(out, "{}", arg.ident);
719 }
720 }
721 write!(out, ")");
722 match &efn.ret {
723 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
724 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800725 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400726 _ => {}
727 }
728 if indirect_return {
729 write!(out, ")");
730 }
731 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700732 if efn.throws {
733 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800734 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700735 writeln!(out, " throw$.ptr = nullptr;");
736 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700737 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700738 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700739 writeln!(
740 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800741 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700742 );
David Tolnay5d121442020-03-17 22:14:40 -0700743 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700744 writeln!(out, " return throw$;");
745 }
David Tolnay7db73692019-10-20 14:51:12 -0400746 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700747 for arg in &efn.args {
748 if let Type::Fn(f) = &arg.ty {
749 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700750 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700751 }
752 }
David Tolnayca563ee2020-11-01 20:12:27 -0800753 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700754}
755
756fn write_function_pointer_trampoline(
757 out: &mut OutFile,
758 efn: &ExternFn,
759 var: &Ident,
760 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700761) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700762 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700763 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700764 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700765
766 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700767 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
768 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400769}
770
David Tolnay0b9b9f82020-11-01 20:41:00 -0800771fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800772 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800773 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700774 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700775 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700776 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800777 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700778}
779
780fn write_rust_function_decl_impl(
781 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700782 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700783 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700784 indirect_call: bool,
785) {
David Tolnay04770742020-11-01 13:50:50 -0800786 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700787 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700788 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700789 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700790 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700791 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700792 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700793 write!(out, "{}(", link_name);
794 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700795 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800796 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700797 write!(out, "const ");
798 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700799 write!(
800 out,
801 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800802 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700803 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700804 needs_comma = true;
805 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700806 for arg in &sig.args {
807 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400808 write!(out, ", ");
809 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700810 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700811 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400812 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700813 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700814 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400815 write!(out, ", ");
816 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700817 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400818 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700819 needs_comma = true;
820 }
821 if indirect_call {
822 if needs_comma {
823 write!(out, ", ");
824 }
825 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400826 }
827 writeln!(out, ") noexcept;");
828}
829
David Tolnay0b9b9f82020-11-01 20:41:00 -0800830fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800831 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400832 for line in efn.doc.to_string().lines() {
833 writeln!(out, "//{}", line);
834 }
David Tolnaya73853b2020-04-20 01:19:56 -0700835 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800836 None => efn.name.cxx.to_string(),
837 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700838 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700839 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700840 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700841 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700842}
843
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700844fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700845 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700846 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700847 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700848 indirect_call: bool,
849) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700850 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700851 write!(out, "{}(", local_name);
852 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400853 if i > 0 {
854 write!(out, ", ");
855 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700856 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400857 write!(out, "{}", arg.ident);
858 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700859 if indirect_call {
860 if !sig.args.is_empty() {
861 write!(out, ", ");
862 }
863 write!(out, "void *extern$");
864 }
David Tolnay1e548172020-03-16 13:37:09 -0700865 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700866 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800867 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700868 write!(out, " const");
869 }
870 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700871 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700872 write!(out, " noexcept");
873 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700874}
875
876fn write_rust_function_shim_impl(
877 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700878 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700879 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700880 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700881 indirect_call: bool,
882) {
883 if out.header && sig.receiver.is_some() {
884 // We've already defined this inside the struct.
885 return;
886 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700887 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400888 if out.header {
889 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700890 return;
David Tolnay7db73692019-10-20 14:51:12 -0400891 }
David Tolnay439cde22020-04-20 00:46:25 -0700892 writeln!(out, " {{");
893 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700894 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700895 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700896 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700897 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700898 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700899 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
900 }
901 }
902 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700903 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700904 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700905 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700906 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700907 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700908 writeln!(out, "> return$;");
909 write!(out, " ");
910 } else if let Some(ret) = &sig.ret {
911 write!(out, "return ");
912 match ret {
913 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700914 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700915 write!(out, "::from_raw(");
916 }
917 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700918 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700919 write!(out, "(");
920 }
921 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700922 Type::Str(_) => {
923 out.builtin.rust_str_new_unchecked = true;
924 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
925 }
David Tolnay73b72642020-11-25 17:44:05 -0800926 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700927 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800928 write!(out, "::rust::impl<");
929 write_type(out, ret);
930 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700931 }
David Tolnay439cde22020-04-20 00:46:25 -0700932 _ => {}
933 }
934 }
935 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700936 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700937 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700938 }
939 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -0800940 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -0700941 if sig.receiver.is_some() {
942 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -0800943 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700944 }
David Tolnay9d840362020-11-10 08:50:40 -0800945 for arg in &sig.args {
946 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700947 write!(out, ", ");
948 }
949 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700950 Type::Str(_) => {
951 out.builtin.rust_str_repr = true;
952 write!(out, "::rust::impl<::rust::Str>::repr(");
953 }
David Tolnay73b72642020-11-25 17:44:05 -0800954 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700955 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800956 write!(out, "::rust::impl<");
957 write_type(out, &arg.ty);
958 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700959 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700960 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700961 _ => {}
962 }
963 write!(out, "{}", arg.ident);
964 match &arg.ty {
965 Type::RustBox(_) => write!(out, ".into_raw()"),
966 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800967 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700968 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700969 _ => {}
970 }
David Tolnay9d840362020-11-10 08:50:40 -0800971 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700972 }
973 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -0800974 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700975 write!(out, ", ");
976 }
977 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -0800978 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700979 }
980 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -0800981 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700982 write!(out, ", ");
983 }
984 write!(out, "extern$");
985 }
986 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400987 if !indirect_return {
988 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -0800989 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400990 write!(out, ")");
991 }
David Tolnay439cde22020-04-20 00:46:25 -0700992 }
993 }
994 writeln!(out, ";");
995 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -0700996 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700997 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700998 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700999 writeln!(out, " }}");
1000 }
1001 if indirect_return {
1002 out.include.utility = true;
1003 writeln!(out, " return ::std::move(return$.value);");
1004 }
1005 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001006}
1007
David Tolnaya7c2ea12020-10-30 21:32:53 -07001008fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001009 match ty {
1010 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001011 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001012 }
1013}
1014
David Tolnay75dca2e2020-03-25 20:17:52 -07001015fn indirect_return(sig: &Signature, types: &Types) -> bool {
1016 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001017 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001018 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001019}
1020
David Tolnaya7c2ea12020-10-30 21:32:53 -07001021fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001022 match ty {
1023 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001024 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001025 write!(out, "*");
1026 }
1027 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001028 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001029 write!(out, "const ");
1030 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001031 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001032 write!(out, " *");
1033 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001034 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001035 }
1036}
1037
David Tolnaya7c2ea12020-10-30 21:32:53 -07001038fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1039 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001040 match ty {
1041 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001042 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001043 _ => write_space_after_type(out, ty),
1044 }
1045}
1046
David Tolnaya7c2ea12020-10-30 21:32:53 -07001047fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001048 match ty {
1049 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001050 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001051 write!(out, "*");
1052 }
David Tolnay4a441222020-01-25 16:24:27 -08001053 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001054 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001055 write!(out, "const ");
1056 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001057 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001058 write!(out, " *");
1059 }
David Tolnay73b72642020-11-25 17:44:05 -08001060 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001061 out.builtin.ptr_len = true;
1062 write!(out, "::rust::repr::PtrLen ");
1063 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001064 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1065 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001066 }
1067}
1068
David Tolnaya7c2ea12020-10-30 21:32:53 -07001069fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001070 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001071 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001072 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001073 write!(out, "*");
1074 }
David Tolnay73b72642020-11-25 17:44:05 -08001075 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001076 out.builtin.ptr_len = true;
1077 write!(out, "::rust::repr::PtrLen ");
1078 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001079 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001080 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001081 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001082 write!(out, "*");
1083 }
1084 write!(out, "{}", arg.ident);
1085}
1086
David Tolnaya7c2ea12020-10-30 21:32:53 -07001087fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001088 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001089 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001090 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001091 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001092 },
1093 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001094 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001095 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001096 write!(out, ">");
1097 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001098 Type::RustVec(ty) => {
1099 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001100 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001101 write!(out, ">");
1102 }
David Tolnay7db73692019-10-20 14:51:12 -04001103 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001104 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001105 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001106 write!(out, ">");
1107 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001108 Type::SharedPtr(ptr) => {
1109 write!(out, "::std::shared_ptr<");
1110 write_type(out, &ptr.inner);
1111 write!(out, ">");
1112 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001113 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001114 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001115 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001116 write!(out, ">");
1117 }
David Tolnay7db73692019-10-20 14:51:12 -04001118 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001119 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001120 write!(out, "const ");
1121 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001122 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001123 write!(out, " &");
1124 }
1125 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001126 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001127 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001128 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001129 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001130 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001131 write!(out, "const ");
1132 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001133 write_type(out, &slice.inner);
1134 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001135 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001136 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001137 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001138 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001139 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001140 None => write!(out, "void"),
1141 }
1142 write!(out, "(");
1143 for (i, arg) in f.args.iter().enumerate() {
1144 if i > 0 {
1145 write!(out, ", ");
1146 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001147 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001148 }
1149 write!(out, ")>");
1150 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001151 Type::Array(a) => {
1152 write!(out, "::std::array<");
1153 write_type(out, &a.inner);
1154 write!(out, ", {}>", &a.len);
1155 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001156 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001157 }
1158}
1159
David Tolnayf6a89f22020-05-10 23:39:27 -07001160fn write_atom(out: &mut OutFile, atom: Atom) {
1161 match atom {
1162 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001163 Char => write!(out, "char"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001164 U8 => write!(out, "uint8_t"),
1165 U16 => write!(out, "uint16_t"),
1166 U32 => write!(out, "uint32_t"),
1167 U64 => write!(out, "uint64_t"),
1168 Usize => write!(out, "size_t"),
1169 I8 => write!(out, "int8_t"),
1170 I16 => write!(out, "int16_t"),
1171 I32 => write!(out, "int32_t"),
1172 I64 => write!(out, "int64_t"),
1173 Isize => write!(out, "::rust::isize"),
1174 F32 => write!(out, "float"),
1175 F64 => write!(out, "double"),
1176 CxxString => write!(out, "::std::string"),
1177 RustString => write!(out, "::rust::String"),
1178 }
1179}
1180
David Tolnaya7c2ea12020-10-30 21:32:53 -07001181fn write_type_space(out: &mut OutFile, ty: &Type) {
1182 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001183 write_space_after_type(out, ty);
1184}
1185
1186fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001187 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001188 Type::Ident(_)
1189 | Type::RustBox(_)
1190 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001191 | Type::SharedPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001192 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001193 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001194 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001195 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001196 | Type::Fn(_)
1197 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001198 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001199 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001200 }
1201}
1202
David Tolnay1bdb4712020-11-25 07:27:54 -08001203#[derive(Copy, Clone)]
1204enum UniquePtr<'a> {
1205 Ident(&'a ResolvableName),
1206 CxxVector(&'a ResolvableName),
1207}
1208
1209trait ToTypename {
1210 fn to_typename(&self, types: &Types) -> String;
1211}
1212
1213impl ToTypename for ResolvableName {
1214 fn to_typename(&self, types: &Types) -> String {
1215 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001216 }
1217}
1218
David Tolnay1bdb4712020-11-25 07:27:54 -08001219impl<'a> ToTypename for UniquePtr<'a> {
1220 fn to_typename(&self, types: &Types) -> String {
1221 match self {
1222 UniquePtr::Ident(ident) => ident.to_typename(types),
1223 UniquePtr::CxxVector(element) => {
1224 format!("::std::vector<{}>", element.to_typename(types))
1225 }
1226 }
1227 }
1228}
1229
1230trait ToMangled {
1231 fn to_mangled(&self, types: &Types) -> Symbol;
1232}
1233
1234impl ToMangled for ResolvableName {
1235 fn to_mangled(&self, types: &Types) -> Symbol {
1236 self.to_symbol(types)
1237 }
1238}
1239
1240impl<'a> ToMangled for UniquePtr<'a> {
1241 fn to_mangled(&self, types: &Types) -> Symbol {
1242 match self {
1243 UniquePtr::Ident(ident) => ident.to_mangled(types),
1244 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1245 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001246 }
1247}
1248
David Tolnaya7c2ea12020-10-30 21:32:53 -07001249fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001250 if out.header {
1251 return;
1252 }
1253
1254 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001255 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001256 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001257 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001258 if let Type::RustBox(ty) = ty {
1259 if let Type::Ident(inner) = &ty.inner {
1260 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001261 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001262 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001263 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001264 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001265 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001266 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001267 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001268 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001269 }
David Tolnay7db73692019-10-20 14:51:12 -04001270 } else if let Type::UniquePtr(ptr) = ty {
1271 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001272 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001273 && (!out.types.aliases.contains_key(&inner.rust)
1274 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001275 {
David Tolnay7db73692019-10-20 14:51:12 -04001276 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001277 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001278 }
1279 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001280 } else if let Type::SharedPtr(ptr) = ty {
1281 if let Type::Ident(inner) = &ptr.inner {
1282 if Atom::from(&inner.rust).is_none()
1283 && !(out.types.aliases.contains_key(&inner.rust)
1284 || out.types.explicit_impls.contains(ty))
1285 {
1286 out.next_section();
1287 write_shared_ptr(out, inner);
1288 }
1289 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001290 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001291 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001292 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001293 && (!out.types.aliases.contains_key(&inner.rust)
1294 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001295 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001296 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001297 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001298 }
1299 }
1300 }
1301 }
David Tolnay0c033e32020-11-01 15:15:48 -08001302 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001303
David Tolnay0c033e32020-11-01 15:15:48 -08001304 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001305 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001306 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001307 if let Type::RustBox(ty) = ty {
1308 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001309 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001310 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001311 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001312 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001313 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001314 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001315 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001316 }
David Tolnay7db73692019-10-20 14:51:12 -04001317 }
1318 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001319 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001320 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001321}
1322
David Tolnay8faec772020-11-02 00:18:19 -08001323fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001324 let inner = ident.to_fully_qualified();
1325 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001326
David Tolnay0f0162f2020-11-16 23:43:37 -08001327 writeln!(out, "#ifndef CXXBRIDGE1_RUST_BOX_{}", instance);
1328 writeln!(out, "#define CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001329 writeln!(
1330 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001331 "void cxxbridge1$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001332 instance, inner,
1333 );
1334 writeln!(
1335 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001336 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001337 instance, inner,
1338 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001339 writeln!(out, "#endif // CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001340}
1341
David Tolnaya7c2ea12020-10-30 21:32:53 -07001342fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001343 let inner = element.to_typename(out.types);
1344 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001345
David Tolnay0f0162f2020-11-16 23:43:37 -08001346 writeln!(out, "#ifndef CXXBRIDGE1_RUST_VEC_{}", instance);
1347 writeln!(out, "#define CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001348 writeln!(
1349 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001350 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001351 instance, inner,
1352 );
1353 writeln!(
1354 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001355 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001356 instance, inner,
1357 );
1358 writeln!(
1359 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001360 "size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001361 instance, inner,
1362 );
David Tolnay219c0792020-04-24 20:31:37 -07001363 writeln!(
1364 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001365 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001366 inner, instance,
1367 );
David Tolnay503d0192020-04-24 22:18:56 -07001368 writeln!(
1369 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001370 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001371 instance, inner,
1372 );
1373 writeln!(
1374 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001375 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001376 instance, inner,
1377 );
1378 writeln!(
1379 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001380 "size_t cxxbridge1$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001381 instance,
1382 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001383 writeln!(out, "#endif // CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001384}
1385
David Tolnay8faec772020-11-02 00:18:19 -08001386fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001387 let inner = ident.to_fully_qualified();
1388 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001389
1390 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001391 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001392 writeln!(out, " cxxbridge1$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001393 writeln!(out, "}}");
1394
1395 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001396 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001397 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001398 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001399}
1400
David Tolnaya7c2ea12020-10-30 21:32:53 -07001401fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001402 let inner = element.to_typename(out.types);
1403 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001404
Myron Ahneba35cf2020-02-05 19:41:51 +07001405 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001406 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001407 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001408 writeln!(out, "}}");
1409
1410 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001411 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001412 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001413 writeln!(out, "}}");
1414
1415 writeln!(out, "template <>");
1416 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001417 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001418 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001419
1420 writeln!(out, "template <>");
1421 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001422 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001423 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001424
1425 writeln!(out, "template <>");
David Tolnayfb6b73c2020-11-10 14:32:16 -08001426 writeln!(
1427 out,
1428 "void Vec<{}>::reserve_total(size_t cap) noexcept {{",
1429 inner,
1430 );
1431 writeln!(
1432 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001433 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001434 instance,
1435 );
1436 writeln!(out, "}}");
1437
1438 writeln!(out, "template <>");
1439 writeln!(out, "void Vec<{}>::set_len(size_t len) noexcept {{", inner);
1440 writeln!(
1441 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001442 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001443 instance,
1444 );
1445 writeln!(out, "}}");
1446
1447 writeln!(out, "template <>");
David Tolnay503d0192020-04-24 22:18:56 -07001448 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001449 writeln!(out, " return cxxbridge1$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001450 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001451}
1452
David Tolnaya7c2ea12020-10-30 21:32:53 -07001453fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001454 let ty = UniquePtr::Ident(ident);
1455 let instance = ty.to_mangled(out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001456
David Tolnay0f0162f2020-11-16 23:43:37 -08001457 writeln!(out, "#ifndef CXXBRIDGE1_UNIQUE_PTR_{}", instance);
1458 writeln!(out, "#define CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001459
David Tolnay1bdb4712020-11-25 07:27:54 -08001460 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001461
David Tolnay0f0162f2020-11-16 23:43:37 -08001462 writeln!(out, "#endif // CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001463}
1464
1465// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001466fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001467 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001468 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001469 let inner = ty.to_typename(out.types);
1470 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001471
David Tolnay63da4d32020-04-25 09:41:12 -07001472 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001473 // Some aliases are to opaque types; some are to trivial types. We can't
1474 // know at code generation time, so we generate both C++ and Rust side
1475 // bindings for a "new" method anyway. But the Rust code can't be called
1476 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001477 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001478 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001479 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001480 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001481 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001482 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001483 };
1484
David Tolnay53462762020-11-25 07:08:10 -08001485 let conditional_delete = match ty {
1486 UniquePtr::Ident(ident) => {
1487 !out.types.structs.contains_key(&ident.rust)
1488 && !out.types.enums.contains_key(&ident.rust)
1489 }
1490 UniquePtr::CxxVector(_) => false,
1491 };
1492
1493 if conditional_delete {
1494 out.builtin.is_complete = true;
1495 let definition = match ty {
1496 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1497 UniquePtr::CxxVector(_) => unreachable!(),
1498 };
1499 writeln!(
1500 out,
1501 "static_assert(::rust::is_complete<{}>::value, \"definition of {} is required\");",
1502 inner, definition,
1503 );
1504 }
David Tolnay7db73692019-10-20 14:51:12 -04001505 writeln!(
1506 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001507 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001508 inner,
1509 );
1510 writeln!(
1511 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001512 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001513 inner,
1514 );
1515 writeln!(
1516 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001517 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001518 instance, inner,
1519 );
David Tolnay7e219b82020-03-01 13:14:51 -08001520 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001521 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001522 if can_construct_from_value {
1523 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001524 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001525 "void cxxbridge1$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001526 instance, inner, inner,
1527 );
David Tolnay63da4d32020-04-25 09:41:12 -07001528 writeln!(
1529 out,
1530 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1531 inner, inner,
1532 );
1533 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001534 }
David Tolnay7db73692019-10-20 14:51:12 -04001535 writeln!(
1536 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001537 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001538 instance, inner, inner,
1539 );
David Tolnay7e219b82020-03-01 13:14:51 -08001540 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001541 writeln!(out, "}}");
1542 writeln!(
1543 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001544 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001545 inner, instance, inner,
1546 );
1547 writeln!(out, " return ptr.get();");
1548 writeln!(out, "}}");
1549 writeln!(
1550 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001551 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001552 inner, instance, inner,
1553 );
1554 writeln!(out, " return ptr.release();");
1555 writeln!(out, "}}");
1556 writeln!(
1557 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001558 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001559 instance, inner,
1560 );
David Tolnay53462762020-11-25 07:08:10 -08001561 if conditional_delete {
1562 out.builtin.deleter_if = true;
1563 writeln!(
1564 out,
1565 " ::rust::deleter_if<::rust::is_complete<{}>::value>{{}}(ptr);",
1566 inner,
1567 );
1568 } else {
1569 writeln!(out, " ptr->~unique_ptr();");
1570 }
David Tolnay7db73692019-10-20 14:51:12 -04001571 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001572}
Myron Ahneba35cf2020-02-05 19:41:51 +07001573
David Tolnayb3b24a12020-12-01 15:27:43 -08001574fn write_shared_ptr(out: &mut OutFile, ident: &ResolvableName) {
1575 let resolved = out.types.resolve(ident);
1576 let inner = resolved.to_fully_qualified();
1577 let instance = ident.to_symbol(out.types);
1578
1579 writeln!(out, "#ifndef CXXBRIDGE1_SHARED_PTR_{}", instance);
1580 writeln!(out, "#define CXXBRIDGE1_SHARED_PTR_{}", instance);
1581 out.include.new = true;
1582 out.include.utility = true;
1583
1584 // Some aliases are to opaque types; some are to trivial types. We can't
1585 // know at code generation time, so we generate both C++ and Rust side
1586 // bindings for a "new" method anyway. But the Rust code can't be called for
1587 // Opaque types because the 'new' method is not implemented.
1588 let can_construct_from_value = out.types.structs.contains_key(&ident.rust)
1589 || out.types.enums.contains_key(&ident.rust)
1590 || out.types.aliases.contains_key(&ident.rust);
1591
1592 if !out.types.structs.contains_key(&ident.rust) && !out.types.enums.contains_key(&ident.rust) {
1593 out.builtin.is_complete = true;
1594 writeln!(
1595 out,
1596 "static_assert(::rust::is_complete<{}>::value, \"definition of {} is required\");",
1597 inner, resolved.cxx,
1598 );
1599 }
1600 writeln!(
1601 out,
1602 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1603 inner,
1604 );
1605 writeln!(
1606 out,
1607 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1608 inner,
1609 );
1610 writeln!(
1611 out,
1612 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1613 instance, inner,
1614 );
1615 writeln!(out, " new (ptr) ::std::shared_ptr<{}>();", inner);
1616 writeln!(out, "}}");
1617 if can_construct_from_value {
1618 writeln!(
1619 out,
1620 "void cxxbridge1$shared_ptr${}$new(::std::shared_ptr<{}> *ptr, {} *value) noexcept {{",
1621 instance, inner, inner,
1622 );
1623 writeln!(
1624 out,
1625 " new (ptr) ::std::shared_ptr<{}>(new {}(::std::move(*value)));",
1626 inner, inner,
1627 );
1628 writeln!(out, "}}");
1629 }
1630 writeln!(
1631 out,
1632 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1633 instance, inner, inner,
1634 );
1635 writeln!(out, " new (ptr) ::std::shared_ptr<{}>(self);", inner);
1636 writeln!(out, "}}");
1637 writeln!(
1638 out,
1639 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1640 inner, instance, inner,
1641 );
1642 writeln!(out, " return self.get();");
1643 writeln!(out, "}}");
1644 writeln!(
1645 out,
1646 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1647 instance, inner,
1648 );
1649 writeln!(out, " self->~shared_ptr();");
1650 writeln!(out, "}}");
1651
1652 writeln!(out, "#endif // CXXBRIDGE1_SHARED_PTR_{}", instance);
1653}
1654
David Tolnay1bdb4712020-11-25 07:27:54 -08001655fn write_cxx_vector(out: &mut OutFile, element: &ResolvableName) {
1656 let inner = element.to_typename(out.types);
1657 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001658
David Tolnay0f0162f2020-11-16 23:43:37 -08001659 writeln!(out, "#ifndef CXXBRIDGE1_VECTOR_{}", instance);
1660 writeln!(out, "#define CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001661 writeln!(
1662 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001663 "size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001664 instance, inner,
1665 );
1666 writeln!(out, " return s.size();");
1667 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001668 writeln!(
1669 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001670 "const {} *cxxbridge1$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001671 inner, instance, inner,
1672 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001673 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001674 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001675
David Tolnay1bdb4712020-11-25 07:27:54 -08001676 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
David Tolnay63da4d32020-04-25 09:41:12 -07001677
David Tolnay0f0162f2020-11-16 23:43:37 -08001678 writeln!(out, "#endif // CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001679}