blob: 851b95373ee880fe8572c5433fb04f6e43c5e4de [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 Tolnay75ea17c2020-12-06 21:08:34 -08008 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, RustName, Signature, Struct, Trait,
9 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();
David Tolnay12320e12020-12-09 23:09:36 -0800161 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800162 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800163 let qualified = strct.name.to_fully_qualified();
164 writeln!(out, "template <> struct hash<{}> {{", qualified);
165 writeln!(
166 out,
167 " size_t operator()(const {} &self) const noexcept {{",
168 qualified,
169 );
170 let link_name = mangle::operator(&strct.name, "hash");
171 write!(out, " return ::");
172 for name in &strct.name.namespace {
173 write!(out, "{}::", name);
174 }
175 writeln!(out, "{}(self);", link_name);
176 writeln!(out, " }}");
177 writeln!(out, "}};");
178 }
179 }
180 }
181
182 out.end_block(Block::Namespace("std"));
183}
184
David Tolnaydfb82d72020-11-02 00:10:10 -0800185fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
186 for api in apis {
187 if let Api::Include(include) = api {
188 out.include.insert(include);
189 }
190 }
191
David Tolnaya7c2ea12020-10-30 21:32:53 -0700192 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400193 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700194 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700195 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
196 | Some(I64) => out.include.cstdint = true,
197 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800198 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700199 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800200 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800201 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700202 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800203 Type::RustBox(_) => out.builtin.rust_box = true,
204 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700205 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb3b24a12020-12-01 15:27:43 -0800206 Type::SharedPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800207 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700208 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800209 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800210 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800211 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800212 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400213 }
214 }
David Tolnayec66d112020-10-31 21:00:26 -0700215}
David Tolnayf51447e2020-03-06 14:14:27 -0800216
David Tolnay102c7ea2020-11-08 18:58:09 -0800217fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800218 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800219 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800220
David Tolnay17a934c2020-11-02 00:40:04 -0800221 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800222 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700223 writeln!(out, "#ifndef {}", guard);
224 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400225 for line in strct.doc.to_string().lines() {
226 writeln!(out, "//{}", line);
227 }
David Tolnay17a934c2020-11-02 00:40:04 -0800228 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800229
David Tolnay7db73692019-10-20 14:51:12 -0400230 for field in &strct.fields {
231 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700232 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400233 writeln!(out, "{};", field.ident);
234 }
David Tolnay84389352020-11-27 17:12:10 -0800235
236 if !methods.is_empty() || operator_eq || operator_ord {
David Tolnay102c7ea2020-11-08 18:58:09 -0800237 writeln!(out);
238 }
David Tolnay84389352020-11-27 17:12:10 -0800239
David Tolnay102c7ea2020-11-08 18:58:09 -0800240 for method in methods {
241 write!(out, " ");
242 let sig = &method.sig;
243 let local_name = method.name.cxx.to_string();
244 write_rust_function_shim_decl(out, &local_name, sig, false);
245 writeln!(out, ";");
246 }
David Tolnay84389352020-11-27 17:12:10 -0800247
David Tolnayb960ed22020-11-27 14:34:30 -0800248 if operator_eq {
249 writeln!(
250 out,
251 " bool operator==(const {} &) const noexcept;",
252 strct.name.cxx,
253 );
254 writeln!(
255 out,
256 " bool operator!=(const {} &) const noexcept;",
257 strct.name.cxx,
258 );
259 }
David Tolnay84389352020-11-27 17:12:10 -0800260
261 if operator_ord {
262 writeln!(
263 out,
264 " bool operator<(const {} &) const noexcept;",
265 strct.name.cxx,
266 );
267 writeln!(
268 out,
269 " bool operator<=(const {} &) const noexcept;",
270 strct.name.cxx,
271 );
272 writeln!(
273 out,
274 " bool operator>(const {} &) const noexcept;",
275 strct.name.cxx,
276 );
277 writeln!(
278 out,
279 " bool operator>=(const {} &) const noexcept;",
280 strct.name.cxx,
281 );
282 }
283
David Tolnay7db73692019-10-20 14:51:12 -0400284 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700285 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400286}
287
288fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
289 writeln!(out, "struct {};", ident);
290}
291
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800292fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800293 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800294 write_atom(out, enm.repr);
295 writeln!(out, ";");
296}
297
David Tolnay8faec772020-11-02 00:18:19 -0800298fn write_struct_using(out: &mut OutFile, ident: &Pair) {
299 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800300}
301
David Tolnay0b9b9f82020-11-01 20:41:00 -0800302fn write_struct_with_methods<'a>(
303 out: &mut OutFile<'a>,
304 ety: &'a ExternType,
305 methods: &[&ExternFn],
306) {
David Tolnay17a934c2020-11-02 00:40:04 -0800307 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800308 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700309 writeln!(out, "#ifndef {}", guard);
310 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700311 for line in ety.doc.to_string().lines() {
312 writeln!(out, "//{}", line);
313 }
David Tolnay365fc7c2020-11-25 16:08:13 -0800314 out.builtin.opaque = true;
David Tolnay7c06b862020-11-25 16:59:09 -0800315 writeln!(
316 out,
317 "struct {} final : public ::rust::Opaque {{",
318 ety.name.cxx,
319 );
Joel Galenson968738f2020-04-15 14:19:33 -0700320 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700321 write!(out, " ");
322 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800323 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700324 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700325 writeln!(out, ";");
326 }
327 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700328 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700329}
330
David Tolnay0b9b9f82020-11-01 20:41:00 -0800331fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800332 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800333 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700334 writeln!(out, "#ifndef {}", guard);
335 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700336 for line in enm.doc.to_string().lines() {
337 writeln!(out, "//{}", line);
338 }
David Tolnay17a934c2020-11-02 00:40:04 -0800339 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700340 write_atom(out, enm.repr);
341 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700342 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700343 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700344 }
345 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700346 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700347}
348
David Tolnay0b9b9f82020-11-01 20:41:00 -0800349fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800350 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800351 out.include.type_traits = true;
352 writeln!(
353 out,
354 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
355 enm.name.cxx,
356 );
David Tolnay17a934c2020-11-02 00:40:04 -0800357 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700358 write_atom(out, enm.repr);
359 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700360 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700361 write!(out, "static_assert(static_cast<");
362 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700363 writeln!(
364 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700365 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnay17a934c2020-11-02 00:40:04 -0800366 enm.name.cxx, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700367 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700368 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700369}
370
David Tolnay8faec772020-11-02 00:18:19 -0800371fn check_trivial_extern_type(out: &mut OutFile, id: &Pair) {
David Tolnay174bd952020-11-02 09:23:12 -0800372 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700373 // necessary for soundness. That's because triviality is always declared by
374 // the user in the form of an unsafe impl of cxx::ExternType:
375 //
376 // unsafe impl ExternType for MyType {
377 // type Id = cxx::type_id!("...");
378 // type Kind = cxx::kind::Trivial;
379 // }
380 //
381 // Since the user went on the record with their unsafe impl to unsafely
382 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800383 // them if that were wrong. However, in practice correctly reasoning about
384 // the relocatability of C++ types is challenging, particularly if the type
385 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700386 //
David Tolnay174bd952020-11-02 09:23:12 -0800387 // There may be legitimate reasons to opt out of this assertion for support
388 // of types that the programmer knows are soundly Rust-movable despite not
389 // being recognized as such by the C++ type system due to a move constructor
390 // or destructor. To opt out of the relocatability check, they need to do
391 // one of the following things in any header used by `include!` in their
392 // bridge.
393 //
394 // --- if they define the type:
395 // struct MyType {
396 // ...
397 // + using IsRelocatable = std::true_type;
398 // };
399 //
400 // --- otherwise:
401 // + template <>
402 // + struct rust::IsRelocatable<MyType> : std::true_type {};
403 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700404
David Tolnay174bd952020-11-02 09:23:12 -0800405 let id = id.to_fully_qualified();
406 out.builtin.relocatable = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700407 writeln!(out, "static_assert(");
David Tolnay174bd952020-11-02 09:23:12 -0800408 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700409 writeln!(
410 out,
David Tolnay174bd952020-11-02 09:23:12 -0800411 " \"type {} marked as Trivial in Rust is not trivially move constructible and trivially destructible in C++\");",
David Tolnay7426cc12020-10-03 19:04:04 -0700412 id,
413 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700414}
415
David Tolnayb960ed22020-11-27 14:34:30 -0800416fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
417 out.set_namespace(&strct.name.namespace);
418 out.begin_block(Block::ExternC);
419
420 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800421 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800422 writeln!(
423 out,
424 "bool {}(const {1} &, const {1} &) noexcept;",
425 link_name, strct.name.cxx,
426 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800427
428 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800429 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800430 writeln!(
431 out,
432 "bool {}(const {1} &, const {1} &) noexcept;",
433 link_name, strct.name.cxx,
434 );
435 }
David Tolnayb960ed22020-11-27 14:34:30 -0800436 }
437
David Tolnay84389352020-11-27 17:12:10 -0800438 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800439 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800440 writeln!(
441 out,
442 "bool {}(const {1} &, const {1} &) noexcept;",
443 link_name, strct.name.cxx,
444 );
445
David Tolnaya05f9402020-11-27 17:44:05 -0800446 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800447 writeln!(
448 out,
449 "bool {}(const {1} &, const {1} &) noexcept;",
450 link_name, strct.name.cxx,
451 );
452
453 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800454 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800455 writeln!(
456 out,
457 "bool {}(const {1} &, const {1} &) noexcept;",
458 link_name, strct.name.cxx,
459 );
460
David Tolnaya05f9402020-11-27 17:44:05 -0800461 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800462 writeln!(
463 out,
464 "bool {}(const {1} &, const {1} &) noexcept;",
465 link_name, strct.name.cxx,
466 );
467 }
468 }
469
David Tolnay7da38202020-11-27 17:36:16 -0800470 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800471 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800472 let link_name = mangle::operator(&strct.name, "hash");
473 writeln!(
474 out,
475 "size_t {}(const {} &) noexcept;",
476 link_name, strct.name.cxx,
477 );
478 }
479
David Tolnayb960ed22020-11-27 14:34:30 -0800480 out.end_block(Block::ExternC);
481}
482
483fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
484 if out.header {
485 return;
486 }
487
488 out.set_namespace(&strct.name.namespace);
489
490 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800491 out.next_section();
492 writeln!(
493 out,
494 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
495 strct.name.cxx,
496 );
David Tolnaya05f9402020-11-27 17:44:05 -0800497 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800498 writeln!(out, " return {}(*this, rhs);", link_name);
499 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800500
David Tolnayb960ed22020-11-27 14:34:30 -0800501 out.next_section();
502 writeln!(
503 out,
504 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
505 strct.name.cxx,
506 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800507 if derive::contains(&strct.derives, Trait::Eq) {
508 writeln!(out, " return !(*this == rhs);");
509 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800510 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800511 writeln!(out, " return {}(*this, rhs);", link_name);
512 }
David Tolnayb960ed22020-11-27 14:34:30 -0800513 writeln!(out, "}}");
514 }
David Tolnay84389352020-11-27 17:12:10 -0800515
516 if derive::contains(&strct.derives, Trait::PartialOrd) {
517 out.next_section();
518 writeln!(
519 out,
520 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
521 strct.name.cxx,
522 );
David Tolnaya05f9402020-11-27 17:44:05 -0800523 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800524 writeln!(out, " return {}(*this, rhs);", link_name);
525 writeln!(out, "}}");
526
527 out.next_section();
528 writeln!(
529 out,
530 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
531 strct.name.cxx,
532 );
David Tolnaya05f9402020-11-27 17:44:05 -0800533 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800534 writeln!(out, " return {}(*this, rhs);", link_name);
535 writeln!(out, "}}");
536
537 out.next_section();
538 writeln!(
539 out,
540 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
541 strct.name.cxx,
542 );
543 if derive::contains(&strct.derives, Trait::Ord) {
544 writeln!(out, " return !(*this <= rhs);");
545 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800546 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800547 writeln!(out, " return {}(*this, rhs);", link_name);
548 }
549 writeln!(out, "}}");
550
551 out.next_section();
552 writeln!(
553 out,
554 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
555 strct.name.cxx,
556 );
557 if derive::contains(&strct.derives, Trait::Ord) {
558 writeln!(out, " return !(*this < rhs);");
559 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800560 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800561 writeln!(out, " return {}(*this, rhs);", link_name);
562 }
563 writeln!(out, "}}");
564 }
David Tolnayb960ed22020-11-27 14:34:30 -0800565}
566
David Tolnay0b9b9f82020-11-01 20:41:00 -0800567fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800568 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800569 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800570 out.begin_block(Block::ExternC);
David Tolnaye1476af2020-11-01 13:47:25 -0800571 if let Some(annotation) = &out.opt.cxx_impl_annotations {
David Tolnaycc1ae762020-10-31 15:53:50 -0700572 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700573 }
David Tolnayebef4a22020-03-17 15:33:47 -0700574 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700575 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700576 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700577 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700578 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700579 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700580 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700581 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700582 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800583 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700584 write!(out, "const ");
585 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700586 write!(
587 out,
588 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800589 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700590 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700591 }
David Tolnay7db73692019-10-20 14:51:12 -0400592 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700593 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400594 write!(out, ", ");
595 }
David Tolnaya46a2372020-03-06 10:03:48 -0800596 if arg.ty == RustString {
597 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700598 } else if let Type::RustVec(_) = arg.ty {
599 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800600 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700601 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400602 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700603 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400604 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700605 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400606 write!(out, ", ");
607 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700608 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400609 write!(out, "*return$");
610 }
611 writeln!(out, ") noexcept {{");
612 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700613 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700614 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800615 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700616 Some(receiver) => write!(
617 out,
618 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700619 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800620 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700621 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700622 }
David Tolnay7db73692019-10-20 14:51:12 -0400623 for (i, arg) in efn.args.iter().enumerate() {
624 if i > 0 {
625 write!(out, ", ");
626 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700627 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400628 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700629 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700630 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800631 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700632 write!(out, " const");
633 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700634 }
635 write!(out, " = ");
636 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800637 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700638 Some(receiver) => write!(
639 out,
640 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700641 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800642 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700643 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700644 }
645 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400646 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700647 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700648 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700649 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700650 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700651 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700652 writeln!(out, " [&] {{");
653 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700654 }
David Tolnay7db73692019-10-20 14:51:12 -0400655 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700656 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400657 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700658 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400659 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700660 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400661 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700662 }
663 match &efn.ret {
664 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700665 Some(Type::Str(_)) if !indirect_return => {
666 out.builtin.rust_str_repr = true;
667 write!(out, "::rust::impl<::rust::Str>::repr(");
668 }
David Tolnay73b72642020-11-25 17:44:05 -0800669 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700670 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800671 write!(out, "::rust::impl<");
672 write_type(out, ty);
673 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700674 }
David Tolnay99642622020-03-25 13:07:35 -0700675 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400676 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700677 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800678 None => write!(out, "{}$(", efn.name.rust),
679 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700680 }
David Tolnay7db73692019-10-20 14:51:12 -0400681 for (i, arg) in efn.args.iter().enumerate() {
682 if i > 0 {
683 write!(out, ", ");
684 }
685 if let Type::RustBox(_) = &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, "::from_raw({})", arg.ident);
688 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700689 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400690 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700691 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700692 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700693 write!(
694 out,
695 "::rust::impl<::rust::Str>::new_unchecked({})",
696 arg.ident,
697 );
David Tolnaya46a2372020-03-06 10:03:48 -0800698 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700699 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800700 write!(
701 out,
702 "::rust::String(::rust::unsafe_bitcopy, *{})",
703 arg.ident,
704 );
David Tolnay313b10e2020-04-25 16:30:51 -0700705 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700706 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700707 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700708 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800709 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800710 write_type(out, &arg.ty);
711 write!(out, "(static_cast<");
712 if slice.mutability.is_none() {
713 write!(out, "const ");
714 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800715 write_type_space(out, &slice.inner);
716 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700717 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700718 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800719 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400720 } else {
721 write!(out, "{}", arg.ident);
722 }
723 }
724 write!(out, ")");
725 match &efn.ret {
726 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
727 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800728 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400729 _ => {}
730 }
731 if indirect_return {
732 write!(out, ")");
733 }
734 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700735 if efn.throws {
736 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800737 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700738 writeln!(out, " throw$.ptr = nullptr;");
739 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700740 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700741 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700742 writeln!(
743 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800744 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700745 );
David Tolnay5d121442020-03-17 22:14:40 -0700746 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700747 writeln!(out, " return throw$;");
748 }
David Tolnay7db73692019-10-20 14:51:12 -0400749 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700750 for arg in &efn.args {
751 if let Type::Fn(f) = &arg.ty {
752 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700753 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700754 }
755 }
David Tolnayca563ee2020-11-01 20:12:27 -0800756 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700757}
758
759fn write_function_pointer_trampoline(
760 out: &mut OutFile,
761 efn: &ExternFn,
762 var: &Ident,
763 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700764) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700765 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700766 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700767 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700768
769 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700770 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
771 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400772}
773
David Tolnay0b9b9f82020-11-01 20:41:00 -0800774fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800775 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800776 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700777 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700778 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700779 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800780 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700781}
782
783fn write_rust_function_decl_impl(
784 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700785 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700786 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700787 indirect_call: bool,
788) {
David Tolnay04770742020-11-01 13:50:50 -0800789 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700790 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700791 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700792 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700793 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700794 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700795 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700796 write!(out, "{}(", link_name);
797 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700798 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800799 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700800 write!(out, "const ");
801 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700802 write!(
803 out,
804 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800805 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700806 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700807 needs_comma = true;
808 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700809 for arg in &sig.args {
810 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400811 write!(out, ", ");
812 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700813 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700814 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400815 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700816 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700817 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400818 write!(out, ", ");
819 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700820 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400821 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700822 needs_comma = true;
823 }
824 if indirect_call {
825 if needs_comma {
826 write!(out, ", ");
827 }
828 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400829 }
830 writeln!(out, ") noexcept;");
831}
832
David Tolnay0b9b9f82020-11-01 20:41:00 -0800833fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800834 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400835 for line in efn.doc.to_string().lines() {
836 writeln!(out, "//{}", line);
837 }
David Tolnaya73853b2020-04-20 01:19:56 -0700838 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800839 None => efn.name.cxx.to_string(),
840 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700841 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700842 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700843 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700844 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700845}
846
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700847fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700848 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700849 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700850 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700851 indirect_call: bool,
852) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700853 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700854 write!(out, "{}(", local_name);
855 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400856 if i > 0 {
857 write!(out, ", ");
858 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700859 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400860 write!(out, "{}", arg.ident);
861 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700862 if indirect_call {
863 if !sig.args.is_empty() {
864 write!(out, ", ");
865 }
866 write!(out, "void *extern$");
867 }
David Tolnay1e548172020-03-16 13:37:09 -0700868 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700869 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800870 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700871 write!(out, " const");
872 }
873 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700874 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700875 write!(out, " noexcept");
876 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700877}
878
879fn write_rust_function_shim_impl(
880 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700881 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700882 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700883 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700884 indirect_call: bool,
885) {
886 if out.header && sig.receiver.is_some() {
887 // We've already defined this inside the struct.
888 return;
889 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700890 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400891 if out.header {
892 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700893 return;
David Tolnay7db73692019-10-20 14:51:12 -0400894 }
David Tolnay439cde22020-04-20 00:46:25 -0700895 writeln!(out, " {{");
896 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700897 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700898 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700899 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700900 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700901 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700902 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
903 }
904 }
905 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700906 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700907 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700908 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700909 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700910 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700911 writeln!(out, "> return$;");
912 write!(out, " ");
913 } else if let Some(ret) = &sig.ret {
914 write!(out, "return ");
915 match ret {
916 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700917 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700918 write!(out, "::from_raw(");
919 }
920 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700921 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700922 write!(out, "(");
923 }
924 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700925 Type::Str(_) => {
926 out.builtin.rust_str_new_unchecked = true;
927 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
928 }
David Tolnay73b72642020-11-25 17:44:05 -0800929 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700930 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800931 write!(out, "::rust::impl<");
932 write_type(out, ret);
933 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700934 }
David Tolnay439cde22020-04-20 00:46:25 -0700935 _ => {}
936 }
937 }
938 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700939 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700940 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700941 }
942 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -0800943 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -0700944 if sig.receiver.is_some() {
945 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -0800946 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700947 }
David Tolnay9d840362020-11-10 08:50:40 -0800948 for arg in &sig.args {
949 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700950 write!(out, ", ");
951 }
952 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700953 Type::Str(_) => {
954 out.builtin.rust_str_repr = true;
955 write!(out, "::rust::impl<::rust::Str>::repr(");
956 }
David Tolnay73b72642020-11-25 17:44:05 -0800957 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700958 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800959 write!(out, "::rust::impl<");
960 write_type(out, &arg.ty);
961 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700962 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700963 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700964 _ => {}
965 }
966 write!(out, "{}", arg.ident);
967 match &arg.ty {
968 Type::RustBox(_) => write!(out, ".into_raw()"),
969 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800970 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700971 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700972 _ => {}
973 }
David Tolnay9d840362020-11-10 08:50:40 -0800974 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700975 }
976 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -0800977 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700978 write!(out, ", ");
979 }
980 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -0800981 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700982 }
983 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -0800984 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700985 write!(out, ", ");
986 }
987 write!(out, "extern$");
988 }
989 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400990 if !indirect_return {
991 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -0800992 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400993 write!(out, ")");
994 }
David Tolnay439cde22020-04-20 00:46:25 -0700995 }
996 }
997 writeln!(out, ";");
998 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -0700999 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001000 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001001 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001002 writeln!(out, " }}");
1003 }
1004 if indirect_return {
1005 out.include.utility = true;
1006 writeln!(out, " return ::std::move(return$.value);");
1007 }
1008 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001009}
1010
David Tolnaya7c2ea12020-10-30 21:32:53 -07001011fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001012 match ty {
1013 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001014 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001015 }
1016}
1017
David Tolnay75dca2e2020-03-25 20:17:52 -07001018fn indirect_return(sig: &Signature, types: &Types) -> bool {
1019 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001020 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001021 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001022}
1023
David Tolnaya7c2ea12020-10-30 21:32:53 -07001024fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001025 match ty {
1026 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001027 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001028 write!(out, "*");
1029 }
1030 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001031 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001032 write!(out, "const ");
1033 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001034 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001035 write!(out, " *");
1036 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001037 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001038 }
1039}
1040
David Tolnaya7c2ea12020-10-30 21:32:53 -07001041fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1042 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001043 match ty {
1044 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001045 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001046 _ => write_space_after_type(out, ty),
1047 }
1048}
1049
David Tolnaya7c2ea12020-10-30 21:32:53 -07001050fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001051 match ty {
1052 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001053 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001054 write!(out, "*");
1055 }
David Tolnay4a441222020-01-25 16:24:27 -08001056 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001057 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001058 write!(out, "const ");
1059 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001060 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001061 write!(out, " *");
1062 }
David Tolnay73b72642020-11-25 17:44:05 -08001063 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001064 out.builtin.ptr_len = true;
1065 write!(out, "::rust::repr::PtrLen ");
1066 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001067 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1068 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001069 }
1070}
1071
David Tolnaya7c2ea12020-10-30 21:32:53 -07001072fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001073 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001074 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001075 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001076 write!(out, "*");
1077 }
David Tolnay73b72642020-11-25 17:44:05 -08001078 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001079 out.builtin.ptr_len = true;
1080 write!(out, "::rust::repr::PtrLen ");
1081 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001082 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001083 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001084 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001085 write!(out, "*");
1086 }
1087 write!(out, "{}", arg.ident);
1088}
1089
David Tolnaya7c2ea12020-10-30 21:32:53 -07001090fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001091 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001092 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001093 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001094 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001095 },
1096 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001097 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001098 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001099 write!(out, ">");
1100 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001101 Type::RustVec(ty) => {
1102 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001103 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001104 write!(out, ">");
1105 }
David Tolnay7db73692019-10-20 14:51:12 -04001106 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001107 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001108 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001109 write!(out, ">");
1110 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001111 Type::SharedPtr(ptr) => {
1112 write!(out, "::std::shared_ptr<");
1113 write_type(out, &ptr.inner);
1114 write!(out, ">");
1115 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001116 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001117 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001118 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001119 write!(out, ">");
1120 }
David Tolnay7db73692019-10-20 14:51:12 -04001121 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001122 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001123 write!(out, "const ");
1124 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001125 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001126 write!(out, " &");
1127 }
1128 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001129 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001130 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001131 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001132 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001133 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001134 write!(out, "const ");
1135 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001136 write_type(out, &slice.inner);
1137 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001138 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001139 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001140 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001141 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001142 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001143 None => write!(out, "void"),
1144 }
1145 write!(out, "(");
1146 for (i, arg) in f.args.iter().enumerate() {
1147 if i > 0 {
1148 write!(out, ", ");
1149 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001150 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001151 }
1152 write!(out, ")>");
1153 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001154 Type::Array(a) => {
1155 write!(out, "::std::array<");
1156 write_type(out, &a.inner);
1157 write!(out, ", {}>", &a.len);
1158 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001159 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001160 }
1161}
1162
David Tolnayf6a89f22020-05-10 23:39:27 -07001163fn write_atom(out: &mut OutFile, atom: Atom) {
1164 match atom {
1165 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001166 Char => write!(out, "char"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001167 U8 => write!(out, "uint8_t"),
1168 U16 => write!(out, "uint16_t"),
1169 U32 => write!(out, "uint32_t"),
1170 U64 => write!(out, "uint64_t"),
1171 Usize => write!(out, "size_t"),
1172 I8 => write!(out, "int8_t"),
1173 I16 => write!(out, "int16_t"),
1174 I32 => write!(out, "int32_t"),
1175 I64 => write!(out, "int64_t"),
1176 Isize => write!(out, "::rust::isize"),
1177 F32 => write!(out, "float"),
1178 F64 => write!(out, "double"),
1179 CxxString => write!(out, "::std::string"),
1180 RustString => write!(out, "::rust::String"),
1181 }
1182}
1183
David Tolnaya7c2ea12020-10-30 21:32:53 -07001184fn write_type_space(out: &mut OutFile, ty: &Type) {
1185 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001186 write_space_after_type(out, ty);
1187}
1188
1189fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001190 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001191 Type::Ident(_)
1192 | Type::RustBox(_)
1193 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001194 | Type::SharedPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001195 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001196 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001197 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001198 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001199 | Type::Fn(_)
1200 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001201 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001202 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001203 }
1204}
1205
David Tolnay1bdb4712020-11-25 07:27:54 -08001206#[derive(Copy, Clone)]
1207enum UniquePtr<'a> {
David Tolnay75ea17c2020-12-06 21:08:34 -08001208 Ident(&'a RustName),
1209 CxxVector(&'a RustName),
David Tolnay1bdb4712020-11-25 07:27:54 -08001210}
1211
1212trait ToTypename {
1213 fn to_typename(&self, types: &Types) -> String;
1214}
1215
David Tolnay75ea17c2020-12-06 21:08:34 -08001216impl ToTypename for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001217 fn to_typename(&self, types: &Types) -> String {
1218 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001219 }
1220}
1221
David Tolnay1bdb4712020-11-25 07:27:54 -08001222impl<'a> ToTypename for UniquePtr<'a> {
1223 fn to_typename(&self, types: &Types) -> String {
1224 match self {
1225 UniquePtr::Ident(ident) => ident.to_typename(types),
1226 UniquePtr::CxxVector(element) => {
1227 format!("::std::vector<{}>", element.to_typename(types))
1228 }
1229 }
1230 }
1231}
1232
1233trait ToMangled {
1234 fn to_mangled(&self, types: &Types) -> Symbol;
1235}
1236
David Tolnay75ea17c2020-12-06 21:08:34 -08001237impl ToMangled for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001238 fn to_mangled(&self, types: &Types) -> Symbol {
1239 self.to_symbol(types)
1240 }
1241}
1242
1243impl<'a> ToMangled for UniquePtr<'a> {
1244 fn to_mangled(&self, types: &Types) -> Symbol {
1245 match self {
1246 UniquePtr::Ident(ident) => ident.to_mangled(types),
1247 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1248 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001249 }
1250}
1251
David Tolnaya7c2ea12020-10-30 21:32:53 -07001252fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001253 if out.header {
1254 return;
1255 }
1256
1257 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001258 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001259 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001260 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001261 if let Type::RustBox(ptr) = ty {
1262 if let Type::Ident(inner) = &ptr.inner {
1263 if Atom::from(&inner.rust).is_none()
1264 && (!out.types.aliases.contains_key(&inner.rust)
1265 || out.types.explicit_impls.contains(ty))
1266 {
1267 out.next_section();
1268 write_rust_box_extern(out, &out.types.resolve(&inner));
1269 }
David Tolnay7db73692019-10-20 14:51:12 -04001270 }
David Tolnayf33bc242020-12-04 18:27:02 -08001271 } else if let Type::RustVec(vec) = ty {
1272 if let Type::Ident(inner) = &vec.inner {
1273 if Atom::from(&inner.rust).is_none()
1274 && (!out.types.aliases.contains_key(&inner.rust)
1275 || out.types.explicit_impls.contains(ty))
1276 {
David Tolnay6787be62020-04-25 11:01:02 -07001277 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001278 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001279 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001280 }
David Tolnay7db73692019-10-20 14:51:12 -04001281 } else if let Type::UniquePtr(ptr) = ty {
1282 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001283 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001284 && (!out.types.aliases.contains_key(&inner.rust)
1285 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001286 {
David Tolnay7db73692019-10-20 14:51:12 -04001287 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001288 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001289 }
1290 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001291 } else if let Type::SharedPtr(ptr) = ty {
1292 if let Type::Ident(inner) = &ptr.inner {
1293 if Atom::from(&inner.rust).is_none()
David Tolnayb7453812020-12-01 21:46:55 -08001294 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnayb3b24a12020-12-01 15:27:43 -08001295 || out.types.explicit_impls.contains(ty))
1296 {
1297 out.next_section();
1298 write_shared_ptr(out, inner);
1299 }
1300 }
David Tolnayf33bc242020-12-04 18:27:02 -08001301 } else if let Type::CxxVector(vector) = ty {
1302 if let Type::Ident(inner) = &vector.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001303 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001304 && (!out.types.aliases.contains_key(&inner.rust)
1305 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001306 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001307 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001308 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001309 }
1310 }
1311 }
1312 }
David Tolnay0c033e32020-11-01 15:15:48 -08001313 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001314
David Tolnay0c033e32020-11-01 15:15:48 -08001315 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001316 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001317 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001318 if let Type::RustBox(ptr) = ty {
1319 if let Type::Ident(inner) = &ptr.inner {
1320 if Atom::from(&inner.rust).is_none()
1321 && (!out.types.aliases.contains_key(&inner.rust)
1322 || out.types.explicit_impls.contains(ty))
1323 {
1324 write_rust_box_impl(out, &out.types.resolve(&inner));
1325 }
David Tolnay7db73692019-10-20 14:51:12 -04001326 }
David Tolnayf33bc242020-12-04 18:27:02 -08001327 } else if let Type::RustVec(vec) = ty {
1328 if let Type::Ident(inner) = &vec.inner {
1329 if Atom::from(&inner.rust).is_none()
1330 && (!out.types.aliases.contains_key(&inner.rust)
1331 || out.types.explicit_impls.contains(ty))
1332 {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001333 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001334 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001335 }
David Tolnay7db73692019-10-20 14:51:12 -04001336 }
1337 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001338 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001339 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001340}
1341
David Tolnay8faec772020-11-02 00:18:19 -08001342fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001343 let inner = ident.to_fully_qualified();
1344 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001345
David Tolnay0f0162f2020-11-16 23:43:37 -08001346 writeln!(out, "#ifndef CXXBRIDGE1_RUST_BOX_{}", instance);
1347 writeln!(out, "#define CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001348 writeln!(
1349 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001350 "void cxxbridge1$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001351 instance, inner,
1352 );
1353 writeln!(
1354 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001355 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001356 instance, inner,
1357 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001358 writeln!(out, "#endif // CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001359}
1360
David Tolnay75ea17c2020-12-06 21:08:34 -08001361fn write_rust_vec_extern(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001362 let inner = element.to_typename(out.types);
1363 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001364
David Tolnay12320e12020-12-09 23:09:36 -08001365 out.include.cstddef = true;
1366
David Tolnay0f0162f2020-11-16 23:43:37 -08001367 writeln!(out, "#ifndef CXXBRIDGE1_RUST_VEC_{}", instance);
1368 writeln!(out, "#define CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001369 writeln!(
1370 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001371 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001372 instance, inner,
1373 );
1374 writeln!(
1375 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001376 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001377 instance, inner,
1378 );
1379 writeln!(
1380 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001381 "size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001382 instance, inner,
1383 );
David Tolnay219c0792020-04-24 20:31:37 -07001384 writeln!(
1385 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001386 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001387 inner, instance,
1388 );
David Tolnay503d0192020-04-24 22:18:56 -07001389 writeln!(
1390 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001391 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001392 instance, inner,
1393 );
1394 writeln!(
1395 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001396 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001397 instance, inner,
1398 );
1399 writeln!(
1400 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001401 "size_t cxxbridge1$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001402 instance,
1403 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001404 writeln!(out, "#endif // CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001405}
1406
David Tolnay8faec772020-11-02 00:18:19 -08001407fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001408 let inner = ident.to_fully_qualified();
1409 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001410
1411 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001412 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001413 writeln!(out, " cxxbridge1$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001414 writeln!(out, "}}");
1415
1416 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001417 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001418 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001419 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001420}
1421
David Tolnay75ea17c2020-12-06 21:08:34 -08001422fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001423 let inner = element.to_typename(out.types);
1424 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001425
David Tolnay12320e12020-12-09 23:09:36 -08001426 out.include.cstddef = true;
1427
Myron Ahneba35cf2020-02-05 19:41:51 +07001428 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001429 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001430 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001431 writeln!(out, "}}");
1432
1433 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001434 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001435 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001436 writeln!(out, "}}");
1437
1438 writeln!(out, "template <>");
1439 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001440 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001441 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001442
1443 writeln!(out, "template <>");
1444 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001445 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001446 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001447
1448 writeln!(out, "template <>");
David Tolnayfb6b73c2020-11-10 14:32:16 -08001449 writeln!(
1450 out,
1451 "void Vec<{}>::reserve_total(size_t cap) noexcept {{",
1452 inner,
1453 );
1454 writeln!(
1455 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001456 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001457 instance,
1458 );
1459 writeln!(out, "}}");
1460
1461 writeln!(out, "template <>");
1462 writeln!(out, "void Vec<{}>::set_len(size_t len) noexcept {{", inner);
1463 writeln!(
1464 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001465 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001466 instance,
1467 );
1468 writeln!(out, "}}");
1469
1470 writeln!(out, "template <>");
David Tolnay503d0192020-04-24 22:18:56 -07001471 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001472 writeln!(out, " return cxxbridge1$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001473 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001474}
1475
David Tolnay75ea17c2020-12-06 21:08:34 -08001476fn write_unique_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001477 let ty = UniquePtr::Ident(ident);
1478 let instance = ty.to_mangled(out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001479
David Tolnay0f0162f2020-11-16 23:43:37 -08001480 writeln!(out, "#ifndef CXXBRIDGE1_UNIQUE_PTR_{}", instance);
1481 writeln!(out, "#define CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001482
David Tolnay1bdb4712020-11-25 07:27:54 -08001483 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001484
David Tolnay0f0162f2020-11-16 23:43:37 -08001485 writeln!(out, "#endif // CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001486}
1487
1488// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001489fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001490 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001491 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001492 let inner = ty.to_typename(out.types);
1493 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001494
David Tolnay63da4d32020-04-25 09:41:12 -07001495 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001496 // Some aliases are to opaque types; some are to trivial types. We can't
1497 // know at code generation time, so we generate both C++ and Rust side
1498 // bindings for a "new" method anyway. But the Rust code can't be called
1499 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001500 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001501 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001502 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001503 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001504 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001505 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001506 };
1507
David Tolnay53462762020-11-25 07:08:10 -08001508 let conditional_delete = match ty {
1509 UniquePtr::Ident(ident) => {
1510 !out.types.structs.contains_key(&ident.rust)
1511 && !out.types.enums.contains_key(&ident.rust)
1512 }
1513 UniquePtr::CxxVector(_) => false,
1514 };
1515
1516 if conditional_delete {
1517 out.builtin.is_complete = true;
1518 let definition = match ty {
1519 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1520 UniquePtr::CxxVector(_) => unreachable!(),
1521 };
1522 writeln!(
1523 out,
1524 "static_assert(::rust::is_complete<{}>::value, \"definition of {} is required\");",
1525 inner, definition,
1526 );
1527 }
David Tolnay7db73692019-10-20 14:51:12 -04001528 writeln!(
1529 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001530 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001531 inner,
1532 );
1533 writeln!(
1534 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001535 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001536 inner,
1537 );
1538 writeln!(
1539 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001540 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001541 instance, inner,
1542 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001543 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001544 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001545 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001546 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001547 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001548 out,
David Tolnay0b881402020-12-01 21:05:08 -08001549 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1550 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001551 );
David Tolnay63da4d32020-04-25 09:41:12 -07001552 writeln!(
1553 out,
David Tolnay0b881402020-12-01 21:05:08 -08001554 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1555 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001556 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001557 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001558 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001559 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001560 }
David Tolnay7db73692019-10-20 14:51:12 -04001561 writeln!(
1562 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001563 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001564 instance, inner, inner,
1565 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001566 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001567 writeln!(out, "}}");
1568 writeln!(
1569 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001570 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001571 inner, instance, inner,
1572 );
1573 writeln!(out, " return ptr.get();");
1574 writeln!(out, "}}");
1575 writeln!(
1576 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001577 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001578 inner, instance, inner,
1579 );
1580 writeln!(out, " return ptr.release();");
1581 writeln!(out, "}}");
1582 writeln!(
1583 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001584 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001585 instance, inner,
1586 );
David Tolnay53462762020-11-25 07:08:10 -08001587 if conditional_delete {
1588 out.builtin.deleter_if = true;
1589 writeln!(
1590 out,
1591 " ::rust::deleter_if<::rust::is_complete<{}>::value>{{}}(ptr);",
1592 inner,
1593 );
1594 } else {
1595 writeln!(out, " ptr->~unique_ptr();");
1596 }
David Tolnay7db73692019-10-20 14:51:12 -04001597 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001598}
Myron Ahneba35cf2020-02-05 19:41:51 +07001599
David Tolnay75ea17c2020-12-06 21:08:34 -08001600fn write_shared_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnayb3b24a12020-12-01 15:27:43 -08001601 let resolved = out.types.resolve(ident);
1602 let inner = resolved.to_fully_qualified();
1603 let instance = ident.to_symbol(out.types);
1604
1605 writeln!(out, "#ifndef CXXBRIDGE1_SHARED_PTR_{}", instance);
1606 writeln!(out, "#define CXXBRIDGE1_SHARED_PTR_{}", instance);
1607 out.include.new = true;
1608 out.include.utility = true;
1609
1610 // Some aliases are to opaque types; some are to trivial types. We can't
1611 // know at code generation time, so we generate both C++ and Rust side
1612 // bindings for a "new" method anyway. But the Rust code can't be called for
1613 // Opaque types because the 'new' method is not implemented.
1614 let can_construct_from_value = out.types.structs.contains_key(&ident.rust)
1615 || out.types.enums.contains_key(&ident.rust)
1616 || out.types.aliases.contains_key(&ident.rust);
1617
David Tolnayb3b24a12020-12-01 15:27:43 -08001618 writeln!(
1619 out,
1620 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1621 inner,
1622 );
1623 writeln!(
1624 out,
1625 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1626 inner,
1627 );
1628 writeln!(
1629 out,
1630 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1631 instance, inner,
1632 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001633 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001634 writeln!(out, "}}");
1635 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001636 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001637 writeln!(
1638 out,
David Tolnay0b881402020-12-01 21:05:08 -08001639 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1640 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001641 );
1642 writeln!(
1643 out,
David Tolnay0b881402020-12-01 21:05:08 -08001644 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1645 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001646 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001647 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001648 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001649 writeln!(out, "}}");
1650 }
1651 writeln!(
1652 out,
1653 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1654 instance, inner, inner,
1655 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001656 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001657 writeln!(out, "}}");
1658 writeln!(
1659 out,
1660 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1661 inner, instance, inner,
1662 );
1663 writeln!(out, " return self.get();");
1664 writeln!(out, "}}");
1665 writeln!(
1666 out,
1667 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1668 instance, inner,
1669 );
1670 writeln!(out, " self->~shared_ptr();");
1671 writeln!(out, "}}");
1672
1673 writeln!(out, "#endif // CXXBRIDGE1_SHARED_PTR_{}", instance);
1674}
1675
David Tolnay75ea17c2020-12-06 21:08:34 -08001676fn write_cxx_vector(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001677 let inner = element.to_typename(out.types);
1678 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001679
David Tolnay12320e12020-12-09 23:09:36 -08001680 out.include.cstddef = true;
1681
David Tolnay0f0162f2020-11-16 23:43:37 -08001682 writeln!(out, "#ifndef CXXBRIDGE1_VECTOR_{}", instance);
1683 writeln!(out, "#define CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001684 writeln!(
1685 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001686 "size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001687 instance, inner,
1688 );
1689 writeln!(out, " return s.size();");
1690 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001691 writeln!(
1692 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001693 "const {} *cxxbridge1$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001694 inner, instance, inner,
1695 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001696 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001697 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001698
David Tolnay70820672020-12-07 11:15:14 -08001699 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001700 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
David Tolnay63da4d32020-04-25 09:41:12 -07001701
David Tolnay0f0162f2020-11-16 23:43:37 -08001702 writeln!(out, "#endif // CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001703}