blob: f28a58e0b365463b2b6860f354e0c106fd808b23 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Joel Galensonc03402a2020-04-23 17:31:09 -07006use crate::syntax::{mangle, Api, Enum, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -07008use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04009
David Tolnay33d30292020-03-18 18:02:02 -070010pub(super) fn gen(
David Tolnay2ec14632020-05-04 00:47:10 -070011 namespace: &Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070012 apis: &[Api],
13 types: &Types,
14 opt: Opt,
15 header: bool,
16) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040017 let mut out_file = OutFile::new(namespace.clone(), header);
18 let out = &mut out_file;
19
20 if header {
21 writeln!(out, "#pragma once");
22 }
23
David Tolnay33d30292020-03-18 18:02:02 -070024 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080027 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070035 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040036 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070045 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040046 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
65 write_struct(out, strct);
66 }
Joel Galensonc03402a2020-04-23 17:31:09 -070067 Api::Enum(enm) => {
68 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070069 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070070 check_enum(out, enm);
71 } else {
72 write_enum(out, enm);
73 }
Joel Galensonc03402a2020-04-23 17:31:09 -070074 }
David Tolnayc1fe0052020-04-17 15:15:06 -070075 Api::RustType(ety) => {
76 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070077 out.next_section();
78 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070079 }
David Tolnayc1fe0052020-04-17 15:15:06 -070080 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070081 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040082 }
83 }
84
85 if !header {
86 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070087 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040088 for api in apis {
89 let (efn, write): (_, fn(_, _, _)) = match api {
90 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
91 Api::RustFunction(efn) => (efn, write_rust_function_decl),
92 _ => continue,
93 };
94 out.next_section();
95 write(out, efn, types);
96 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080097 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040098 }
99
100 for api in apis {
101 if let Api::RustFunction(efn) = api {
102 out.next_section();
103 write_rust_function_shim(out, efn, types);
104 }
105 }
106
107 out.next_section();
108 for name in namespace.iter().rev() {
109 writeln!(out, "}} // namespace {}", name);
110 }
111
112 if !header {
113 out.next_section();
114 write_generic_instantiations(out, types);
115 }
116
David Tolnay9c68b1a2020-03-06 11:12:55 -0800117 out.prepend(out.include.to_string());
118
David Tolnay7db73692019-10-20 14:51:12 -0400119 out_file
120}
121
122fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400123 for ty in types {
124 match ty {
125 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700126 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
127 | Some(I64) => out.include.cstdint = true,
128 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800129 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700130 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400131 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800132 Type::RustBox(_) => out.include.type_traits = true,
133 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700134 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700135 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700136 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400137 }
138 }
David Tolnay7db73692019-10-20 14:51:12 -0400139}
140
David Tolnayf51447e2020-03-06 14:14:27 -0800141fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 let mut needs_rust_string = false;
143 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700144 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400145 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700146 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700147 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700148 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400149 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700150 match ty {
151 Type::RustBox(_) => {
152 out.include.type_traits = true;
153 needs_rust_box = true;
154 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700155 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700156 out.include.array = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700157 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700158 needs_rust_vec = true;
159 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700160 Type::Str(_) => {
161 out.include.cstdint = true;
162 out.include.string = true;
163 needs_rust_str = true;
164 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700165 Type::Fn(_) => {
166 needs_rust_fn = true;
167 }
David Tolnay4770b472020-04-14 16:32:59 -0700168 Type::Slice(_) | Type::SliceRefU8(_) => {
169 needs_rust_slice = true;
170 }
David Tolnay7c295462020-04-25 12:45:07 -0700171 ty if ty == Isize => {
172 out.include.base_tsd = true;
173 needs_rust_isize = true;
174 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700175 ty if ty == RustString => {
176 out.include.array = true;
177 out.include.cstdint = true;
178 out.include.string = true;
179 needs_rust_string = true;
180 }
181 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400182 }
183 }
184
David Tolnayb7a7cb62020-03-17 21:18:40 -0700185 let mut needs_rust_error = false;
186 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800187 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800188 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700189 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800190 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 match api {
192 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700193 if efn.throws {
194 needs_trycatch = true;
195 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700196 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700197 let bitcopy = match arg.ty {
198 Type::RustVec(_) => true,
199 _ => arg.ty == RustString,
200 };
201 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700202 needs_unsafe_bitcopy = true;
203 break;
204 }
David Tolnay09011c32020-03-06 14:40:28 -0800205 }
206 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 Api::RustFunction(efn) if !out.header => {
208 if efn.throws {
209 out.include.exception = true;
210 needs_rust_error = true;
211 }
212 for arg in &efn.args {
213 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
214 needs_manually_drop = true;
215 break;
216 }
217 }
218 if let Some(ret) = &efn.ret {
219 if types.needs_indirect_abi(ret) {
220 needs_maybe_uninit = true;
221 }
David Tolnayf51447e2020-03-06 14:14:27 -0800222 }
223 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700224 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800225 }
226 }
227
David Tolnay750755e2020-03-01 13:04:08 -0800228 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700229 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800230
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 if needs_rust_string
232 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700233 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700234 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700235 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700236 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700238 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 || needs_unsafe_bitcopy
240 || needs_manually_drop
241 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700242 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 {
David Tolnay736cbca2020-03-11 16:49:18 -0700244 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800245 }
246
David Tolnayd1402742020-03-25 22:21:42 -0700247 if needs_rust_string {
248 out.next_section();
249 writeln!(out, "struct unsafe_bitcopy_t;");
250 }
251
David Tolnay69601622020-04-29 18:48:36 -0700252 write_header_section(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
253 write_header_section(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
254 write_header_section(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
255 write_header_section(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
256 write_header_section(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
257 write_header_section(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
258 write_header_section(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
259 write_header_section(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
260 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800261
262 if needs_manually_drop {
263 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700264 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800265 writeln!(out, "template <typename T>");
266 writeln!(out, "union ManuallyDrop {{");
267 writeln!(out, " T value;");
268 writeln!(
269 out,
270 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
271 );
272 writeln!(out, " ~ManuallyDrop() {{}}");
273 writeln!(out, "}};");
274 }
275
David Tolnay09011c32020-03-06 14:40:28 -0800276 if needs_maybe_uninit {
277 out.next_section();
278 writeln!(out, "template <typename T>");
279 writeln!(out, "union MaybeUninit {{");
280 writeln!(out, " T value;");
281 writeln!(out, " MaybeUninit() {{}}");
282 writeln!(out, " ~MaybeUninit() {{}}");
283 writeln!(out, "}};");
284 }
285
David Tolnay69601622020-04-29 18:48:36 -0700286 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700287
David Tolnay5d121442020-03-17 22:14:40 -0700288 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700289 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700290 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700291 out.include.type_traits = true;
292 out.include.utility = true;
293 writeln!(out, "class missing {{}};");
294 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700295 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700296 writeln!(out, "template <typename Try, typename Fail>");
297 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700298 writeln!(
299 out,
David Tolnay04722332020-03-18 11:31:54 -0700300 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700301 );
David Tolnay04722332020-03-18 11:31:54 -0700302 writeln!(out, " missing>::value>::type");
303 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700304 writeln!(out, " func();");
305 writeln!(out, "}} catch (const ::std::exception &e) {{");
306 writeln!(out, " fail(e.what());");
307 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700308 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700309 }
310
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800311 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400312}
313
David Tolnayb7a7cb62020-03-17 21:18:40 -0700314fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700315 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700316 if needed {
317 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700318 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700319 if !line.trim_start().starts_with("//") {
320 writeln!(out, "{}", line);
321 }
322 }
323 }
324}
325
David Tolnay7db73692019-10-20 14:51:12 -0400326fn write_struct(out: &mut OutFile, strct: &Struct) {
327 for line in strct.doc.to_string().lines() {
328 writeln!(out, "//{}", line);
329 }
330 writeln!(out, "struct {} final {{", strct.ident);
331 for field in &strct.fields {
332 write!(out, " ");
333 write_type_space(out, &field.ty);
334 writeln!(out, "{};", field.ident);
335 }
336 writeln!(out, "}};");
337}
338
339fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
340 writeln!(out, "struct {};", ident);
341}
342
David Tolnay8861bee2020-01-20 18:39:24 -0800343fn write_struct_using(out: &mut OutFile, ident: &Ident) {
344 writeln!(out, "using {} = {};", ident, ident);
345}
346
David Tolnayc1fe0052020-04-17 15:15:06 -0700347fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700348 for line in ety.doc.to_string().lines() {
349 writeln!(out, "//{}", line);
350 }
351 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700352 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700353 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700354 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700355 write!(out, " ");
356 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700357 let local_name = method.ident.to_string();
358 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700359 writeln!(out, ";");
360 }
361 writeln!(out, "}};");
362}
363
Joel Galensonc03402a2020-04-23 17:31:09 -0700364fn write_enum(out: &mut OutFile, enm: &Enum) {
365 for line in enm.doc.to_string().lines() {
366 writeln!(out, "//{}", line);
367 }
368 writeln!(out, "enum class {} : uint32_t {{", enm.ident);
369 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700370 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700371 }
372 writeln!(out, "}};");
373}
374
Joel Galenson905eb2e2020-05-04 14:58:14 -0700375fn check_enum(out: &mut OutFile, enm: &Enum) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700376 writeln!(
377 out,
Joel Galenson9fe5ea32020-05-04 20:25:15 -0700378 "static_assert(sizeof({}) == sizeof(uint32_t), \"incorrect size\");",
Joel Galenson905eb2e2020-05-04 14:58:14 -0700379 enm.ident
380 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700381 for variant in &enm.variants {
Joel Galenson0f654ff2020-05-04 20:04:21 -0700382 writeln!(
383 out,
384 "static_assert(static_cast<uint32_t>({}::{}) == {},
385 \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700386 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700387 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700388 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700389}
390
David Tolnayebef4a22020-03-17 15:33:47 -0700391fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
392 let mut has_cxx_throws = false;
393 for api in apis {
394 if let Api::CxxFunction(efn) = api {
395 if efn.throws {
396 has_cxx_throws = true;
397 break;
398 }
399 }
400 }
401
402 if has_cxx_throws {
403 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700404 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700405 out,
David Tolnay69601622020-04-29 18:48:36 -0700406 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700407 );
408 }
409}
410
David Tolnay7db73692019-10-20 14:51:12 -0400411fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700412 if efn.throws {
413 write!(out, "::rust::Str::Repr ");
414 } else {
David Tolnay99642622020-03-25 13:07:35 -0700415 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700416 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700417 let mangled = mangle::extern_fn(&out.namespace, efn);
418 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700419 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700420 if receiver.mutability.is_none() {
421 write!(out, "const ");
422 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700423 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700424 }
David Tolnay7db73692019-10-20 14:51:12 -0400425 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700426 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400427 write!(out, ", ");
428 }
David Tolnaya46a2372020-03-06 10:03:48 -0800429 if arg.ty == RustString {
430 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700431 } else if let Type::RustVec(_) = arg.ty {
432 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800433 }
David Tolnay7db73692019-10-20 14:51:12 -0400434 write_extern_arg(out, arg, types);
435 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700436 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400437 if indirect_return {
438 if !efn.args.is_empty() {
439 write!(out, ", ");
440 }
David Tolnay99642622020-03-25 13:07:35 -0700441 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400442 write!(out, "*return$");
443 }
444 writeln!(out, ") noexcept {{");
445 write!(out, " ");
446 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700447 match &efn.receiver {
448 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700449 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700450 }
David Tolnay7db73692019-10-20 14:51:12 -0400451 for (i, arg) in efn.args.iter().enumerate() {
452 if i > 0 {
453 write!(out, ", ");
454 }
455 write_type(out, &arg.ty);
456 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700457 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700458 if let Some(receiver) = &efn.receiver {
459 if receiver.mutability.is_none() {
460 write!(out, " const");
461 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700462 }
463 write!(out, " = ");
464 match &efn.receiver {
465 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700466 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700467 }
468 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400469 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700470 if efn.throws {
471 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700472 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700473 writeln!(out, " [&] {{");
474 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700475 }
David Tolnay7db73692019-10-20 14:51:12 -0400476 if indirect_return {
477 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700478 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400479 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700480 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400481 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700482 }
483 match &efn.ret {
484 Some(Type::Ref(_)) => write!(out, "&"),
485 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700486 Some(Type::SliceRefU8(_)) if !indirect_return => {
487 write!(out, "::rust::Slice<uint8_t>::Repr(")
488 }
David Tolnay99642622020-03-25 13:07:35 -0700489 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400490 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700491 match &efn.receiver {
492 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700493 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700494 }
David Tolnay7db73692019-10-20 14:51:12 -0400495 for (i, arg) in efn.args.iter().enumerate() {
496 if i > 0 {
497 write!(out, ", ");
498 }
499 if let Type::RustBox(_) = &arg.ty {
500 write_type(out, &arg.ty);
501 write!(out, "::from_raw({})", arg.ident);
502 } else if let Type::UniquePtr(_) = &arg.ty {
503 write_type(out, &arg.ty);
504 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800505 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800506 write!(
507 out,
508 "::rust::String(::rust::unsafe_bitcopy, *{})",
509 arg.ident,
510 );
David Tolnay313b10e2020-04-25 16:30:51 -0700511 } else if let Type::RustVec(_) = arg.ty {
512 write_type(out, &arg.ty);
513 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400514 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700515 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800516 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400517 } else {
518 write!(out, "{}", arg.ident);
519 }
520 }
521 write!(out, ")");
522 match &efn.ret {
523 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
524 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700525 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400526 _ => {}
527 }
528 if indirect_return {
529 write!(out, ")");
530 }
531 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700532 if efn.throws {
533 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700534 writeln!(out, " throw$.ptr = nullptr;");
535 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700536 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700537 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700538 writeln!(
539 out,
David Tolnay69601622020-04-29 18:48:36 -0700540 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700541 );
David Tolnay5d121442020-03-17 22:14:40 -0700542 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700543 writeln!(out, " return throw$;");
544 }
David Tolnay7db73692019-10-20 14:51:12 -0400545 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700546 for arg in &efn.args {
547 if let Type::Fn(f) = &arg.ty {
548 let var = &arg.ident;
549 write_function_pointer_trampoline(out, efn, var, f, types);
550 }
551 }
552}
553
554fn write_function_pointer_trampoline(
555 out: &mut OutFile,
556 efn: &ExternFn,
557 var: &Ident,
558 f: &Signature,
559 types: &Types,
560) {
561 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700562 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700563 let indirect_call = true;
564 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
565
566 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700567 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700568 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400569}
570
571fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700572 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700573 let indirect_call = false;
574 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
575}
576
577fn write_rust_function_decl_impl(
578 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700579 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700580 sig: &Signature,
581 types: &Types,
582 indirect_call: bool,
583) {
584 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700585 write!(out, "::rust::Str::Repr ");
586 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700587 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700588 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700589 write!(out, "{}(", link_name);
590 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700591 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700592 if receiver.mutability.is_none() {
593 write!(out, "const ");
594 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700595 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700596 needs_comma = true;
597 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 for arg in &sig.args {
599 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400600 write!(out, ", ");
601 }
602 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400604 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700605 if indirect_return(sig, types) {
606 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400607 write!(out, ", ");
608 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700609 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400610 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700611 needs_comma = true;
612 }
613 if indirect_call {
614 if needs_comma {
615 write!(out, ", ");
616 }
617 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400618 }
619 writeln!(out, ") noexcept;");
620}
621
622fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400623 for line in efn.doc.to_string().lines() {
624 writeln!(out, "//{}", line);
625 }
David Tolnaya73853b2020-04-20 01:19:56 -0700626 let local_name = match &efn.sig.receiver {
627 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700628 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700629 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700630 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700631 let indirect_call = false;
632 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
633}
634
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700635fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700636 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700637 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700638 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700639 indirect_call: bool,
640) {
641 write_return_type(out, &sig.ret);
642 write!(out, "{}(", local_name);
643 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400644 if i > 0 {
645 write!(out, ", ");
646 }
647 write_type_space(out, &arg.ty);
648 write!(out, "{}", arg.ident);
649 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700650 if indirect_call {
651 if !sig.args.is_empty() {
652 write!(out, ", ");
653 }
654 write!(out, "void *extern$");
655 }
David Tolnay1e548172020-03-16 13:37:09 -0700656 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700657 if let Some(receiver) = &sig.receiver {
658 if receiver.mutability.is_none() {
659 write!(out, " const");
660 }
661 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700662 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700663 write!(out, " noexcept");
664 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700665}
666
667fn write_rust_function_shim_impl(
668 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700669 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700670 sig: &Signature,
671 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700672 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700673 indirect_call: bool,
674) {
675 if out.header && sig.receiver.is_some() {
676 // We've already defined this inside the struct.
677 return;
678 }
David Tolnaya73853b2020-04-20 01:19:56 -0700679 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400680 if out.header {
681 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700682 return;
David Tolnay7db73692019-10-20 14:51:12 -0400683 }
David Tolnay439cde22020-04-20 00:46:25 -0700684 writeln!(out, " {{");
685 for arg in &sig.args {
686 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
687 out.include.utility = true;
688 write!(out, " ::rust::ManuallyDrop<");
689 write_type(out, &arg.ty);
690 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
691 }
692 }
693 write!(out, " ");
694 let indirect_return = indirect_return(sig, types);
695 if indirect_return {
696 write!(out, "::rust::MaybeUninit<");
697 write_type(out, sig.ret.as_ref().unwrap());
698 writeln!(out, "> return$;");
699 write!(out, " ");
700 } else if let Some(ret) = &sig.ret {
701 write!(out, "return ");
702 match ret {
703 Type::RustBox(_) => {
704 write_type(out, ret);
705 write!(out, "::from_raw(");
706 }
707 Type::UniquePtr(_) => {
708 write_type(out, ret);
709 write!(out, "(");
710 }
711 Type::Ref(_) => write!(out, "*"),
712 _ => {}
713 }
714 }
715 if sig.throws {
716 write!(out, "::rust::Str::Repr error$ = ");
717 }
718 write!(out, "{}(", invoke);
719 if sig.receiver.is_some() {
720 write!(out, "*this");
721 }
722 for (i, arg) in sig.args.iter().enumerate() {
723 if i > 0 || sig.receiver.is_some() {
724 write!(out, ", ");
725 }
726 match &arg.ty {
727 Type::Str(_) => write!(out, "::rust::Str::Repr("),
728 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
729 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
730 _ => {}
731 }
732 write!(out, "{}", arg.ident);
733 match &arg.ty {
734 Type::RustBox(_) => write!(out, ".into_raw()"),
735 Type::UniquePtr(_) => write!(out, ".release()"),
736 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
737 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
738 _ => {}
739 }
740 }
741 if indirect_return {
742 if !sig.args.is_empty() {
743 write!(out, ", ");
744 }
745 write!(out, "&return$.value");
746 }
747 if indirect_call {
748 if !sig.args.is_empty() || indirect_return {
749 write!(out, ", ");
750 }
751 write!(out, "extern$");
752 }
753 write!(out, ")");
754 if let Some(ret) = &sig.ret {
755 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
756 write!(out, ")");
757 }
758 }
759 writeln!(out, ";");
760 if sig.throws {
761 writeln!(out, " if (error$.ptr) {{");
762 writeln!(out, " throw ::rust::Error(error$);");
763 writeln!(out, " }}");
764 }
765 if indirect_return {
766 out.include.utility = true;
767 writeln!(out, " return ::std::move(return$.value);");
768 }
769 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400770}
771
772fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
773 match ty {
774 None => write!(out, "void "),
775 Some(ty) => write_type_space(out, ty),
776 }
777}
778
David Tolnay75dca2e2020-03-25 20:17:52 -0700779fn indirect_return(sig: &Signature, types: &Types) -> bool {
780 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700781 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700782 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700783}
784
David Tolnay99642622020-03-25 13:07:35 -0700785fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
786 match ty {
787 Type::RustBox(ty) | Type::UniquePtr(ty) => {
788 write_type_space(out, &ty.inner);
789 write!(out, "*");
790 }
791 Type::Ref(ty) => {
792 if ty.mutability.is_none() {
793 write!(out, "const ");
794 }
795 write_type(out, &ty.inner);
796 write!(out, " *");
797 }
798 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700799 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700800 _ => write_type(out, ty),
801 }
802}
803
804fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
805 write_indirect_return_type(out, ty);
806 match ty {
807 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700808 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700809 _ => write_space_after_type(out, ty),
810 }
811}
812
813fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400814 match ty {
815 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
816 write_type_space(out, &ty.inner);
817 write!(out, "*");
818 }
David Tolnay4a441222020-01-25 16:24:27 -0800819 Some(Type::Ref(ty)) => {
820 if ty.mutability.is_none() {
821 write!(out, "const ");
822 }
823 write_type(out, &ty.inner);
824 write!(out, " *");
825 }
David Tolnay750755e2020-03-01 13:04:08 -0800826 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700827 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400828 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
829 _ => write_return_type(out, ty),
830 }
831}
832
833fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
834 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700835 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400836 write_type_space(out, &ty.inner);
837 write!(out, "*");
838 }
David Tolnay750755e2020-03-01 13:04:08 -0800839 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700840 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400841 _ => write_type_space(out, &arg.ty),
842 }
843 if types.needs_indirect_abi(&arg.ty) {
844 write!(out, "*");
845 }
846 write!(out, "{}", arg.ident);
847}
848
849fn write_type(out: &mut OutFile, ty: &Type) {
850 match ty {
851 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay029f1d62020-04-25 10:19:25 -0700852 Some(Bool) => write!(out, "bool"),
853 Some(U8) => write!(out, "uint8_t"),
854 Some(U16) => write!(out, "uint16_t"),
855 Some(U32) => write!(out, "uint32_t"),
856 Some(U64) => write!(out, "uint64_t"),
857 Some(Usize) => write!(out, "size_t"),
858 Some(I8) => write!(out, "int8_t"),
859 Some(I16) => write!(out, "int16_t"),
860 Some(I32) => write!(out, "int32_t"),
861 Some(I64) => write!(out, "int64_t"),
862 Some(Isize) => write!(out, "::rust::isize"),
863 Some(F32) => write!(out, "float"),
864 Some(F64) => write!(out, "double"),
865 Some(CxxString) => write!(out, "::std::string"),
866 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400867 None => write!(out, "{}", ident),
868 },
869 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800870 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400871 write_type(out, &ty.inner);
872 write!(out, ">");
873 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700874 Type::RustVec(ty) => {
875 write!(out, "::rust::Vec<");
876 write_type(out, &ty.inner);
877 write!(out, ">");
878 }
David Tolnay7db73692019-10-20 14:51:12 -0400879 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800880 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400881 write_type(out, &ptr.inner);
882 write!(out, ">");
883 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700884 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700885 write!(out, "::std::vector<");
886 write_type(out, &ty.inner);
887 write!(out, ">");
888 }
David Tolnay7db73692019-10-20 14:51:12 -0400889 Type::Ref(r) => {
890 if r.mutability.is_none() {
891 write!(out, "const ");
892 }
893 write_type(out, &r.inner);
894 write!(out, " &");
895 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700896 Type::Slice(_) => {
897 // For now, only U8 slices are supported, which are covered separately below
898 unreachable!()
899 }
David Tolnay7db73692019-10-20 14:51:12 -0400900 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800901 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400902 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700903 Type::SliceRefU8(_) => {
904 write!(out, "::rust::Slice<uint8_t>");
905 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700906 Type::Fn(f) => {
907 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
908 match &f.ret {
909 Some(ret) => write_type(out, ret),
910 None => write!(out, "void"),
911 }
912 write!(out, "(");
913 for (i, arg) in f.args.iter().enumerate() {
914 if i > 0 {
915 write!(out, ", ");
916 }
917 write_type(out, &arg.ty);
918 }
919 write!(out, ")>");
920 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700921 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400922 }
923}
924
925fn write_type_space(out: &mut OutFile, ty: &Type) {
926 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700927 write_space_after_type(out, ty);
928}
929
930fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400931 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700932 Type::Ident(_)
933 | Type::RustBox(_)
934 | Type::UniquePtr(_)
935 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700936 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700937 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700938 | Type::SliceRefU8(_)
939 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400940 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700941 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400942 }
943}
944
David Tolnaycd08c442020-04-25 10:16:33 -0700945// Only called for legal referent types of unique_ptr and element types of
946// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700947fn to_typename(namespace: &Namespace, ty: &Type) -> String {
948 match ty {
949 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700950 let mut path = String::new();
951 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700952 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700953 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700954 }
David Tolnaycd08c442020-04-25 10:16:33 -0700955 path += &ident.to_string();
956 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700957 }
958 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700959 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700960 }
961}
962
David Tolnayacdf20a2020-04-25 12:40:53 -0700963// Only called for legal referent types of unique_ptr and element types of
964// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700965fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
966 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700967 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700968 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700969 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700970 }
971}
972
David Tolnay7db73692019-10-20 14:51:12 -0400973fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
974 fn allow_unique_ptr(ident: &Ident) -> bool {
975 Atom::from(ident).is_none()
976 }
977
978 out.begin_block("extern \"C\"");
979 for ty in types {
980 if let Type::RustBox(ty) = ty {
981 if let Type::Ident(inner) = &ty.inner {
982 out.next_section();
983 write_rust_box_extern(out, inner);
984 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700985 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700986 if let Type::Ident(inner) = &ty.inner {
987 if Atom::from(inner).is_none() {
988 out.next_section();
989 write_rust_vec_extern(out, inner);
990 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700991 }
David Tolnay7db73692019-10-20 14:51:12 -0400992 } else if let Type::UniquePtr(ptr) = ty {
993 if let Type::Ident(inner) = &ptr.inner {
994 if allow_unique_ptr(inner) {
995 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -0700996 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +0700997 }
998 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700999 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001000 if let Type::Ident(inner) = &ptr.inner {
David Tolnay63da4d32020-04-25 09:41:12 -07001001 if Atom::from(inner).is_none() {
Myron Ahneba35cf2020-02-05 19:41:51 +07001002 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001003 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001004 }
1005 }
1006 }
1007 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001008 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001009
David Tolnay750755e2020-03-01 13:04:08 -08001010 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001011 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001012 for ty in types {
1013 if let Type::RustBox(ty) = ty {
1014 if let Type::Ident(inner) = &ty.inner {
1015 write_rust_box_impl(out, inner);
1016 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001017 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001018 if let Type::Ident(inner) = &ty.inner {
1019 if Atom::from(inner).is_none() {
1020 write_rust_vec_impl(out, inner);
1021 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001022 }
David Tolnay7db73692019-10-20 14:51:12 -04001023 }
1024 }
David Tolnay69601622020-04-29 18:48:36 -07001025 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001026 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001027}
1028
1029fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1030 let mut inner = String::new();
1031 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001032 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001033 inner += "::";
1034 }
1035 inner += &ident.to_string();
1036 let instance = inner.replace("::", "$");
1037
David Tolnay69601622020-04-29 18:48:36 -07001038 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1039 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001040 writeln!(
1041 out,
David Tolnay69601622020-04-29 18:48:36 -07001042 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001043 instance, inner,
1044 );
1045 writeln!(
1046 out,
David Tolnay69601622020-04-29 18:48:36 -07001047 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001048 instance, inner,
1049 );
David Tolnay69601622020-04-29 18:48:36 -07001050 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001051}
1052
David Tolnay6787be62020-04-25 11:01:02 -07001053fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1054 let element = Type::Ident(element.clone());
1055 let inner = to_typename(&out.namespace, &element);
1056 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001057
David Tolnay69601622020-04-29 18:48:36 -07001058 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1059 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001060 writeln!(
1061 out,
David Tolnay69601622020-04-29 18:48:36 -07001062 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001063 instance, inner,
1064 );
1065 writeln!(
1066 out,
David Tolnay69601622020-04-29 18:48:36 -07001067 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001068 instance, inner,
1069 );
1070 writeln!(
1071 out,
David Tolnay69601622020-04-29 18:48:36 -07001072 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001073 instance, inner,
1074 );
David Tolnay219c0792020-04-24 20:31:37 -07001075 writeln!(
1076 out,
David Tolnay69601622020-04-29 18:48:36 -07001077 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001078 inner, instance,
1079 );
David Tolnay503d0192020-04-24 22:18:56 -07001080 writeln!(
1081 out,
David Tolnay69601622020-04-29 18:48:36 -07001082 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001083 instance,
1084 );
David Tolnay69601622020-04-29 18:48:36 -07001085 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001086}
1087
David Tolnay7db73692019-10-20 14:51:12 -04001088fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1089 let mut inner = String::new();
1090 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001091 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001092 inner += "::";
1093 }
1094 inner += &ident.to_string();
1095 let instance = inner.replace("::", "$");
1096
1097 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001098 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001099 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001100 writeln!(out, "}}");
1101
1102 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001103 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001104 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001105 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001106}
1107
David Tolnay6787be62020-04-25 11:01:02 -07001108fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1109 let element = Type::Ident(element.clone());
1110 let inner = to_typename(&out.namespace, &element);
1111 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001112
Myron Ahneba35cf2020-02-05 19:41:51 +07001113 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001114 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001115 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001116 writeln!(out, "}}");
1117
1118 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001119 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1120 writeln!(
1121 out,
David Tolnay69601622020-04-29 18:48:36 -07001122 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001123 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001124 );
1125 writeln!(out, "}}");
1126
1127 writeln!(out, "template <>");
1128 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001129 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001130 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001131
1132 writeln!(out, "template <>");
1133 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1134 writeln!(
1135 out,
David Tolnay69601622020-04-29 18:48:36 -07001136 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001137 instance,
1138 );
1139 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001140
1141 writeln!(out, "template <>");
1142 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001143 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001144 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001145}
1146
David Tolnay63da4d32020-04-25 09:41:12 -07001147fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1148 let ty = Type::Ident(ident.clone());
1149 let instance = to_mangled(&out.namespace, &ty);
1150
David Tolnay69601622020-04-29 18:48:36 -07001151 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1152 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001153
1154 write_unique_ptr_common(out, &ty, types);
1155
David Tolnay69601622020-04-29 18:48:36 -07001156 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001157}
1158
1159// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1160fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001161 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001162 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001163 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001164
David Tolnay63da4d32020-04-25 09:41:12 -07001165 let can_construct_from_value = match ty {
1166 Type::Ident(ident) => types.structs.contains_key(ident),
1167 _ => false,
1168 };
1169
David Tolnay7db73692019-10-20 14:51:12 -04001170 writeln!(
1171 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001172 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001173 inner,
1174 );
1175 writeln!(
1176 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001177 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001178 inner,
1179 );
1180 writeln!(
1181 out,
David Tolnay69601622020-04-29 18:48:36 -07001182 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001183 instance, inner,
1184 );
David Tolnay7e219b82020-03-01 13:14:51 -08001185 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001186 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001187 if can_construct_from_value {
1188 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001189 out,
David Tolnay69601622020-04-29 18:48:36 -07001190 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001191 instance, inner, inner,
1192 );
David Tolnay63da4d32020-04-25 09:41:12 -07001193 writeln!(
1194 out,
1195 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1196 inner, inner,
1197 );
1198 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001199 }
David Tolnay7db73692019-10-20 14:51:12 -04001200 writeln!(
1201 out,
David Tolnay69601622020-04-29 18:48:36 -07001202 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001203 instance, inner, inner,
1204 );
David Tolnay7e219b82020-03-01 13:14:51 -08001205 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001206 writeln!(out, "}}");
1207 writeln!(
1208 out,
David Tolnay69601622020-04-29 18:48:36 -07001209 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001210 inner, instance, inner,
1211 );
1212 writeln!(out, " return ptr.get();");
1213 writeln!(out, "}}");
1214 writeln!(
1215 out,
David Tolnay69601622020-04-29 18:48:36 -07001216 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001217 inner, instance, inner,
1218 );
1219 writeln!(out, " return ptr.release();");
1220 writeln!(out, "}}");
1221 writeln!(
1222 out,
David Tolnay69601622020-04-29 18:48:36 -07001223 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001224 instance, inner,
1225 );
1226 writeln!(out, " ptr->~unique_ptr();");
1227 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001228}
Myron Ahneba35cf2020-02-05 19:41:51 +07001229
David Tolnay92105da2020-04-25 11:15:31 -07001230fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001231 let element = Type::Ident(element.clone());
1232 let inner = to_typename(&out.namespace, &element);
1233 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001234
David Tolnay69601622020-04-29 18:48:36 -07001235 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1236 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001237 writeln!(
1238 out,
David Tolnay69601622020-04-29 18:48:36 -07001239 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001240 instance, inner,
1241 );
1242 writeln!(out, " return s.size();");
1243 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001244 writeln!(
1245 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001246 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001247 inner, instance, inner,
1248 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001249 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001250 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001251
1252 write_unique_ptr_common(out, vector_ty, types);
1253
David Tolnay69601622020-04-29 18:48:36 -07001254 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001255}