blob: 2cf9faf6e242e910f919b0d52e52aac573cea666 [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 Tolnay91e87fa2020-05-11 19:10:23 -070027 out.include.insert(include);
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 Tolnay32ee9d32020-05-12 20:01:09 -0700247 if needs_rust_string || needs_rust_vec {
David Tolnayd1402742020-03-25 22:21:42 -0700248 out.next_section();
249 writeln!(out, "struct unsafe_bitcopy_t;");
250 }
251
David Tolnaye54f3382020-05-12 19:58:36 -0700252 include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
253 include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
254 include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
255 include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
256 include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
257 include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
258 include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
259 include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
260 include::write(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
314fn write_struct(out: &mut OutFile, strct: &Struct) {
315 for line in strct.doc.to_string().lines() {
316 writeln!(out, "//{}", line);
317 }
318 writeln!(out, "struct {} final {{", strct.ident);
319 for field in &strct.fields {
320 write!(out, " ");
321 write_type_space(out, &field.ty);
322 writeln!(out, "{};", field.ident);
323 }
324 writeln!(out, "}};");
325}
326
327fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
328 writeln!(out, "struct {};", ident);
329}
330
David Tolnay8861bee2020-01-20 18:39:24 -0800331fn write_struct_using(out: &mut OutFile, ident: &Ident) {
332 writeln!(out, "using {} = {};", ident, ident);
333}
334
David Tolnayc1fe0052020-04-17 15:15:06 -0700335fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700336 for line in ety.doc.to_string().lines() {
337 writeln!(out, "//{}", line);
338 }
339 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700340 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700341 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700342 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700343 write!(out, " ");
344 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700345 let local_name = method.ident.to_string();
346 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700347 writeln!(out, ";");
348 }
349 writeln!(out, "}};");
350}
351
Joel Galensonc03402a2020-04-23 17:31:09 -0700352fn write_enum(out: &mut OutFile, enm: &Enum) {
353 for line in enm.doc.to_string().lines() {
354 writeln!(out, "//{}", line);
355 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700356 write!(out, "enum class {} : ", enm.ident);
357 write_atom(out, enm.repr);
358 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700359 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700360 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700361 }
362 writeln!(out, "}};");
363}
364
Joel Galenson905eb2e2020-05-04 14:58:14 -0700365fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700366 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
367 write_atom(out, enm.repr);
368 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700369 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700370 write!(out, "static_assert(static_cast<");
371 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700372 writeln!(
373 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700374 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700375 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700376 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700377 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700378}
379
David Tolnayebef4a22020-03-17 15:33:47 -0700380fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
381 let mut has_cxx_throws = false;
382 for api in apis {
383 if let Api::CxxFunction(efn) = api {
384 if efn.throws {
385 has_cxx_throws = true;
386 break;
387 }
388 }
389 }
390
391 if has_cxx_throws {
392 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700393 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700394 out,
David Tolnay69601622020-04-29 18:48:36 -0700395 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700396 );
397 }
398}
399
David Tolnay7db73692019-10-20 14:51:12 -0400400fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700401 if efn.throws {
402 write!(out, "::rust::Str::Repr ");
403 } else {
David Tolnay99642622020-03-25 13:07:35 -0700404 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700405 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700406 let mangled = mangle::extern_fn(&out.namespace, efn);
407 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700408 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700409 if receiver.mutability.is_none() {
410 write!(out, "const ");
411 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700412 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700413 }
David Tolnay7db73692019-10-20 14:51:12 -0400414 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700415 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400416 write!(out, ", ");
417 }
David Tolnaya46a2372020-03-06 10:03:48 -0800418 if arg.ty == RustString {
419 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700420 } else if let Type::RustVec(_) = arg.ty {
421 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800422 }
David Tolnay7db73692019-10-20 14:51:12 -0400423 write_extern_arg(out, arg, types);
424 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700425 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400426 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700427 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400428 write!(out, ", ");
429 }
David Tolnay99642622020-03-25 13:07:35 -0700430 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400431 write!(out, "*return$");
432 }
433 writeln!(out, ") noexcept {{");
434 write!(out, " ");
435 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700436 match &efn.receiver {
437 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700438 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700439 }
David Tolnay7db73692019-10-20 14:51:12 -0400440 for (i, arg) in efn.args.iter().enumerate() {
441 if i > 0 {
442 write!(out, ", ");
443 }
444 write_type(out, &arg.ty);
445 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700446 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700447 if let Some(receiver) = &efn.receiver {
448 if receiver.mutability.is_none() {
449 write!(out, " const");
450 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700451 }
452 write!(out, " = ");
453 match &efn.receiver {
454 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700455 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700456 }
457 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400458 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700459 if efn.throws {
460 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700461 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700462 writeln!(out, " [&] {{");
463 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700464 }
David Tolnay7db73692019-10-20 14:51:12 -0400465 if indirect_return {
466 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700467 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400468 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700469 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400470 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700471 }
472 match &efn.ret {
473 Some(Type::Ref(_)) => write!(out, "&"),
474 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700475 Some(Type::SliceRefU8(_)) if !indirect_return => {
476 write!(out, "::rust::Slice<uint8_t>::Repr(")
477 }
David Tolnay99642622020-03-25 13:07:35 -0700478 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400479 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700480 match &efn.receiver {
481 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700482 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700483 }
David Tolnay7db73692019-10-20 14:51:12 -0400484 for (i, arg) in efn.args.iter().enumerate() {
485 if i > 0 {
486 write!(out, ", ");
487 }
488 if let Type::RustBox(_) = &arg.ty {
489 write_type(out, &arg.ty);
490 write!(out, "::from_raw({})", arg.ident);
491 } else if let Type::UniquePtr(_) = &arg.ty {
492 write_type(out, &arg.ty);
493 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800494 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800495 write!(
496 out,
497 "::rust::String(::rust::unsafe_bitcopy, *{})",
498 arg.ident,
499 );
David Tolnay313b10e2020-04-25 16:30:51 -0700500 } else if let Type::RustVec(_) = arg.ty {
501 write_type(out, &arg.ty);
502 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400503 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700504 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800505 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400506 } else {
507 write!(out, "{}", arg.ident);
508 }
509 }
510 write!(out, ")");
511 match &efn.ret {
512 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
513 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700514 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400515 _ => {}
516 }
517 if indirect_return {
518 write!(out, ")");
519 }
520 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700521 if efn.throws {
522 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700523 writeln!(out, " throw$.ptr = nullptr;");
524 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700525 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700526 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700527 writeln!(
528 out,
David Tolnay69601622020-04-29 18:48:36 -0700529 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700530 );
David Tolnay5d121442020-03-17 22:14:40 -0700531 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700532 writeln!(out, " return throw$;");
533 }
David Tolnay7db73692019-10-20 14:51:12 -0400534 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700535 for arg in &efn.args {
536 if let Type::Fn(f) = &arg.ty {
537 let var = &arg.ident;
538 write_function_pointer_trampoline(out, efn, var, f, types);
539 }
540 }
541}
542
543fn write_function_pointer_trampoline(
544 out: &mut OutFile,
545 efn: &ExternFn,
546 var: &Ident,
547 f: &Signature,
548 types: &Types,
549) {
550 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700551 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700552 let indirect_call = true;
553 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
554
555 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700556 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700557 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400558}
559
560fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700561 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700562 let indirect_call = false;
563 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
564}
565
566fn write_rust_function_decl_impl(
567 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700568 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700569 sig: &Signature,
570 types: &Types,
571 indirect_call: bool,
572) {
573 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700574 write!(out, "::rust::Str::Repr ");
575 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700576 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700577 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700578 write!(out, "{}(", link_name);
579 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700580 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700581 if receiver.mutability.is_none() {
582 write!(out, "const ");
583 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700584 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700585 needs_comma = true;
586 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700587 for arg in &sig.args {
588 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400589 write!(out, ", ");
590 }
591 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400593 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 if indirect_return(sig, types) {
595 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400596 write!(out, ", ");
597 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400599 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700600 needs_comma = true;
601 }
602 if indirect_call {
603 if needs_comma {
604 write!(out, ", ");
605 }
606 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400607 }
608 writeln!(out, ") noexcept;");
609}
610
611fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400612 for line in efn.doc.to_string().lines() {
613 writeln!(out, "//{}", line);
614 }
David Tolnaya73853b2020-04-20 01:19:56 -0700615 let local_name = match &efn.sig.receiver {
616 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700617 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700618 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700619 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700620 let indirect_call = false;
621 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
622}
623
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700624fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700625 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700626 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 indirect_call: bool,
629) {
630 write_return_type(out, &sig.ret);
631 write!(out, "{}(", local_name);
632 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400633 if i > 0 {
634 write!(out, ", ");
635 }
636 write_type_space(out, &arg.ty);
637 write!(out, "{}", arg.ident);
638 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700639 if indirect_call {
640 if !sig.args.is_empty() {
641 write!(out, ", ");
642 }
643 write!(out, "void *extern$");
644 }
David Tolnay1e548172020-03-16 13:37:09 -0700645 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700646 if let Some(receiver) = &sig.receiver {
647 if receiver.mutability.is_none() {
648 write!(out, " const");
649 }
650 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700651 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700652 write!(out, " noexcept");
653 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700654}
655
656fn write_rust_function_shim_impl(
657 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700658 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700659 sig: &Signature,
660 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700661 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700662 indirect_call: bool,
663) {
664 if out.header && sig.receiver.is_some() {
665 // We've already defined this inside the struct.
666 return;
667 }
David Tolnaya73853b2020-04-20 01:19:56 -0700668 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400669 if out.header {
670 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700671 return;
David Tolnay7db73692019-10-20 14:51:12 -0400672 }
David Tolnay439cde22020-04-20 00:46:25 -0700673 writeln!(out, " {{");
674 for arg in &sig.args {
675 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
676 out.include.utility = true;
677 write!(out, " ::rust::ManuallyDrop<");
678 write_type(out, &arg.ty);
679 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
680 }
681 }
682 write!(out, " ");
683 let indirect_return = indirect_return(sig, types);
684 if indirect_return {
685 write!(out, "::rust::MaybeUninit<");
686 write_type(out, sig.ret.as_ref().unwrap());
687 writeln!(out, "> return$;");
688 write!(out, " ");
689 } else if let Some(ret) = &sig.ret {
690 write!(out, "return ");
691 match ret {
692 Type::RustBox(_) => {
693 write_type(out, ret);
694 write!(out, "::from_raw(");
695 }
696 Type::UniquePtr(_) => {
697 write_type(out, ret);
698 write!(out, "(");
699 }
700 Type::Ref(_) => write!(out, "*"),
701 _ => {}
702 }
703 }
704 if sig.throws {
705 write!(out, "::rust::Str::Repr error$ = ");
706 }
707 write!(out, "{}(", invoke);
708 if sig.receiver.is_some() {
709 write!(out, "*this");
710 }
711 for (i, arg) in sig.args.iter().enumerate() {
712 if i > 0 || sig.receiver.is_some() {
713 write!(out, ", ");
714 }
715 match &arg.ty {
716 Type::Str(_) => write!(out, "::rust::Str::Repr("),
717 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
718 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
719 _ => {}
720 }
721 write!(out, "{}", arg.ident);
722 match &arg.ty {
723 Type::RustBox(_) => write!(out, ".into_raw()"),
724 Type::UniquePtr(_) => write!(out, ".release()"),
725 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
726 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
727 _ => {}
728 }
729 }
730 if indirect_return {
731 if !sig.args.is_empty() {
732 write!(out, ", ");
733 }
734 write!(out, "&return$.value");
735 }
736 if indirect_call {
737 if !sig.args.is_empty() || indirect_return {
738 write!(out, ", ");
739 }
740 write!(out, "extern$");
741 }
742 write!(out, ")");
743 if let Some(ret) = &sig.ret {
744 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
745 write!(out, ")");
746 }
747 }
748 writeln!(out, ";");
749 if sig.throws {
750 writeln!(out, " if (error$.ptr) {{");
751 writeln!(out, " throw ::rust::Error(error$);");
752 writeln!(out, " }}");
753 }
754 if indirect_return {
755 out.include.utility = true;
756 writeln!(out, " return ::std::move(return$.value);");
757 }
758 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400759}
760
761fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
762 match ty {
763 None => write!(out, "void "),
764 Some(ty) => write_type_space(out, ty),
765 }
766}
767
David Tolnay75dca2e2020-03-25 20:17:52 -0700768fn indirect_return(sig: &Signature, types: &Types) -> bool {
769 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700770 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700771 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700772}
773
David Tolnay99642622020-03-25 13:07:35 -0700774fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
775 match ty {
776 Type::RustBox(ty) | Type::UniquePtr(ty) => {
777 write_type_space(out, &ty.inner);
778 write!(out, "*");
779 }
780 Type::Ref(ty) => {
781 if ty.mutability.is_none() {
782 write!(out, "const ");
783 }
784 write_type(out, &ty.inner);
785 write!(out, " *");
786 }
787 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700788 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700789 _ => write_type(out, ty),
790 }
791}
792
793fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
794 write_indirect_return_type(out, ty);
795 match ty {
796 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700797 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700798 _ => write_space_after_type(out, ty),
799 }
800}
801
802fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400803 match ty {
804 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
805 write_type_space(out, &ty.inner);
806 write!(out, "*");
807 }
David Tolnay4a441222020-01-25 16:24:27 -0800808 Some(Type::Ref(ty)) => {
809 if ty.mutability.is_none() {
810 write!(out, "const ");
811 }
812 write_type(out, &ty.inner);
813 write!(out, " *");
814 }
David Tolnay750755e2020-03-01 13:04:08 -0800815 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700816 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400817 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
818 _ => write_return_type(out, ty),
819 }
820}
821
822fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
823 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700824 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400825 write_type_space(out, &ty.inner);
826 write!(out, "*");
827 }
David Tolnay750755e2020-03-01 13:04:08 -0800828 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700829 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400830 _ => write_type_space(out, &arg.ty),
831 }
832 if types.needs_indirect_abi(&arg.ty) {
833 write!(out, "*");
834 }
835 write!(out, "{}", arg.ident);
836}
837
838fn write_type(out: &mut OutFile, ty: &Type) {
839 match ty {
840 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700841 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400842 None => write!(out, "{}", ident),
843 },
844 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800845 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400846 write_type(out, &ty.inner);
847 write!(out, ">");
848 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700849 Type::RustVec(ty) => {
850 write!(out, "::rust::Vec<");
851 write_type(out, &ty.inner);
852 write!(out, ">");
853 }
David Tolnay7db73692019-10-20 14:51:12 -0400854 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800855 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400856 write_type(out, &ptr.inner);
857 write!(out, ">");
858 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700859 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700860 write!(out, "::std::vector<");
861 write_type(out, &ty.inner);
862 write!(out, ">");
863 }
David Tolnay7db73692019-10-20 14:51:12 -0400864 Type::Ref(r) => {
865 if r.mutability.is_none() {
866 write!(out, "const ");
867 }
868 write_type(out, &r.inner);
869 write!(out, " &");
870 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700871 Type::Slice(_) => {
872 // For now, only U8 slices are supported, which are covered separately below
873 unreachable!()
874 }
David Tolnay7db73692019-10-20 14:51:12 -0400875 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800876 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400877 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700878 Type::SliceRefU8(_) => {
879 write!(out, "::rust::Slice<uint8_t>");
880 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700881 Type::Fn(f) => {
882 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
883 match &f.ret {
884 Some(ret) => write_type(out, ret),
885 None => write!(out, "void"),
886 }
887 write!(out, "(");
888 for (i, arg) in f.args.iter().enumerate() {
889 if i > 0 {
890 write!(out, ", ");
891 }
892 write_type(out, &arg.ty);
893 }
894 write!(out, ")>");
895 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700896 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400897 }
898}
899
David Tolnayf6a89f22020-05-10 23:39:27 -0700900fn write_atom(out: &mut OutFile, atom: Atom) {
901 match atom {
902 Bool => write!(out, "bool"),
903 U8 => write!(out, "uint8_t"),
904 U16 => write!(out, "uint16_t"),
905 U32 => write!(out, "uint32_t"),
906 U64 => write!(out, "uint64_t"),
907 Usize => write!(out, "size_t"),
908 I8 => write!(out, "int8_t"),
909 I16 => write!(out, "int16_t"),
910 I32 => write!(out, "int32_t"),
911 I64 => write!(out, "int64_t"),
912 Isize => write!(out, "::rust::isize"),
913 F32 => write!(out, "float"),
914 F64 => write!(out, "double"),
915 CxxString => write!(out, "::std::string"),
916 RustString => write!(out, "::rust::String"),
917 }
918}
919
David Tolnay7db73692019-10-20 14:51:12 -0400920fn write_type_space(out: &mut OutFile, ty: &Type) {
921 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700922 write_space_after_type(out, ty);
923}
924
925fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400926 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700927 Type::Ident(_)
928 | Type::RustBox(_)
929 | Type::UniquePtr(_)
930 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700931 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700932 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700933 | Type::SliceRefU8(_)
934 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400935 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700936 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400937 }
938}
939
David Tolnaycd08c442020-04-25 10:16:33 -0700940// Only called for legal referent types of unique_ptr and element types of
941// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700942fn to_typename(namespace: &Namespace, ty: &Type) -> String {
943 match ty {
944 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700945 let mut path = String::new();
946 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700947 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700948 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700949 }
David Tolnaycd08c442020-04-25 10:16:33 -0700950 path += &ident.to_string();
951 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700952 }
953 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700954 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700955 }
956}
957
David Tolnayacdf20a2020-04-25 12:40:53 -0700958// Only called for legal referent types of unique_ptr and element types of
959// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700960fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
961 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700962 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700963 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700964 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700965 }
966}
967
David Tolnay7db73692019-10-20 14:51:12 -0400968fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400969 out.begin_block("extern \"C\"");
970 for ty in types {
971 if let Type::RustBox(ty) = ty {
972 if let Type::Ident(inner) = &ty.inner {
973 out.next_section();
974 write_rust_box_extern(out, inner);
975 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700976 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700977 if let Type::Ident(inner) = &ty.inner {
978 if Atom::from(inner).is_none() {
979 out.next_section();
980 write_rust_vec_extern(out, inner);
981 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700982 }
David Tolnay7db73692019-10-20 14:51:12 -0400983 } else if let Type::UniquePtr(ptr) = ty {
984 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -0700985 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -0400986 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -0700987 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +0700988 }
989 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700990 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +0700991 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -0700992 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700993 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -0700994 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400995 }
996 }
997 }
998 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800999 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001000
David Tolnay750755e2020-03-01 13:04:08 -08001001 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001002 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001003 for ty in types {
1004 if let Type::RustBox(ty) = ty {
1005 if let Type::Ident(inner) = &ty.inner {
1006 write_rust_box_impl(out, inner);
1007 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001008 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001009 if let Type::Ident(inner) = &ty.inner {
1010 if Atom::from(inner).is_none() {
1011 write_rust_vec_impl(out, inner);
1012 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001013 }
David Tolnay7db73692019-10-20 14:51:12 -04001014 }
1015 }
David Tolnay69601622020-04-29 18:48:36 -07001016 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001017 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001018}
1019
1020fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1021 let mut inner = String::new();
1022 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001023 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001024 inner += "::";
1025 }
1026 inner += &ident.to_string();
1027 let instance = inner.replace("::", "$");
1028
David Tolnay69601622020-04-29 18:48:36 -07001029 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1030 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001031 writeln!(
1032 out,
David Tolnay69601622020-04-29 18:48:36 -07001033 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001034 instance, inner,
1035 );
1036 writeln!(
1037 out,
David Tolnay69601622020-04-29 18:48:36 -07001038 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001039 instance, inner,
1040 );
David Tolnay69601622020-04-29 18:48:36 -07001041 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001042}
1043
David Tolnay6787be62020-04-25 11:01:02 -07001044fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1045 let element = Type::Ident(element.clone());
1046 let inner = to_typename(&out.namespace, &element);
1047 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001048
David Tolnay69601622020-04-29 18:48:36 -07001049 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1050 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001051 writeln!(
1052 out,
David Tolnay69601622020-04-29 18:48:36 -07001053 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001054 instance, inner,
1055 );
1056 writeln!(
1057 out,
David Tolnay69601622020-04-29 18:48:36 -07001058 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001059 instance, inner,
1060 );
1061 writeln!(
1062 out,
David Tolnay69601622020-04-29 18:48:36 -07001063 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001064 instance, inner,
1065 );
David Tolnay219c0792020-04-24 20:31:37 -07001066 writeln!(
1067 out,
David Tolnay69601622020-04-29 18:48:36 -07001068 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001069 inner, instance,
1070 );
David Tolnay503d0192020-04-24 22:18:56 -07001071 writeln!(
1072 out,
David Tolnay69601622020-04-29 18:48:36 -07001073 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001074 instance,
1075 );
David Tolnay69601622020-04-29 18:48:36 -07001076 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001077}
1078
David Tolnay7db73692019-10-20 14:51:12 -04001079fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1080 let mut inner = String::new();
1081 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001082 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001083 inner += "::";
1084 }
1085 inner += &ident.to_string();
1086 let instance = inner.replace("::", "$");
1087
1088 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001089 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001090 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001091 writeln!(out, "}}");
1092
1093 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001094 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001095 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001096 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001097}
1098
David Tolnay6787be62020-04-25 11:01:02 -07001099fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1100 let element = Type::Ident(element.clone());
1101 let inner = to_typename(&out.namespace, &element);
1102 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001103
Myron Ahneba35cf2020-02-05 19:41:51 +07001104 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001105 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001106 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001107 writeln!(out, "}}");
1108
1109 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001110 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1111 writeln!(
1112 out,
David Tolnay69601622020-04-29 18:48:36 -07001113 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001114 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001115 );
1116 writeln!(out, "}}");
1117
1118 writeln!(out, "template <>");
1119 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001120 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001121 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001122
1123 writeln!(out, "template <>");
1124 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1125 writeln!(
1126 out,
David Tolnay69601622020-04-29 18:48:36 -07001127 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001128 instance,
1129 );
1130 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001131
1132 writeln!(out, "template <>");
1133 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001134 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001135 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001136}
1137
David Tolnay63da4d32020-04-25 09:41:12 -07001138fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1139 let ty = Type::Ident(ident.clone());
1140 let instance = to_mangled(&out.namespace, &ty);
1141
David Tolnay69601622020-04-29 18:48:36 -07001142 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1143 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001144
1145 write_unique_ptr_common(out, &ty, types);
1146
David Tolnay69601622020-04-29 18:48:36 -07001147 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001148}
1149
1150// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1151fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001152 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001153 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001154 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001155
David Tolnay63da4d32020-04-25 09:41:12 -07001156 let can_construct_from_value = match ty {
1157 Type::Ident(ident) => types.structs.contains_key(ident),
1158 _ => false,
1159 };
1160
David Tolnay7db73692019-10-20 14:51:12 -04001161 writeln!(
1162 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001163 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001164 inner,
1165 );
1166 writeln!(
1167 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001168 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001169 inner,
1170 );
1171 writeln!(
1172 out,
David Tolnay69601622020-04-29 18:48:36 -07001173 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001174 instance, inner,
1175 );
David Tolnay7e219b82020-03-01 13:14:51 -08001176 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001177 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001178 if can_construct_from_value {
1179 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001180 out,
David Tolnay69601622020-04-29 18:48:36 -07001181 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001182 instance, inner, inner,
1183 );
David Tolnay63da4d32020-04-25 09:41:12 -07001184 writeln!(
1185 out,
1186 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1187 inner, inner,
1188 );
1189 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001190 }
David Tolnay7db73692019-10-20 14:51:12 -04001191 writeln!(
1192 out,
David Tolnay69601622020-04-29 18:48:36 -07001193 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001194 instance, inner, inner,
1195 );
David Tolnay7e219b82020-03-01 13:14:51 -08001196 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001197 writeln!(out, "}}");
1198 writeln!(
1199 out,
David Tolnay69601622020-04-29 18:48:36 -07001200 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001201 inner, instance, inner,
1202 );
1203 writeln!(out, " return ptr.get();");
1204 writeln!(out, "}}");
1205 writeln!(
1206 out,
David Tolnay69601622020-04-29 18:48:36 -07001207 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001208 inner, instance, inner,
1209 );
1210 writeln!(out, " return ptr.release();");
1211 writeln!(out, "}}");
1212 writeln!(
1213 out,
David Tolnay69601622020-04-29 18:48:36 -07001214 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001215 instance, inner,
1216 );
1217 writeln!(out, " ptr->~unique_ptr();");
1218 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001219}
Myron Ahneba35cf2020-02-05 19:41:51 +07001220
David Tolnay92105da2020-04-25 11:15:31 -07001221fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001222 let element = Type::Ident(element.clone());
1223 let inner = to_typename(&out.namespace, &element);
1224 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001225
David Tolnay69601622020-04-29 18:48:36 -07001226 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1227 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001228 writeln!(
1229 out,
David Tolnay69601622020-04-29 18:48:36 -07001230 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001231 instance, inner,
1232 );
1233 writeln!(out, " return s.size();");
1234 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001235 writeln!(
1236 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001237 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001238 inner, instance, inner,
1239 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001240 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001241 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001242
1243 write_unique_ptr_common(out, vector_ty, types);
1244
David Tolnay69601622020-04-29 18:48:36 -07001245 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001246}