blob: 81eeef0cea51a00212293d772e4063f02760fa9b [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 Tolnay7db73692019-10-20 14:51:12 -0400137 }
138
139 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800140 match api {
141 Api::Struct(strct) => write_struct_operators(out, strct),
142 Api::RustFunction(efn) => {
143 out.next_section();
144 write_rust_function_shim(out, efn);
145 }
146 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400147 }
148 }
David Tolnay7db73692019-10-20 14:51:12 -0400149}
150
David Tolnaydfb82d72020-11-02 00:10:10 -0800151fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
152 for api in apis {
153 if let Api::Include(include) = api {
154 out.include.insert(include);
155 }
156 }
157
David Tolnaya7c2ea12020-10-30 21:32:53 -0700158 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400159 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700160 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700161 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
162 | Some(I64) => out.include.cstdint = true,
163 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800164 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700165 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800166 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800167 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700168 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800169 Type::RustBox(_) => out.builtin.rust_box = true,
170 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700171 Type::UniquePtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800172 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700173 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800174 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800175 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800176 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800177 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400178 }
179 }
David Tolnayec66d112020-10-31 21:00:26 -0700180}
David Tolnayf51447e2020-03-06 14:14:27 -0800181
David Tolnay102c7ea2020-11-08 18:58:09 -0800182fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800183 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
184
David Tolnay17a934c2020-11-02 00:40:04 -0800185 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800186 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700187 writeln!(out, "#ifndef {}", guard);
188 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400189 for line in strct.doc.to_string().lines() {
190 writeln!(out, "//{}", line);
191 }
David Tolnay17a934c2020-11-02 00:40:04 -0800192 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400193 for field in &strct.fields {
194 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700195 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400196 writeln!(out, "{};", field.ident);
197 }
David Tolnayb960ed22020-11-27 14:34:30 -0800198 if !methods.is_empty() || operator_eq {
David Tolnay102c7ea2020-11-08 18:58:09 -0800199 writeln!(out);
200 }
201 for method in methods {
202 write!(out, " ");
203 let sig = &method.sig;
204 let local_name = method.name.cxx.to_string();
205 write_rust_function_shim_decl(out, &local_name, sig, false);
206 writeln!(out, ";");
207 }
David Tolnayb960ed22020-11-27 14:34:30 -0800208 if operator_eq {
209 writeln!(
210 out,
211 " bool operator==(const {} &) const noexcept;",
212 strct.name.cxx,
213 );
214 writeln!(
215 out,
216 " bool operator!=(const {} &) const noexcept;",
217 strct.name.cxx,
218 );
219 }
David Tolnay7db73692019-10-20 14:51:12 -0400220 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700221 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400222}
223
224fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
225 writeln!(out, "struct {};", ident);
226}
227
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800228fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800229 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800230 write_atom(out, enm.repr);
231 writeln!(out, ";");
232}
233
David Tolnay8faec772020-11-02 00:18:19 -0800234fn write_struct_using(out: &mut OutFile, ident: &Pair) {
235 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800236}
237
David Tolnay0b9b9f82020-11-01 20:41:00 -0800238fn write_struct_with_methods<'a>(
239 out: &mut OutFile<'a>,
240 ety: &'a ExternType,
241 methods: &[&ExternFn],
242) {
David Tolnay17a934c2020-11-02 00:40:04 -0800243 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800244 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700245 writeln!(out, "#ifndef {}", guard);
246 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700247 for line in ety.doc.to_string().lines() {
248 writeln!(out, "//{}", line);
249 }
David Tolnay365fc7c2020-11-25 16:08:13 -0800250 out.builtin.opaque = true;
David Tolnay7c06b862020-11-25 16:59:09 -0800251 writeln!(
252 out,
253 "struct {} final : public ::rust::Opaque {{",
254 ety.name.cxx,
255 );
Joel Galenson968738f2020-04-15 14:19:33 -0700256 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700257 write!(out, " ");
258 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800259 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700260 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700261 writeln!(out, ";");
262 }
263 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700264 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700265}
266
David Tolnay0b9b9f82020-11-01 20:41:00 -0800267fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800268 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800269 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700270 writeln!(out, "#ifndef {}", guard);
271 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700272 for line in enm.doc.to_string().lines() {
273 writeln!(out, "//{}", line);
274 }
David Tolnay17a934c2020-11-02 00:40:04 -0800275 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700276 write_atom(out, enm.repr);
277 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700278 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700279 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700280 }
281 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700282 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700283}
284
David Tolnay0b9b9f82020-11-01 20:41:00 -0800285fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800286 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800287 out.include.type_traits = true;
288 writeln!(
289 out,
290 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
291 enm.name.cxx,
292 );
David Tolnay17a934c2020-11-02 00:40:04 -0800293 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700294 write_atom(out, enm.repr);
295 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700296 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700297 write!(out, "static_assert(static_cast<");
298 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700299 writeln!(
300 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700301 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnay17a934c2020-11-02 00:40:04 -0800302 enm.name.cxx, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700303 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700304 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700305}
306
David Tolnay8faec772020-11-02 00:18:19 -0800307fn check_trivial_extern_type(out: &mut OutFile, id: &Pair) {
David Tolnay174bd952020-11-02 09:23:12 -0800308 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700309 // necessary for soundness. That's because triviality is always declared by
310 // the user in the form of an unsafe impl of cxx::ExternType:
311 //
312 // unsafe impl ExternType for MyType {
313 // type Id = cxx::type_id!("...");
314 // type Kind = cxx::kind::Trivial;
315 // }
316 //
317 // Since the user went on the record with their unsafe impl to unsafely
318 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800319 // them if that were wrong. However, in practice correctly reasoning about
320 // the relocatability of C++ types is challenging, particularly if the type
321 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700322 //
David Tolnay174bd952020-11-02 09:23:12 -0800323 // There may be legitimate reasons to opt out of this assertion for support
324 // of types that the programmer knows are soundly Rust-movable despite not
325 // being recognized as such by the C++ type system due to a move constructor
326 // or destructor. To opt out of the relocatability check, they need to do
327 // one of the following things in any header used by `include!` in their
328 // bridge.
329 //
330 // --- if they define the type:
331 // struct MyType {
332 // ...
333 // + using IsRelocatable = std::true_type;
334 // };
335 //
336 // --- otherwise:
337 // + template <>
338 // + struct rust::IsRelocatable<MyType> : std::true_type {};
339 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700340
David Tolnay174bd952020-11-02 09:23:12 -0800341 let id = id.to_fully_qualified();
342 out.builtin.relocatable = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700343 writeln!(out, "static_assert(");
David Tolnay174bd952020-11-02 09:23:12 -0800344 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700345 writeln!(
346 out,
David Tolnay174bd952020-11-02 09:23:12 -0800347 " \"type {} marked as Trivial in Rust is not trivially move constructible and trivially destructible in C++\");",
David Tolnay7426cc12020-10-03 19:04:04 -0700348 id,
349 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700350}
351
David Tolnayb960ed22020-11-27 14:34:30 -0800352fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
353 out.set_namespace(&strct.name.namespace);
354 out.begin_block(Block::ExternC);
355
356 if derive::contains(&strct.derives, Trait::PartialEq) {
357 let link_name = mangle::operator(&strct.name, "__operator_eq");
358 writeln!(
359 out,
360 "bool {}(const {1} &, const {1} &) noexcept;",
361 link_name, strct.name.cxx,
362 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800363
364 if !derive::contains(&strct.derives, Trait::Eq) {
365 let link_name = mangle::operator(&strct.name, "__operator_ne");
366 writeln!(
367 out,
368 "bool {}(const {1} &, const {1} &) noexcept;",
369 link_name, strct.name.cxx,
370 );
371 }
David Tolnayb960ed22020-11-27 14:34:30 -0800372 }
373
374 out.end_block(Block::ExternC);
375}
376
377fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
378 if out.header {
379 return;
380 }
381
382 out.set_namespace(&strct.name.namespace);
383
384 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800385 out.next_section();
386 writeln!(
387 out,
388 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
389 strct.name.cxx,
390 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800391 let link_name = mangle::operator(&strct.name, "__operator_eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800392 writeln!(out, " return {}(*this, rhs);", link_name);
393 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800394
David Tolnayb960ed22020-11-27 14:34:30 -0800395 out.next_section();
396 writeln!(
397 out,
398 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
399 strct.name.cxx,
400 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800401 if derive::contains(&strct.derives, Trait::Eq) {
402 writeln!(out, " return !(*this == rhs);");
403 } else {
404 let link_name = mangle::operator(&strct.name, "__operator_ne");
405 writeln!(out, " return {}(*this, rhs);", link_name);
406 }
David Tolnayb960ed22020-11-27 14:34:30 -0800407 writeln!(out, "}}");
408 }
409}
410
David Tolnay0b9b9f82020-11-01 20:41:00 -0800411fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800412 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800413 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800414 out.begin_block(Block::ExternC);
David Tolnaye1476af2020-11-01 13:47:25 -0800415 if let Some(annotation) = &out.opt.cxx_impl_annotations {
David Tolnaycc1ae762020-10-31 15:53:50 -0700416 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700417 }
David Tolnayebef4a22020-03-17 15:33:47 -0700418 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700419 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700420 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700421 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700422 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700423 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700424 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700425 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700426 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800427 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700428 write!(out, "const ");
429 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700430 write!(
431 out,
432 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800433 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700434 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700435 }
David Tolnay7db73692019-10-20 14:51:12 -0400436 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700437 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400438 write!(out, ", ");
439 }
David Tolnaya46a2372020-03-06 10:03:48 -0800440 if arg.ty == RustString {
441 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700442 } else if let Type::RustVec(_) = arg.ty {
443 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800444 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700445 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400446 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700447 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400448 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700449 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400450 write!(out, ", ");
451 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700452 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400453 write!(out, "*return$");
454 }
455 writeln!(out, ") noexcept {{");
456 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700457 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700458 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800459 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700460 Some(receiver) => write!(
461 out,
462 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700463 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800464 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700465 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700466 }
David Tolnay7db73692019-10-20 14:51:12 -0400467 for (i, arg) in efn.args.iter().enumerate() {
468 if i > 0 {
469 write!(out, ", ");
470 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700471 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400472 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700473 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700474 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800475 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700476 write!(out, " const");
477 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700478 }
479 write!(out, " = ");
480 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800481 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700482 Some(receiver) => write!(
483 out,
484 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700485 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800486 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700487 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700488 }
489 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400490 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700491 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700492 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700493 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700494 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700495 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700496 writeln!(out, " [&] {{");
497 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700498 }
David Tolnay7db73692019-10-20 14:51:12 -0400499 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700500 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400501 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700502 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400503 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700504 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400505 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700506 }
507 match &efn.ret {
508 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700509 Some(Type::Str(_)) if !indirect_return => {
510 out.builtin.rust_str_repr = true;
511 write!(out, "::rust::impl<::rust::Str>::repr(");
512 }
David Tolnay73b72642020-11-25 17:44:05 -0800513 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700514 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800515 write!(out, "::rust::impl<");
516 write_type(out, ty);
517 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700518 }
David Tolnay99642622020-03-25 13:07:35 -0700519 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400520 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700521 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800522 None => write!(out, "{}$(", efn.name.rust),
523 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700524 }
David Tolnay7db73692019-10-20 14:51:12 -0400525 for (i, arg) in efn.args.iter().enumerate() {
526 if i > 0 {
527 write!(out, ", ");
528 }
529 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700530 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400531 write!(out, "::from_raw({})", arg.ident);
532 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700533 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400534 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700535 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700536 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700537 write!(
538 out,
539 "::rust::impl<::rust::Str>::new_unchecked({})",
540 arg.ident,
541 );
David Tolnaya46a2372020-03-06 10:03:48 -0800542 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700543 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800544 write!(
545 out,
546 "::rust::String(::rust::unsafe_bitcopy, *{})",
547 arg.ident,
548 );
David Tolnay313b10e2020-04-25 16:30:51 -0700549 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700550 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700551 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700552 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800553 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800554 write_type(out, &arg.ty);
555 write!(out, "(static_cast<");
556 if slice.mutability.is_none() {
557 write!(out, "const ");
558 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800559 write_type_space(out, &slice.inner);
560 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700561 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700562 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800563 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400564 } else {
565 write!(out, "{}", arg.ident);
566 }
567 }
568 write!(out, ")");
569 match &efn.ret {
570 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
571 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800572 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400573 _ => {}
574 }
575 if indirect_return {
576 write!(out, ")");
577 }
578 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700579 if efn.throws {
580 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800581 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700582 writeln!(out, " throw$.ptr = nullptr;");
583 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700584 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700585 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700586 writeln!(
587 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800588 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700589 );
David Tolnay5d121442020-03-17 22:14:40 -0700590 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700591 writeln!(out, " return throw$;");
592 }
David Tolnay7db73692019-10-20 14:51:12 -0400593 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 for arg in &efn.args {
595 if let Type::Fn(f) = &arg.ty {
596 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700597 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 }
599 }
David Tolnayca563ee2020-11-01 20:12:27 -0800600 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700601}
602
603fn write_function_pointer_trampoline(
604 out: &mut OutFile,
605 efn: &ExternFn,
606 var: &Ident,
607 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700608) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700609 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700610 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700611 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700612
613 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700614 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
615 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400616}
617
David Tolnay0b9b9f82020-11-01 20:41:00 -0800618fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800619 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800620 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700621 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700622 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700623 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800624 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700625}
626
627fn write_rust_function_decl_impl(
628 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700629 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700630 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700631 indirect_call: bool,
632) {
David Tolnay04770742020-11-01 13:50:50 -0800633 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700634 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700635 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700636 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700637 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700638 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700639 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700640 write!(out, "{}(", link_name);
641 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700642 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800643 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700644 write!(out, "const ");
645 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700646 write!(
647 out,
648 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800649 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700650 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700651 needs_comma = true;
652 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 for arg in &sig.args {
654 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400655 write!(out, ", ");
656 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700657 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700658 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400659 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700660 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700661 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400662 write!(out, ", ");
663 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700664 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400665 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700666 needs_comma = true;
667 }
668 if indirect_call {
669 if needs_comma {
670 write!(out, ", ");
671 }
672 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400673 }
674 writeln!(out, ") noexcept;");
675}
676
David Tolnay0b9b9f82020-11-01 20:41:00 -0800677fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800678 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400679 for line in efn.doc.to_string().lines() {
680 writeln!(out, "//{}", line);
681 }
David Tolnaya73853b2020-04-20 01:19:56 -0700682 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800683 None => efn.name.cxx.to_string(),
684 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700685 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700686 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700687 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700688 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700689}
690
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700691fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700692 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700693 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700694 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700695 indirect_call: bool,
696) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700697 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700698 write!(out, "{}(", local_name);
699 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400700 if i > 0 {
701 write!(out, ", ");
702 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700703 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400704 write!(out, "{}", arg.ident);
705 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700706 if indirect_call {
707 if !sig.args.is_empty() {
708 write!(out, ", ");
709 }
710 write!(out, "void *extern$");
711 }
David Tolnay1e548172020-03-16 13:37:09 -0700712 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700713 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800714 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700715 write!(out, " const");
716 }
717 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700718 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700719 write!(out, " noexcept");
720 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700721}
722
723fn write_rust_function_shim_impl(
724 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700725 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700726 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700727 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700728 indirect_call: bool,
729) {
730 if out.header && sig.receiver.is_some() {
731 // We've already defined this inside the struct.
732 return;
733 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700734 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400735 if out.header {
736 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700737 return;
David Tolnay7db73692019-10-20 14:51:12 -0400738 }
David Tolnay439cde22020-04-20 00:46:25 -0700739 writeln!(out, " {{");
740 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700741 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700742 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700743 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700744 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700745 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700746 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
747 }
748 }
749 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700750 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700751 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700752 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700753 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700754 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700755 writeln!(out, "> return$;");
756 write!(out, " ");
757 } else if let Some(ret) = &sig.ret {
758 write!(out, "return ");
759 match ret {
760 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700761 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700762 write!(out, "::from_raw(");
763 }
764 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700765 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700766 write!(out, "(");
767 }
768 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700769 Type::Str(_) => {
770 out.builtin.rust_str_new_unchecked = true;
771 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
772 }
David Tolnay73b72642020-11-25 17:44:05 -0800773 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700774 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800775 write!(out, "::rust::impl<");
776 write_type(out, ret);
777 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700778 }
David Tolnay439cde22020-04-20 00:46:25 -0700779 _ => {}
780 }
781 }
782 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700783 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700784 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700785 }
786 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -0800787 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -0700788 if sig.receiver.is_some() {
789 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -0800790 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700791 }
David Tolnay9d840362020-11-10 08:50:40 -0800792 for arg in &sig.args {
793 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700794 write!(out, ", ");
795 }
796 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700797 Type::Str(_) => {
798 out.builtin.rust_str_repr = true;
799 write!(out, "::rust::impl<::rust::Str>::repr(");
800 }
David Tolnay73b72642020-11-25 17:44:05 -0800801 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700802 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800803 write!(out, "::rust::impl<");
804 write_type(out, &arg.ty);
805 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700806 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700807 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700808 _ => {}
809 }
810 write!(out, "{}", arg.ident);
811 match &arg.ty {
812 Type::RustBox(_) => write!(out, ".into_raw()"),
813 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800814 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700815 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700816 _ => {}
817 }
David Tolnay9d840362020-11-10 08:50:40 -0800818 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700819 }
820 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -0800821 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700822 write!(out, ", ");
823 }
824 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -0800825 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700826 }
827 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -0800828 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700829 write!(out, ", ");
830 }
831 write!(out, "extern$");
832 }
833 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400834 if !indirect_return {
835 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -0800836 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400837 write!(out, ")");
838 }
David Tolnay439cde22020-04-20 00:46:25 -0700839 }
840 }
841 writeln!(out, ";");
842 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -0700843 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700844 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700845 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700846 writeln!(out, " }}");
847 }
848 if indirect_return {
849 out.include.utility = true;
850 writeln!(out, " return ::std::move(return$.value);");
851 }
852 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400853}
854
David Tolnaya7c2ea12020-10-30 21:32:53 -0700855fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400856 match ty {
857 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700858 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400859 }
860}
861
David Tolnay75dca2e2020-03-25 20:17:52 -0700862fn indirect_return(sig: &Signature, types: &Types) -> bool {
863 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700864 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700865 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700866}
867
David Tolnaya7c2ea12020-10-30 21:32:53 -0700868fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700869 match ty {
870 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700871 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700872 write!(out, "*");
873 }
874 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800875 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -0700876 write!(out, "const ");
877 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700878 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700879 write!(out, " *");
880 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700881 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700882 }
883}
884
David Tolnaya7c2ea12020-10-30 21:32:53 -0700885fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
886 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700887 match ty {
888 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -0800889 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700890 _ => write_space_after_type(out, ty),
891 }
892}
893
David Tolnaya7c2ea12020-10-30 21:32:53 -0700894fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400895 match ty {
896 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700897 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400898 write!(out, "*");
899 }
David Tolnay4a441222020-01-25 16:24:27 -0800900 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800901 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -0800902 write!(out, "const ");
903 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700904 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -0800905 write!(out, " *");
906 }
David Tolnay73b72642020-11-25 17:44:05 -0800907 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -0700908 out.builtin.ptr_len = true;
909 write!(out, "::rust::repr::PtrLen ");
910 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700911 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
912 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400913 }
914}
915
David Tolnaya7c2ea12020-10-30 21:32:53 -0700916fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -0400917 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700918 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700919 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400920 write!(out, "*");
921 }
David Tolnay73b72642020-11-25 17:44:05 -0800922 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -0700923 out.builtin.ptr_len = true;
924 write!(out, "::rust::repr::PtrLen ");
925 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700926 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -0400927 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700928 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -0400929 write!(out, "*");
930 }
931 write!(out, "{}", arg.ident);
932}
933
David Tolnaya7c2ea12020-10-30 21:32:53 -0700934fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400935 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700936 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700937 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700938 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -0400939 },
940 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800941 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700942 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400943 write!(out, ">");
944 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700945 Type::RustVec(ty) => {
946 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700947 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +0700948 write!(out, ">");
949 }
David Tolnay7db73692019-10-20 14:51:12 -0400950 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800951 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700952 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400953 write!(out, ">");
954 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700955 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700956 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700957 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +0700958 write!(out, ">");
959 }
David Tolnay7db73692019-10-20 14:51:12 -0400960 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800961 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -0400962 write!(out, "const ");
963 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700964 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400965 write!(out, " &");
966 }
967 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800968 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400969 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800970 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -0800971 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -0800972 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -0800973 write!(out, "const ");
974 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800975 write_type(out, &slice.inner);
976 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700977 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700978 Type::Fn(f) => {
979 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
980 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700981 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -0700982 None => write!(out, "void"),
983 }
984 write!(out, "(");
985 for (i, arg) in f.args.iter().enumerate() {
986 if i > 0 {
987 write!(out, ", ");
988 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700989 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -0700990 }
991 write!(out, ")>");
992 }
Xiangpeng Hao78762352020-11-12 10:24:18 +0800993 Type::Array(a) => {
994 write!(out, "::std::array<");
995 write_type(out, &a.inner);
996 write!(out, ", {}>", &a.len);
997 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700998 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400999 }
1000}
1001
David Tolnayf6a89f22020-05-10 23:39:27 -07001002fn write_atom(out: &mut OutFile, atom: Atom) {
1003 match atom {
1004 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001005 Char => write!(out, "char"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001006 U8 => write!(out, "uint8_t"),
1007 U16 => write!(out, "uint16_t"),
1008 U32 => write!(out, "uint32_t"),
1009 U64 => write!(out, "uint64_t"),
1010 Usize => write!(out, "size_t"),
1011 I8 => write!(out, "int8_t"),
1012 I16 => write!(out, "int16_t"),
1013 I32 => write!(out, "int32_t"),
1014 I64 => write!(out, "int64_t"),
1015 Isize => write!(out, "::rust::isize"),
1016 F32 => write!(out, "float"),
1017 F64 => write!(out, "double"),
1018 CxxString => write!(out, "::std::string"),
1019 RustString => write!(out, "::rust::String"),
1020 }
1021}
1022
David Tolnaya7c2ea12020-10-30 21:32:53 -07001023fn write_type_space(out: &mut OutFile, ty: &Type) {
1024 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001025 write_space_after_type(out, ty);
1026}
1027
1028fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001029 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001030 Type::Ident(_)
1031 | Type::RustBox(_)
1032 | Type::UniquePtr(_)
1033 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001034 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001035 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001036 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001037 | Type::Fn(_)
1038 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001039 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001040 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001041 }
1042}
1043
David Tolnay1bdb4712020-11-25 07:27:54 -08001044#[derive(Copy, Clone)]
1045enum UniquePtr<'a> {
1046 Ident(&'a ResolvableName),
1047 CxxVector(&'a ResolvableName),
1048}
1049
1050trait ToTypename {
1051 fn to_typename(&self, types: &Types) -> String;
1052}
1053
1054impl ToTypename for ResolvableName {
1055 fn to_typename(&self, types: &Types) -> String {
1056 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001057 }
1058}
1059
David Tolnay1bdb4712020-11-25 07:27:54 -08001060impl<'a> ToTypename for UniquePtr<'a> {
1061 fn to_typename(&self, types: &Types) -> String {
1062 match self {
1063 UniquePtr::Ident(ident) => ident.to_typename(types),
1064 UniquePtr::CxxVector(element) => {
1065 format!("::std::vector<{}>", element.to_typename(types))
1066 }
1067 }
1068 }
1069}
1070
1071trait ToMangled {
1072 fn to_mangled(&self, types: &Types) -> Symbol;
1073}
1074
1075impl ToMangled for ResolvableName {
1076 fn to_mangled(&self, types: &Types) -> Symbol {
1077 self.to_symbol(types)
1078 }
1079}
1080
1081impl<'a> ToMangled for UniquePtr<'a> {
1082 fn to_mangled(&self, types: &Types) -> Symbol {
1083 match self {
1084 UniquePtr::Ident(ident) => ident.to_mangled(types),
1085 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1086 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001087 }
1088}
1089
David Tolnaya7c2ea12020-10-30 21:32:53 -07001090fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001091 if out.header {
1092 return;
1093 }
1094
1095 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001096 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001097 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001098 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001099 if let Type::RustBox(ty) = ty {
1100 if let Type::Ident(inner) = &ty.inner {
1101 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001102 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001103 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001104 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001105 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001106 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001107 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001108 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001109 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001110 }
David Tolnay7db73692019-10-20 14:51:12 -04001111 } else if let Type::UniquePtr(ptr) = ty {
1112 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001113 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001114 && (!out.types.aliases.contains_key(&inner.rust)
1115 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001116 {
David Tolnay7db73692019-10-20 14:51:12 -04001117 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001118 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001119 }
1120 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001121 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001122 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001123 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001124 && (!out.types.aliases.contains_key(&inner.rust)
1125 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001126 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001127 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001128 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001129 }
1130 }
1131 }
1132 }
David Tolnay0c033e32020-11-01 15:15:48 -08001133 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001134
David Tolnay0c033e32020-11-01 15:15:48 -08001135 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001136 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001137 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001138 if let Type::RustBox(ty) = ty {
1139 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001140 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001141 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001142 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001143 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001144 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001145 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001146 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001147 }
David Tolnay7db73692019-10-20 14:51:12 -04001148 }
1149 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001150 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001151 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001152}
1153
David Tolnay8faec772020-11-02 00:18:19 -08001154fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001155 let inner = ident.to_fully_qualified();
1156 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001157
David Tolnay0f0162f2020-11-16 23:43:37 -08001158 writeln!(out, "#ifndef CXXBRIDGE1_RUST_BOX_{}", instance);
1159 writeln!(out, "#define CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001160 writeln!(
1161 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001162 "void cxxbridge1$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001163 instance, inner,
1164 );
1165 writeln!(
1166 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001167 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001168 instance, inner,
1169 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001170 writeln!(out, "#endif // CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001171}
1172
David Tolnaya7c2ea12020-10-30 21:32:53 -07001173fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001174 let inner = element.to_typename(out.types);
1175 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001176
David Tolnay0f0162f2020-11-16 23:43:37 -08001177 writeln!(out, "#ifndef CXXBRIDGE1_RUST_VEC_{}", instance);
1178 writeln!(out, "#define CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001179 writeln!(
1180 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001181 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001182 instance, inner,
1183 );
1184 writeln!(
1185 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001186 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001187 instance, inner,
1188 );
1189 writeln!(
1190 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001191 "size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001192 instance, inner,
1193 );
David Tolnay219c0792020-04-24 20:31:37 -07001194 writeln!(
1195 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001196 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001197 inner, instance,
1198 );
David Tolnay503d0192020-04-24 22:18:56 -07001199 writeln!(
1200 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001201 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001202 instance, inner,
1203 );
1204 writeln!(
1205 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001206 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001207 instance, inner,
1208 );
1209 writeln!(
1210 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001211 "size_t cxxbridge1$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001212 instance,
1213 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001214 writeln!(out, "#endif // CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001215}
1216
David Tolnay8faec772020-11-02 00:18:19 -08001217fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001218 let inner = ident.to_fully_qualified();
1219 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001220
1221 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001222 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001223 writeln!(out, " cxxbridge1$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001224 writeln!(out, "}}");
1225
1226 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001227 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001228 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001229 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001230}
1231
David Tolnaya7c2ea12020-10-30 21:32:53 -07001232fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001233 let inner = element.to_typename(out.types);
1234 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001235
Myron Ahneba35cf2020-02-05 19:41:51 +07001236 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001237 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001238 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001239 writeln!(out, "}}");
1240
1241 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001242 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001243 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001244 writeln!(out, "}}");
1245
1246 writeln!(out, "template <>");
1247 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001248 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001249 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001250
1251 writeln!(out, "template <>");
1252 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001253 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001254 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001255
1256 writeln!(out, "template <>");
David Tolnayfb6b73c2020-11-10 14:32:16 -08001257 writeln!(
1258 out,
1259 "void Vec<{}>::reserve_total(size_t cap) noexcept {{",
1260 inner,
1261 );
1262 writeln!(
1263 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001264 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001265 instance,
1266 );
1267 writeln!(out, "}}");
1268
1269 writeln!(out, "template <>");
1270 writeln!(out, "void Vec<{}>::set_len(size_t len) noexcept {{", inner);
1271 writeln!(
1272 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001273 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001274 instance,
1275 );
1276 writeln!(out, "}}");
1277
1278 writeln!(out, "template <>");
David Tolnay503d0192020-04-24 22:18:56 -07001279 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001280 writeln!(out, " return cxxbridge1$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001281 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001282}
1283
David Tolnaya7c2ea12020-10-30 21:32:53 -07001284fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001285 let ty = UniquePtr::Ident(ident);
1286 let instance = ty.to_mangled(out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001287
David Tolnay0f0162f2020-11-16 23:43:37 -08001288 writeln!(out, "#ifndef CXXBRIDGE1_UNIQUE_PTR_{}", instance);
1289 writeln!(out, "#define CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001290
David Tolnay1bdb4712020-11-25 07:27:54 -08001291 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001292
David Tolnay0f0162f2020-11-16 23:43:37 -08001293 writeln!(out, "#endif // CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001294}
1295
1296// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001297fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001298 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001299 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001300 let inner = ty.to_typename(out.types);
1301 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001302
David Tolnay63da4d32020-04-25 09:41:12 -07001303 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001304 // Some aliases are to opaque types; some are to trivial types. We can't
1305 // know at code generation time, so we generate both C++ and Rust side
1306 // bindings for a "new" method anyway. But the Rust code can't be called
1307 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001308 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001309 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001310 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001311 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001312 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001313 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001314 };
1315
David Tolnay53462762020-11-25 07:08:10 -08001316 let conditional_delete = match ty {
1317 UniquePtr::Ident(ident) => {
1318 !out.types.structs.contains_key(&ident.rust)
1319 && !out.types.enums.contains_key(&ident.rust)
1320 }
1321 UniquePtr::CxxVector(_) => false,
1322 };
1323
1324 if conditional_delete {
1325 out.builtin.is_complete = true;
1326 let definition = match ty {
1327 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1328 UniquePtr::CxxVector(_) => unreachable!(),
1329 };
1330 writeln!(
1331 out,
1332 "static_assert(::rust::is_complete<{}>::value, \"definition of {} is required\");",
1333 inner, definition,
1334 );
1335 }
David Tolnay7db73692019-10-20 14:51:12 -04001336 writeln!(
1337 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001338 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001339 inner,
1340 );
1341 writeln!(
1342 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001343 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001344 inner,
1345 );
1346 writeln!(
1347 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001348 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001349 instance, inner,
1350 );
David Tolnay7e219b82020-03-01 13:14:51 -08001351 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001352 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001353 if can_construct_from_value {
1354 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001355 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001356 "void cxxbridge1$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001357 instance, inner, inner,
1358 );
David Tolnay63da4d32020-04-25 09:41:12 -07001359 writeln!(
1360 out,
1361 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1362 inner, inner,
1363 );
1364 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001365 }
David Tolnay7db73692019-10-20 14:51:12 -04001366 writeln!(
1367 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001368 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001369 instance, inner, inner,
1370 );
David Tolnay7e219b82020-03-01 13:14:51 -08001371 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001372 writeln!(out, "}}");
1373 writeln!(
1374 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001375 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001376 inner, instance, inner,
1377 );
1378 writeln!(out, " return ptr.get();");
1379 writeln!(out, "}}");
1380 writeln!(
1381 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001382 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001383 inner, instance, inner,
1384 );
1385 writeln!(out, " return ptr.release();");
1386 writeln!(out, "}}");
1387 writeln!(
1388 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001389 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001390 instance, inner,
1391 );
David Tolnay53462762020-11-25 07:08:10 -08001392 if conditional_delete {
1393 out.builtin.deleter_if = true;
1394 writeln!(
1395 out,
1396 " ::rust::deleter_if<::rust::is_complete<{}>::value>{{}}(ptr);",
1397 inner,
1398 );
1399 } else {
1400 writeln!(out, " ptr->~unique_ptr();");
1401 }
David Tolnay7db73692019-10-20 14:51:12 -04001402 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001403}
Myron Ahneba35cf2020-02-05 19:41:51 +07001404
David Tolnay1bdb4712020-11-25 07:27:54 -08001405fn write_cxx_vector(out: &mut OutFile, element: &ResolvableName) {
1406 let inner = element.to_typename(out.types);
1407 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001408
David Tolnay0f0162f2020-11-16 23:43:37 -08001409 writeln!(out, "#ifndef CXXBRIDGE1_VECTOR_{}", instance);
1410 writeln!(out, "#define CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001411 writeln!(
1412 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001413 "size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001414 instance, inner,
1415 );
1416 writeln!(out, " return s.size();");
1417 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001418 writeln!(
1419 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001420 "const {} *cxxbridge1$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001421 inner, instance, inner,
1422 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001423 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001424 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001425
David Tolnay1bdb4712020-11-25 07:27:54 -08001426 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
David Tolnay63da4d32020-04-25 09:41:12 -07001427
David Tolnay0f0162f2020-11-16 23:43:37 -08001428 writeln!(out, "#endif // CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001429}