blob: 70620ce760d25540acd3a9db04957c3a0c0d8a1d [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 {
370 write!(out, " ");
371 write!(out, "{}", variant.ident);
372 if let Some(discriminant) = &variant.discriminant {
373 write!(out, " = {}", discriminant);
374 }
375 writeln!(out, ",");
376 }
377 writeln!(out, "}};");
378}
379
Joel Galenson905eb2e2020-05-04 14:58:14 -0700380fn check_enum(out: &mut OutFile, enm: &Enum) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700381 writeln!(
382 out,
383 "static_assert(sizeof({}) == sizeof(uint32_t));",
384 enm.ident
385 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700386 let mut prev_discriminant = None;
387 for variant in &enm.variants {
388 let discriminant = variant
389 .discriminant
390 .unwrap_or_else(|| prev_discriminant.map_or(0, |n| n + 1));
391 writeln!(
392 out,
393 "static_assert(static_cast<uint32_t>({}::{}) == {},
394 \"disagrees with the value in #[cxx::bridge]\");",
395 enm.ident, variant.ident, discriminant,
396 );
397 prev_discriminant = Some(discriminant);
398 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700399}
400
David Tolnayebef4a22020-03-17 15:33:47 -0700401fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
402 let mut has_cxx_throws = false;
403 for api in apis {
404 if let Api::CxxFunction(efn) = api {
405 if efn.throws {
406 has_cxx_throws = true;
407 break;
408 }
409 }
410 }
411
412 if has_cxx_throws {
413 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700414 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700415 out,
David Tolnay69601622020-04-29 18:48:36 -0700416 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700417 );
418 }
419}
420
David Tolnay7db73692019-10-20 14:51:12 -0400421fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700422 if efn.throws {
423 write!(out, "::rust::Str::Repr ");
424 } else {
David Tolnay99642622020-03-25 13:07:35 -0700425 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700426 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700427 let mangled = mangle::extern_fn(&out.namespace, efn);
428 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700429 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700430 if receiver.mutability.is_none() {
431 write!(out, "const ");
432 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700433 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700434 }
David Tolnay7db73692019-10-20 14:51:12 -0400435 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700436 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400437 write!(out, ", ");
438 }
David Tolnaya46a2372020-03-06 10:03:48 -0800439 if arg.ty == RustString {
440 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700441 } else if let Type::RustVec(_) = arg.ty {
442 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800443 }
David Tolnay7db73692019-10-20 14:51:12 -0400444 write_extern_arg(out, arg, types);
445 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700446 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400447 if indirect_return {
448 if !efn.args.is_empty() {
449 write!(out, ", ");
450 }
David Tolnay99642622020-03-25 13:07:35 -0700451 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400452 write!(out, "*return$");
453 }
454 writeln!(out, ") noexcept {{");
455 write!(out, " ");
456 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700457 match &efn.receiver {
458 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700459 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700460 }
David Tolnay7db73692019-10-20 14:51:12 -0400461 for (i, arg) in efn.args.iter().enumerate() {
462 if i > 0 {
463 write!(out, ", ");
464 }
465 write_type(out, &arg.ty);
466 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700467 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700468 if let Some(receiver) = &efn.receiver {
469 if receiver.mutability.is_none() {
470 write!(out, " const");
471 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700472 }
473 write!(out, " = ");
474 match &efn.receiver {
475 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700476 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700477 }
478 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400479 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700480 if efn.throws {
481 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700482 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700483 writeln!(out, " [&] {{");
484 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700485 }
David Tolnay7db73692019-10-20 14:51:12 -0400486 if indirect_return {
487 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700488 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400489 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700490 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400491 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700492 }
493 match &efn.ret {
494 Some(Type::Ref(_)) => write!(out, "&"),
495 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700496 Some(Type::SliceRefU8(_)) if !indirect_return => {
497 write!(out, "::rust::Slice<uint8_t>::Repr(")
498 }
David Tolnay99642622020-03-25 13:07:35 -0700499 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400500 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700501 match &efn.receiver {
502 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700503 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700504 }
David Tolnay7db73692019-10-20 14:51:12 -0400505 for (i, arg) in efn.args.iter().enumerate() {
506 if i > 0 {
507 write!(out, ", ");
508 }
509 if let Type::RustBox(_) = &arg.ty {
510 write_type(out, &arg.ty);
511 write!(out, "::from_raw({})", arg.ident);
512 } else if let Type::UniquePtr(_) = &arg.ty {
513 write_type(out, &arg.ty);
514 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800515 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800516 write!(
517 out,
518 "::rust::String(::rust::unsafe_bitcopy, *{})",
519 arg.ident,
520 );
David Tolnay313b10e2020-04-25 16:30:51 -0700521 } else if let Type::RustVec(_) = arg.ty {
522 write_type(out, &arg.ty);
523 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400524 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700525 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800526 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400527 } else {
528 write!(out, "{}", arg.ident);
529 }
530 }
531 write!(out, ")");
532 match &efn.ret {
533 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
534 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay4377a9e2020-04-24 15:20:26 -0700535 Some(Type::CxxVector(_)) => write!(
Myron Ahneba35cf2020-02-05 19:41:51 +0700536 out,
537 " /* Use RVO to convert to r-value and move construct */"
538 ),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700539 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400540 _ => {}
541 }
542 if indirect_return {
543 write!(out, ")");
544 }
545 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700546 if efn.throws {
547 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700548 writeln!(out, " throw$.ptr = nullptr;");
549 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700550 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700551 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700552 writeln!(
553 out,
David Tolnay69601622020-04-29 18:48:36 -0700554 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700555 );
David Tolnay5d121442020-03-17 22:14:40 -0700556 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700557 writeln!(out, " return throw$;");
558 }
David Tolnay7db73692019-10-20 14:51:12 -0400559 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700560 for arg in &efn.args {
561 if let Type::Fn(f) = &arg.ty {
562 let var = &arg.ident;
563 write_function_pointer_trampoline(out, efn, var, f, types);
564 }
565 }
566}
567
568fn write_function_pointer_trampoline(
569 out: &mut OutFile,
570 efn: &ExternFn,
571 var: &Ident,
572 f: &Signature,
573 types: &Types,
574) {
575 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700576 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700577 let indirect_call = true;
578 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
579
580 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700581 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700582 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400583}
584
585fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700586 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700587 let indirect_call = false;
588 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
589}
590
591fn write_rust_function_decl_impl(
592 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700593 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 sig: &Signature,
595 types: &Types,
596 indirect_call: bool,
597) {
598 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700599 write!(out, "::rust::Str::Repr ");
600 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700601 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700602 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 write!(out, "{}(", link_name);
604 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700605 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700606 if receiver.mutability.is_none() {
607 write!(out, "const ");
608 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700609 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700610 needs_comma = true;
611 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 for arg in &sig.args {
613 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400614 write!(out, ", ");
615 }
616 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700617 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400618 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 if indirect_return(sig, types) {
620 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400621 write!(out, ", ");
622 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700623 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400624 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700625 needs_comma = true;
626 }
627 if indirect_call {
628 if needs_comma {
629 write!(out, ", ");
630 }
631 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400632 }
633 writeln!(out, ") noexcept;");
634}
635
636fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400637 for line in efn.doc.to_string().lines() {
638 writeln!(out, "//{}", line);
639 }
David Tolnaya73853b2020-04-20 01:19:56 -0700640 let local_name = match &efn.sig.receiver {
641 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700642 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700643 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700644 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700645 let indirect_call = false;
646 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
647}
648
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700649fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700650 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700651 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 indirect_call: bool,
654) {
655 write_return_type(out, &sig.ret);
656 write!(out, "{}(", local_name);
657 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400658 if i > 0 {
659 write!(out, ", ");
660 }
661 write_type_space(out, &arg.ty);
662 write!(out, "{}", arg.ident);
663 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700664 if indirect_call {
665 if !sig.args.is_empty() {
666 write!(out, ", ");
667 }
668 write!(out, "void *extern$");
669 }
David Tolnay1e548172020-03-16 13:37:09 -0700670 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700671 if let Some(receiver) = &sig.receiver {
672 if receiver.mutability.is_none() {
673 write!(out, " const");
674 }
675 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700676 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700677 write!(out, " noexcept");
678 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700679}
680
681fn write_rust_function_shim_impl(
682 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700683 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700684 sig: &Signature,
685 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700686 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700687 indirect_call: bool,
688) {
689 if out.header && sig.receiver.is_some() {
690 // We've already defined this inside the struct.
691 return;
692 }
David Tolnaya73853b2020-04-20 01:19:56 -0700693 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400694 if out.header {
695 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700696 return;
David Tolnay7db73692019-10-20 14:51:12 -0400697 }
David Tolnay439cde22020-04-20 00:46:25 -0700698 writeln!(out, " {{");
699 for arg in &sig.args {
700 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
701 out.include.utility = true;
702 write!(out, " ::rust::ManuallyDrop<");
703 write_type(out, &arg.ty);
704 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
705 }
706 }
707 write!(out, " ");
708 let indirect_return = indirect_return(sig, types);
709 if indirect_return {
710 write!(out, "::rust::MaybeUninit<");
711 write_type(out, sig.ret.as_ref().unwrap());
712 writeln!(out, "> return$;");
713 write!(out, " ");
714 } else if let Some(ret) = &sig.ret {
715 write!(out, "return ");
716 match ret {
717 Type::RustBox(_) => {
718 write_type(out, ret);
719 write!(out, "::from_raw(");
720 }
721 Type::UniquePtr(_) => {
722 write_type(out, ret);
723 write!(out, "(");
724 }
725 Type::Ref(_) => write!(out, "*"),
726 _ => {}
727 }
728 }
729 if sig.throws {
730 write!(out, "::rust::Str::Repr error$ = ");
731 }
732 write!(out, "{}(", invoke);
733 if sig.receiver.is_some() {
734 write!(out, "*this");
735 }
736 for (i, arg) in sig.args.iter().enumerate() {
737 if i > 0 || sig.receiver.is_some() {
738 write!(out, ", ");
739 }
740 match &arg.ty {
741 Type::Str(_) => write!(out, "::rust::Str::Repr("),
742 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
743 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
744 _ => {}
745 }
746 write!(out, "{}", arg.ident);
747 match &arg.ty {
748 Type::RustBox(_) => write!(out, ".into_raw()"),
749 Type::UniquePtr(_) => write!(out, ".release()"),
750 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
751 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
752 _ => {}
753 }
754 }
755 if indirect_return {
756 if !sig.args.is_empty() {
757 write!(out, ", ");
758 }
759 write!(out, "&return$.value");
760 }
761 if indirect_call {
762 if !sig.args.is_empty() || indirect_return {
763 write!(out, ", ");
764 }
765 write!(out, "extern$");
766 }
767 write!(out, ")");
768 if let Some(ret) = &sig.ret {
769 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
770 write!(out, ")");
771 }
772 }
773 writeln!(out, ";");
774 if sig.throws {
775 writeln!(out, " if (error$.ptr) {{");
776 writeln!(out, " throw ::rust::Error(error$);");
777 writeln!(out, " }}");
778 }
779 if indirect_return {
780 out.include.utility = true;
781 writeln!(out, " return ::std::move(return$.value);");
782 }
783 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400784}
785
786fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
787 match ty {
788 None => write!(out, "void "),
789 Some(ty) => write_type_space(out, ty),
790 }
791}
792
David Tolnay75dca2e2020-03-25 20:17:52 -0700793fn indirect_return(sig: &Signature, types: &Types) -> bool {
794 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700795 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700796 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700797}
798
David Tolnay99642622020-03-25 13:07:35 -0700799fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
800 match ty {
801 Type::RustBox(ty) | Type::UniquePtr(ty) => {
802 write_type_space(out, &ty.inner);
803 write!(out, "*");
804 }
805 Type::Ref(ty) => {
806 if ty.mutability.is_none() {
807 write!(out, "const ");
808 }
809 write_type(out, &ty.inner);
810 write!(out, " *");
811 }
812 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700813 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700814 _ => write_type(out, ty),
815 }
816}
817
818fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
819 write_indirect_return_type(out, ty);
820 match ty {
821 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700822 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700823 _ => write_space_after_type(out, ty),
824 }
825}
826
827fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400828 match ty {
829 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
830 write_type_space(out, &ty.inner);
831 write!(out, "*");
832 }
David Tolnay4a441222020-01-25 16:24:27 -0800833 Some(Type::Ref(ty)) => {
834 if ty.mutability.is_none() {
835 write!(out, "const ");
836 }
837 write_type(out, &ty.inner);
838 write!(out, " *");
839 }
David Tolnay750755e2020-03-01 13:04:08 -0800840 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700841 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400842 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
843 _ => write_return_type(out, ty),
844 }
845}
846
847fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
848 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700849 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400850 write_type_space(out, &ty.inner);
851 write!(out, "*");
852 }
David Tolnay750755e2020-03-01 13:04:08 -0800853 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700854 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400855 _ => write_type_space(out, &arg.ty),
856 }
857 if types.needs_indirect_abi(&arg.ty) {
858 write!(out, "*");
859 }
860 write!(out, "{}", arg.ident);
861}
862
863fn write_type(out: &mut OutFile, ty: &Type) {
864 match ty {
865 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay029f1d62020-04-25 10:19:25 -0700866 Some(Bool) => write!(out, "bool"),
867 Some(U8) => write!(out, "uint8_t"),
868 Some(U16) => write!(out, "uint16_t"),
869 Some(U32) => write!(out, "uint32_t"),
870 Some(U64) => write!(out, "uint64_t"),
871 Some(Usize) => write!(out, "size_t"),
872 Some(I8) => write!(out, "int8_t"),
873 Some(I16) => write!(out, "int16_t"),
874 Some(I32) => write!(out, "int32_t"),
875 Some(I64) => write!(out, "int64_t"),
876 Some(Isize) => write!(out, "::rust::isize"),
877 Some(F32) => write!(out, "float"),
878 Some(F64) => write!(out, "double"),
879 Some(CxxString) => write!(out, "::std::string"),
880 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400881 None => write!(out, "{}", ident),
882 },
883 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800884 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400885 write_type(out, &ty.inner);
886 write!(out, ">");
887 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700888 Type::RustVec(ty) => {
889 write!(out, "::rust::Vec<");
890 write_type(out, &ty.inner);
891 write!(out, ">");
892 }
David Tolnay7db73692019-10-20 14:51:12 -0400893 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800894 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400895 write_type(out, &ptr.inner);
896 write!(out, ">");
897 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700898 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700899 write!(out, "::std::vector<");
900 write_type(out, &ty.inner);
901 write!(out, ">");
902 }
David Tolnay7db73692019-10-20 14:51:12 -0400903 Type::Ref(r) => {
904 if r.mutability.is_none() {
905 write!(out, "const ");
906 }
907 write_type(out, &r.inner);
908 write!(out, " &");
909 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700910 Type::Slice(_) => {
911 // For now, only U8 slices are supported, which are covered separately below
912 unreachable!()
913 }
David Tolnay7db73692019-10-20 14:51:12 -0400914 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800915 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400916 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700917 Type::SliceRefU8(_) => {
918 write!(out, "::rust::Slice<uint8_t>");
919 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700920 Type::Fn(f) => {
921 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
922 match &f.ret {
923 Some(ret) => write_type(out, ret),
924 None => write!(out, "void"),
925 }
926 write!(out, "(");
927 for (i, arg) in f.args.iter().enumerate() {
928 if i > 0 {
929 write!(out, ", ");
930 }
931 write_type(out, &arg.ty);
932 }
933 write!(out, ")>");
934 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700935 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400936 }
937}
938
939fn write_type_space(out: &mut OutFile, ty: &Type) {
940 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700941 write_space_after_type(out, ty);
942}
943
944fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400945 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700946 Type::Ident(_)
947 | Type::RustBox(_)
948 | Type::UniquePtr(_)
949 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700950 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700951 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700952 | Type::SliceRefU8(_)
953 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400954 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700955 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400956 }
957}
958
David Tolnaycd08c442020-04-25 10:16:33 -0700959// Only called for legal referent types of unique_ptr and element types of
960// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700961fn to_typename(namespace: &Namespace, ty: &Type) -> String {
962 match ty {
963 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700964 let mut path = String::new();
965 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700966 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700967 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700968 }
David Tolnaycd08c442020-04-25 10:16:33 -0700969 path += &ident.to_string();
970 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700971 }
972 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700973 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700974 }
975}
976
David Tolnayacdf20a2020-04-25 12:40:53 -0700977// Only called for legal referent types of unique_ptr and element types of
978// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700979fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
980 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700981 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700982 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700983 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700984 }
985}
986
David Tolnay7db73692019-10-20 14:51:12 -0400987fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
988 fn allow_unique_ptr(ident: &Ident) -> bool {
989 Atom::from(ident).is_none()
990 }
991
992 out.begin_block("extern \"C\"");
993 for ty in types {
994 if let Type::RustBox(ty) = ty {
995 if let Type::Ident(inner) = &ty.inner {
996 out.next_section();
997 write_rust_box_extern(out, inner);
998 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700999 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001000 if let Type::Ident(inner) = &ty.inner {
1001 if Atom::from(inner).is_none() {
1002 out.next_section();
1003 write_rust_vec_extern(out, inner);
1004 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001005 }
David Tolnay7db73692019-10-20 14:51:12 -04001006 } else if let Type::UniquePtr(ptr) = ty {
1007 if let Type::Ident(inner) = &ptr.inner {
1008 if allow_unique_ptr(inner) {
1009 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001010 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001011 }
1012 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001013 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001014 if let Type::Ident(inner) = &ptr.inner {
David Tolnay63da4d32020-04-25 09:41:12 -07001015 if Atom::from(inner).is_none() {
Myron Ahneba35cf2020-02-05 19:41:51 +07001016 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001017 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001018 }
1019 }
1020 }
1021 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001022 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001023
David Tolnay750755e2020-03-01 13:04:08 -08001024 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001025 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001026 for ty in types {
1027 if let Type::RustBox(ty) = ty {
1028 if let Type::Ident(inner) = &ty.inner {
1029 write_rust_box_impl(out, inner);
1030 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001031 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001032 if let Type::Ident(inner) = &ty.inner {
1033 if Atom::from(inner).is_none() {
1034 write_rust_vec_impl(out, inner);
1035 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001036 }
David Tolnay7db73692019-10-20 14:51:12 -04001037 }
1038 }
David Tolnay69601622020-04-29 18:48:36 -07001039 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001040 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001041}
1042
1043fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1044 let mut inner = String::new();
1045 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001046 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001047 inner += "::";
1048 }
1049 inner += &ident.to_string();
1050 let instance = inner.replace("::", "$");
1051
David Tolnay69601622020-04-29 18:48:36 -07001052 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1053 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001054 writeln!(
1055 out,
David Tolnay69601622020-04-29 18:48:36 -07001056 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001057 instance, inner,
1058 );
1059 writeln!(
1060 out,
David Tolnay69601622020-04-29 18:48:36 -07001061 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001062 instance, inner,
1063 );
David Tolnay69601622020-04-29 18:48:36 -07001064 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001065}
1066
David Tolnay6787be62020-04-25 11:01:02 -07001067fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1068 let element = Type::Ident(element.clone());
1069 let inner = to_typename(&out.namespace, &element);
1070 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001071
David Tolnay69601622020-04-29 18:48:36 -07001072 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1073 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001074 writeln!(
1075 out,
David Tolnay69601622020-04-29 18:48:36 -07001076 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001077 instance, inner,
1078 );
1079 writeln!(
1080 out,
David Tolnay69601622020-04-29 18:48:36 -07001081 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001082 instance, inner,
1083 );
1084 writeln!(
1085 out,
David Tolnay69601622020-04-29 18:48:36 -07001086 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001087 instance, inner,
1088 );
David Tolnay219c0792020-04-24 20:31:37 -07001089 writeln!(
1090 out,
David Tolnay69601622020-04-29 18:48:36 -07001091 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001092 inner, instance,
1093 );
David Tolnay503d0192020-04-24 22:18:56 -07001094 writeln!(
1095 out,
David Tolnay69601622020-04-29 18:48:36 -07001096 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001097 instance,
1098 );
David Tolnay69601622020-04-29 18:48:36 -07001099 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001100}
1101
David Tolnay7db73692019-10-20 14:51:12 -04001102fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1103 let mut inner = String::new();
1104 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001105 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001106 inner += "::";
1107 }
1108 inner += &ident.to_string();
1109 let instance = inner.replace("::", "$");
1110
1111 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001112 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001113 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001114 writeln!(out, "}}");
1115
1116 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001117 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001118 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001119 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001120}
1121
David Tolnay6787be62020-04-25 11:01:02 -07001122fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1123 let element = Type::Ident(element.clone());
1124 let inner = to_typename(&out.namespace, &element);
1125 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001126
Myron Ahneba35cf2020-02-05 19:41:51 +07001127 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001128 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001129 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001130 writeln!(out, "}}");
1131
1132 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001133 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1134 writeln!(
1135 out,
David Tolnay69601622020-04-29 18:48:36 -07001136 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001137 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001138 );
1139 writeln!(out, "}}");
1140
1141 writeln!(out, "template <>");
1142 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001143 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001144 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001145
1146 writeln!(out, "template <>");
1147 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1148 writeln!(
1149 out,
David Tolnay69601622020-04-29 18:48:36 -07001150 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001151 instance,
1152 );
1153 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001154
1155 writeln!(out, "template <>");
1156 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001157 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001158 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001159}
1160
David Tolnay63da4d32020-04-25 09:41:12 -07001161fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1162 let ty = Type::Ident(ident.clone());
1163 let instance = to_mangled(&out.namespace, &ty);
1164
David Tolnay69601622020-04-29 18:48:36 -07001165 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1166 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001167
1168 write_unique_ptr_common(out, &ty, types);
1169
David Tolnay69601622020-04-29 18:48:36 -07001170 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001171}
1172
1173// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1174fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001175 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001176 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001177 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001178
David Tolnay63da4d32020-04-25 09:41:12 -07001179 let can_construct_from_value = match ty {
1180 Type::Ident(ident) => types.structs.contains_key(ident),
1181 _ => false,
1182 };
1183
David Tolnay7db73692019-10-20 14:51:12 -04001184 writeln!(
1185 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001186 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001187 inner,
1188 );
1189 writeln!(
1190 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001191 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001192 inner,
1193 );
1194 writeln!(
1195 out,
David Tolnay69601622020-04-29 18:48:36 -07001196 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001197 instance, inner,
1198 );
David Tolnay7e219b82020-03-01 13:14:51 -08001199 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001200 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001201 if can_construct_from_value {
1202 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001203 out,
David Tolnay69601622020-04-29 18:48:36 -07001204 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001205 instance, inner, inner,
1206 );
David Tolnay63da4d32020-04-25 09:41:12 -07001207 writeln!(
1208 out,
1209 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1210 inner, inner,
1211 );
1212 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001213 }
David Tolnay7db73692019-10-20 14:51:12 -04001214 writeln!(
1215 out,
David Tolnay69601622020-04-29 18:48:36 -07001216 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001217 instance, inner, inner,
1218 );
David Tolnay7e219b82020-03-01 13:14:51 -08001219 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001220 writeln!(out, "}}");
1221 writeln!(
1222 out,
David Tolnay69601622020-04-29 18:48:36 -07001223 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001224 inner, instance, inner,
1225 );
1226 writeln!(out, " return ptr.get();");
1227 writeln!(out, "}}");
1228 writeln!(
1229 out,
David Tolnay69601622020-04-29 18:48:36 -07001230 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001231 inner, instance, inner,
1232 );
1233 writeln!(out, " return ptr.release();");
1234 writeln!(out, "}}");
1235 writeln!(
1236 out,
David Tolnay69601622020-04-29 18:48:36 -07001237 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001238 instance, inner,
1239 );
1240 writeln!(out, " ptr->~unique_ptr();");
1241 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001242}
Myron Ahneba35cf2020-02-05 19:41:51 +07001243
David Tolnay92105da2020-04-25 11:15:31 -07001244fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001245 let element = Type::Ident(element.clone());
1246 let inner = to_typename(&out.namespace, &element);
1247 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001248
David Tolnay69601622020-04-29 18:48:36 -07001249 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1250 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001251 writeln!(
1252 out,
David Tolnay69601622020-04-29 18:48:36 -07001253 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001254 instance, inner,
1255 );
1256 writeln!(out, " return s.size();");
1257 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001258 writeln!(
1259 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001260 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001261 inner, instance, inner,
1262 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001263 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001264 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001265
1266 write_unique_ptr_common(out, vector_ty, types);
1267
David Tolnay69601622020-04-29 18:48:36 -07001268 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001269}