blob: 0514554e9432d5be83c7cd64b0118e99c80ae8d7 [file] [log] [blame]
Adrian Taylor565ddf02020-10-29 21:12:36 -07001use crate::gen::namespace_organizer::NamespaceEntries;
David Tolnay7db73692019-10-20 14:51:12 -04002use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07003use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04004use crate::syntax::atom::Atom::{self, *};
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Adrian Taylorc8713432020-10-21 18:20:55 -07006use crate::syntax::{
7 mangle, Api, CppName, Enum, ExternFn, ExternType, ResolvableName, Signature, Struct, Type,
8 Types, Var,
9};
David Tolnay7db73692019-10-20 14:51:12 -040010use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -070011use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040012
David Tolnayb560a0f2020-10-30 21:28:45 -070013pub(super) fn gen<'a>(apis: &[Api], types: &'a Types, opt: &Opt, header: bool) -> OutFile<'a> {
14 let mut out_file = OutFile::new(header, types);
David Tolnay7db73692019-10-20 14:51:12 -040015 let out = &mut out_file;
16
David Tolnay54702b92020-07-31 11:50:09 -070017 if header {
18 writeln!(out.front, "#pragma once");
19 }
20
David Tolnay4aae7c02020-10-28 12:35:42 -070021 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040022 for api in apis {
23 if let Api::Include(include) = api {
David Tolnay353d98c2020-10-28 15:43:35 -070024 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040025 }
26 }
27
David Tolnay528200f2020-10-31 21:02:22 -070028 pick_includes_and_builtins(out, apis);
David Tolnayec66d112020-10-31 21:00:26 -070029 write_builtins(out);
David Tolnay7db73692019-10-20 14:51:12 -040030
David Tolnay7db73692019-10-20 14:51:12 -040031 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070032
Adrian Taylor565ddf02020-10-29 21:12:36 -070033 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070034
David Tolnay04b81652020-10-31 22:43:25 -070035 gen_namespace_forward_declarations(out, &apis_by_namespace);
36 gen_namespace_contents(out, &apis_by_namespace, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -070037
38 if !header {
39 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070040 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040041 }
42
Adrian Taylorc8713432020-10-21 18:20:55 -070043 write!(out.front, "{}", out.include);
44
45 out_file
46}
47
David Tolnay04b81652020-10-31 22:43:25 -070048fn gen_namespace_forward_declarations(out: &mut OutFile, ns_entries: &NamespaceEntries) {
Adrian Taylor565ddf02020-10-29 21:12:36 -070049 let apis = ns_entries.entries();
Adrian Taylorc8713432020-10-21 18:20:55 -070050
David Tolnay7db73692019-10-20 14:51:12 -040051 out.next_section();
David Tolnay630af882020-10-31 22:03:47 -070052 for api in apis {
David Tolnay7db73692019-10-20 14:51:12 -040053 match api {
Adrian Taylorc8713432020-10-21 18:20:55 -070054 Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident),
55 Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx),
56 Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident),
David Tolnay7c295462020-04-25 12:45:07 -070057 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040058 }
59 }
60
Adrian Taylorf9213622020-10-31 22:25:42 -070061 out.next_section();
62
63 for (child_ns, child_ns_entries) in ns_entries.children() {
64 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -070065 gen_namespace_forward_declarations(out, child_ns_entries);
Adrian Taylorf9213622020-10-31 22:25:42 -070066 writeln!(out, "}} // namespace {}", child_ns);
67 }
68}
69
David Tolnay04b81652020-10-31 22:43:25 -070070fn gen_namespace_contents(out: &mut OutFile, ns_entries: &NamespaceEntries, opt: &Opt) {
Adrian Taylorf9213622020-10-31 22:25:42 -070071 let apis = ns_entries.entries();
72
David Tolnayf94bef12020-04-17 14:46:42 -070073 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070074 for api in apis {
David Tolnayf94bef12020-04-17 14:46:42 -070075 if let Api::RustFunction(efn) = api {
76 if let Some(receiver) = &efn.sig.receiver {
77 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070078 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070079 .or_insert_with(Vec::new)
80 .push(efn);
81 }
82 }
83 }
Joel Galenson968738f2020-04-15 14:19:33 -070084
David Tolnay7db73692019-10-20 14:51:12 -040085 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070086 match api {
87 Api::Struct(strct) => {
88 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070089 if !out.types.cxx.contains(&strct.ident.rust) {
David Tolnaya7c2ea12020-10-30 21:32:53 -070090 write_struct(out, strct);
David Tolnaya593d6e2020-08-29 19:48:08 -070091 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070092 }
Joel Galensonc03402a2020-04-23 17:31:09 -070093 Api::Enum(enm) => {
94 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070095 if out.types.cxx.contains(&enm.ident.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070096 check_enum(out, enm);
97 } else {
98 write_enum(out, enm);
99 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700100 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700101 Api::RustType(ety) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700102 if let Some(methods) = methods_for_type.get(&ety.ident.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -0700103 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700104 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700105 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700106 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700107 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400108 }
109 }
110
David Tolnayfabca772020-10-03 21:25:41 -0700111 out.next_section();
112 for api in apis {
113 if let Api::TypeAlias(ety) = api {
David Tolnay4d148422020-10-31 22:41:37 -0700114 if out.types.required_trivial.contains_key(&ety.ident.rust) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700115 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700116 }
117 }
118 }
119
David Tolnayce5a91f2020-10-31 22:42:08 -0700120 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400121 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700122 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400123 for api in apis {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700124 let (efn, write): (_, fn(_, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400125 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
126 Api::RustFunction(efn) => (efn, write_rust_function_decl),
127 _ => continue,
128 };
129 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700130 write(out, efn, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400131 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800132 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400133 }
134
135 for api in apis {
136 if let Api::RustFunction(efn) = api {
137 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700138 write_rust_function_shim(out, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400139 }
140 }
141
142 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700143
Adrian Taylor565ddf02020-10-29 21:12:36 -0700144 for (child_ns, child_ns_entries) in ns_entries.children() {
Adrian Taylorc8713432020-10-21 18:20:55 -0700145 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -0700146 gen_namespace_contents(out, child_ns_entries, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -0700147 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400148 }
David Tolnay7db73692019-10-20 14:51:12 -0400149}
150
David Tolnay528200f2020-10-31 21:02:22 -0700151fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700152 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400153 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700154 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700155 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
156 | Some(I64) => out.include.cstdint = true,
157 Some(Usize) => out.include.cstddef = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700158 Some(Isize) => {
159 out.include.basetsd = true;
160 out.builtin.rust_isize = true;
161 }
David Tolnay89e386d2020-10-03 19:02:19 -0700162 Some(CxxString) => out.include.string = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700163 Some(RustString) => {
164 out.include.array = true;
165 out.include.cstdint = true;
166 out.include.string = true;
167 out.builtin.rust_string = true;
168 }
169 Some(Bool) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700170 },
David Tolnayb7a7cb62020-03-17 21:18:40 -0700171 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700172 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700173 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700174 out.builtin.rust_box = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700175 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700176 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700177 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700178 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700179 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700180 out.builtin.panic = true;
181 out.builtin.rust_vec = true;
182 out.builtin.unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700183 }
David Tolnayb9da1462020-10-31 21:03:41 -0700184 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700185 Type::Str(_) => {
186 out.include.cstdint = true;
187 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700188 out.builtin.rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700189 }
David Tolnayb9da1462020-10-31 21:03:41 -0700190 Type::CxxVector(_) => out.include.vector = true,
David Tolnay75dca2e2020-03-25 20:17:52 -0700191 Type::Fn(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700192 out.builtin.rust_fn = true;
David Tolnay75dca2e2020-03-25 20:17:52 -0700193 }
David Tolnayb9da1462020-10-31 21:03:41 -0700194 Type::Slice(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700195 out.builtin.rust_slice = true;
David Tolnay4770b472020-04-14 16:32:59 -0700196 }
David Tolnayb9da1462020-10-31 21:03:41 -0700197 Type::SliceRefU8(_) => {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700198 out.include.cstdint = true;
David Tolnayb9da1462020-10-31 21:03:41 -0700199 out.builtin.rust_slice = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700200 }
201 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400202 }
203 }
204
David Tolnay09011c32020-03-06 14:40:28 -0800205 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700206 match api {
207 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700208 if efn.throws {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700209 out.builtin.trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700210 } else if let Some(Type::Str(_)) = efn.ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700211 out.builtin.rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700212 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700214 match arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700215 Type::Str(_) => out.builtin.rust_str_new_unchecked = true,
216 Type::RustVec(_) => out.builtin.unsafe_bitcopy = true,
217 _ => out.builtin.unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700218 }
David Tolnay09011c32020-03-06 14:40:28 -0800219 }
220 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 Api::RustFunction(efn) if !out.header => {
222 if efn.throws {
223 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700224 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700225 out.builtin.rust_str = true;
226 out.builtin.rust_error = true;
227 out.builtin.maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 }
229 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700230 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700231 out.builtin.manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700232 }
233 if let Type::Str(_) = arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700234 out.builtin.rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 }
236 }
237 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700238 if out.types.needs_indirect_abi(ret) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700239 out.builtin.maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700240 } else if let Type::Str(_) = ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700241 out.builtin.rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 }
David Tolnayf51447e2020-03-06 14:14:27 -0800243 }
244 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800246 }
247 }
David Tolnayec66d112020-10-31 21:00:26 -0700248}
David Tolnayf51447e2020-03-06 14:14:27 -0800249
David Tolnayec66d112020-10-31 21:00:26 -0700250fn write_builtins(out: &mut OutFile) {
David Tolnay750755e2020-03-01 13:04:08 -0800251 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700252 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800253
David Tolnay3be0e1f2020-10-31 20:53:00 -0700254 if out.builtin != Default::default() {
David Tolnay736cbca2020-03-11 16:49:18 -0700255 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800256 }
257
David Tolnay3be0e1f2020-10-31 20:53:00 -0700258 include::write(out, out.builtin.panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700259
David Tolnay3be0e1f2020-10-31 20:53:00 -0700260 if out.builtin.rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700261 out.next_section();
262 writeln!(out, "struct unsafe_bitcopy_t;");
263 }
264
David Tolnay3be0e1f2020-10-31 20:53:00 -0700265 if out.builtin.rust_error {
David Tolnay84ddf9e2020-10-31 15:36:48 -0700266 out.begin_block("namespace");
267 writeln!(out, "template <typename T>");
268 writeln!(out, "class impl;");
269 out.end_block("namespace");
270 }
271
David Tolnay3be0e1f2020-10-31 20:53:00 -0700272 include::write(out, out.builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
273 include::write(out, out.builtin.rust_str, "CXXBRIDGE05_RUST_STR");
274 include::write(out, out.builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
275 include::write(out, out.builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
276 include::write(out, out.builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
277 include::write(out, out.builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
278 include::write(out, out.builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
279 include::write(out, out.builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
280 include::write(out, out.builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800281
David Tolnay3be0e1f2020-10-31 20:53:00 -0700282 if out.builtin.manually_drop {
David Tolnayf51447e2020-03-06 14:14:27 -0800283 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700284 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800285 writeln!(out, "template <typename T>");
286 writeln!(out, "union ManuallyDrop {{");
287 writeln!(out, " T value;");
288 writeln!(
289 out,
290 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
291 );
292 writeln!(out, " ~ManuallyDrop() {{}}");
293 writeln!(out, "}};");
294 }
295
David Tolnay3be0e1f2020-10-31 20:53:00 -0700296 if out.builtin.maybe_uninit {
David Tolnay09011c32020-03-06 14:40:28 -0800297 out.next_section();
298 writeln!(out, "template <typename T>");
299 writeln!(out, "union MaybeUninit {{");
300 writeln!(out, " T value;");
301 writeln!(out, " MaybeUninit() {{}}");
302 writeln!(out, " ~MaybeUninit() {{}}");
303 writeln!(out, "}};");
304 }
305
David Tolnayd68dfa82020-10-31 16:01:24 -0700306 out.begin_block("namespace");
307
David Tolnay3be0e1f2020-10-31 20:53:00 -0700308 if out.builtin.trycatch
309 || out.builtin.rust_error
310 || out.builtin.rust_str_new_unchecked
311 || out.builtin.rust_str_repr
312 {
David Tolnayd68dfa82020-10-31 16:01:24 -0700313 out.begin_block("namespace repr");
314 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700315 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700316 writeln!(out, " size_t len;");
317 writeln!(out, "}};");
318 out.end_block("namespace repr");
319 }
320
David Tolnay3be0e1f2020-10-31 20:53:00 -0700321 if out.builtin.rust_str_new_unchecked || out.builtin.rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700322 out.next_section();
323 writeln!(out, "template <>");
324 writeln!(out, "class impl<Str> final {{");
325 writeln!(out, "public:");
David Tolnay3be0e1f2020-10-31 20:53:00 -0700326 if out.builtin.rust_str_new_unchecked {
David Tolnaya4eb9432020-10-31 20:32:19 -0700327 writeln!(
328 out,
329 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
330 );
331 writeln!(out, " Str str;");
332 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
333 writeln!(out, " str.len = repr.len;");
334 writeln!(out, " return str;");
335 writeln!(out, " }}");
336 }
David Tolnay3be0e1f2020-10-31 20:53:00 -0700337 if out.builtin.rust_str_repr {
David Tolnaya4eb9432020-10-31 20:32:19 -0700338 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
339 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
340 writeln!(out, " }}");
341 }
David Tolnay0356d332020-10-31 19:46:41 -0700342 writeln!(out, "}};");
343 }
344
David Tolnay3be0e1f2020-10-31 20:53:00 -0700345 if out.builtin.rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700346 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700347 writeln!(out, "template <>");
348 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700349 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700350 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700351 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700352 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700353 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700354 writeln!(out, " return error;");
355 writeln!(out, " }}");
356 writeln!(out, "}};");
357 }
358
David Tolnayd68dfa82020-10-31 16:01:24 -0700359 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700360 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700361
David Tolnay3be0e1f2020-10-31 20:53:00 -0700362 if out.builtin.trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700363 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700364 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700365 out.include.type_traits = true;
366 out.include.utility = true;
367 writeln!(out, "class missing {{}};");
368 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700369 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700370 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700371 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700372 writeln!(
373 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700374 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700375 );
David Tolnay04722332020-03-18 11:31:54 -0700376 writeln!(out, " missing>::value>::type");
377 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700378 writeln!(out, " func();");
379 writeln!(out, "}} catch (const ::std::exception &e) {{");
380 writeln!(out, " fail(e.what());");
381 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700382 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700383 }
384
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800385 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400386}
387
David Tolnaya7c2ea12020-10-30 21:32:53 -0700388fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700389 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700390 writeln!(out, "#ifndef {}", guard);
391 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400392 for line in strct.doc.to_string().lines() {
393 writeln!(out, "//{}", line);
394 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700395 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400396 for field in &strct.fields {
397 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700398 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400399 writeln!(out, "{};", field.ident);
400 }
401 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700402 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400403}
404
405fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
406 writeln!(out, "struct {};", ident);
407}
408
Adrian Taylorc8713432020-10-21 18:20:55 -0700409fn write_struct_using(out: &mut OutFile, ident: &CppName) {
410 writeln!(
411 out,
412 "using {} = {};",
413 ident.ident,
414 ident.to_fully_qualified()
415 );
David Tolnay8861bee2020-01-20 18:39:24 -0800416}
417
David Tolnaya7c2ea12020-10-30 21:32:53 -0700418fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700419 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700420 writeln!(out, "#ifndef {}", guard);
421 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700422 for line in ety.doc.to_string().lines() {
423 writeln!(out, "//{}", line);
424 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700425 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
426 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
427 writeln!(
428 out,
429 " {}(const {} &) = delete;",
430 ety.ident.cxx.ident, ety.ident.cxx.ident
431 );
Joel Galenson968738f2020-04-15 14:19:33 -0700432 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700433 write!(out, " ");
434 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700435 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700436 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700437 writeln!(out, ";");
438 }
439 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700440 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700441}
442
Joel Galensonc03402a2020-04-23 17:31:09 -0700443fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700444 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700445 writeln!(out, "#ifndef {}", guard);
446 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700447 for line in enm.doc.to_string().lines() {
448 writeln!(out, "//{}", line);
449 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700450 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700451 write_atom(out, enm.repr);
452 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700453 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700454 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700455 }
456 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700457 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700458}
459
Joel Galenson905eb2e2020-05-04 14:58:14 -0700460fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700461 write!(
462 out,
463 "static_assert(sizeof({}) == sizeof(",
464 enm.ident.cxx.ident
465 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700466 write_atom(out, enm.repr);
467 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700468 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700469 write!(out, "static_assert(static_cast<");
470 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700471 writeln!(
472 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700473 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700474 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700475 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700476 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700477}
478
Adrian Taylorc8713432020-10-21 18:20:55 -0700479fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700480 // NOTE: The following two static assertions are just nice-to-have and not
481 // necessary for soundness. That's because triviality is always declared by
482 // the user in the form of an unsafe impl of cxx::ExternType:
483 //
484 // unsafe impl ExternType for MyType {
485 // type Id = cxx::type_id!("...");
486 // type Kind = cxx::kind::Trivial;
487 // }
488 //
489 // Since the user went on the record with their unsafe impl to unsafely
490 // claim they KNOW that the type is trivial, it's fine for that to be on
491 // them if that were wrong.
492 //
493 // There may be a legitimate reason we'll want to remove these assertions
494 // for support of types that the programmer knows are Rust-movable despite
495 // not being recognized as such by the C++ type system due to a move
496 // constructor or destructor.
497
Adrian Taylorc8713432020-10-21 18:20:55 -0700498 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700499 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700500 writeln!(out, "static_assert(");
501 writeln!(
502 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700503 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700504 id,
505 );
506 writeln!(
507 out,
508 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
509 id,
510 );
511 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700512 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700513 writeln!(
514 out,
515 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
516 id,
517 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700518}
519
Adrian Taylorc8713432020-10-21 18:20:55 -0700520fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700521 let mut has_cxx_throws = false;
522 for api in apis {
523 if let Api::CxxFunction(efn) = api {
524 if efn.throws {
525 has_cxx_throws = true;
526 break;
527 }
528 }
529 }
530
531 if has_cxx_throws {
532 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700533 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700534 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700535 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700536 );
537 }
538}
539
David Tolnaya7c2ea12020-10-30 21:32:53 -0700540fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700541 if let Some(annotation) = impl_annotations {
542 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700543 }
David Tolnayebef4a22020-03-17 15:33:47 -0700544 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700545 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700546 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700547 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700548 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700549 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700550 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700551 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700552 if receiver.mutability.is_none() {
553 write!(out, "const ");
554 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700555 write!(
556 out,
557 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700558 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700559 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700560 }
David Tolnay7db73692019-10-20 14:51:12 -0400561 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700562 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400563 write!(out, ", ");
564 }
David Tolnaya46a2372020-03-06 10:03:48 -0800565 if arg.ty == RustString {
566 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700567 } else if let Type::RustVec(_) = arg.ty {
568 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800569 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700570 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400571 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700572 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400573 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700574 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400575 write!(out, ", ");
576 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700577 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400578 write!(out, "*return$");
579 }
580 writeln!(out, ") noexcept {{");
581 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700582 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700583 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700584 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700585 Some(receiver) => write!(
586 out,
587 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700588 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700589 efn.ident.rust
590 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700591 }
David Tolnay7db73692019-10-20 14:51:12 -0400592 for (i, arg) in efn.args.iter().enumerate() {
593 if i > 0 {
594 write!(out, ", ");
595 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700596 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400597 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700598 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700599 if let Some(receiver) = &efn.receiver {
600 if receiver.mutability.is_none() {
601 write!(out, " const");
602 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700603 }
604 write!(out, " = ");
605 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700606 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
607 Some(receiver) => write!(
608 out,
609 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700610 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700611 efn.ident.cxx.ident
612 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700613 }
614 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400615 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700616 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700617 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700618 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700619 writeln!(out, " [&] {{");
620 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700621 }
David Tolnay7db73692019-10-20 14:51:12 -0400622 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700623 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400624 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700625 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400626 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700627 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400628 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700629 }
630 match &efn.ret {
631 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700632 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700633 Some(Type::SliceRefU8(_)) if !indirect_return => {
634 write!(out, "::rust::Slice<uint8_t>::Repr(")
635 }
David Tolnay99642622020-03-25 13:07:35 -0700636 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400637 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700638 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700639 None => write!(out, "{}$(", efn.ident.rust),
640 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700641 }
David Tolnay7db73692019-10-20 14:51:12 -0400642 for (i, arg) in efn.args.iter().enumerate() {
643 if i > 0 {
644 write!(out, ", ");
645 }
646 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700647 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400648 write!(out, "::from_raw({})", arg.ident);
649 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700650 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400651 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700652 } else if let Type::Str(_) = arg.ty {
653 write!(
654 out,
655 "::rust::impl<::rust::Str>::new_unchecked({})",
656 arg.ident,
657 );
David Tolnaya46a2372020-03-06 10:03:48 -0800658 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800659 write!(
660 out,
661 "::rust::String(::rust::unsafe_bitcopy, *{})",
662 arg.ident,
663 );
David Tolnay313b10e2020-04-25 16:30:51 -0700664 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700665 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700666 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700667 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700668 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800669 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400670 } else {
671 write!(out, "{}", arg.ident);
672 }
673 }
674 write!(out, ")");
675 match &efn.ret {
676 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
677 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700678 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400679 _ => {}
680 }
681 if indirect_return {
682 write!(out, ")");
683 }
684 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700685 if efn.throws {
686 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700687 writeln!(out, " throw$.ptr = nullptr;");
688 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700689 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700690 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700691 writeln!(
692 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700693 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700694 );
David Tolnay5d121442020-03-17 22:14:40 -0700695 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700696 writeln!(out, " return throw$;");
697 }
David Tolnay7db73692019-10-20 14:51:12 -0400698 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700699 for arg in &efn.args {
700 if let Type::Fn(f) = &arg.ty {
701 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700702 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700703 }
704 }
705}
706
707fn write_function_pointer_trampoline(
708 out: &mut OutFile,
709 efn: &ExternFn,
710 var: &Ident,
711 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700712) {
713 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700714 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700715 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700716 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700717
718 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700719 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
720 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400721}
722
David Tolnaya7c2ea12020-10-30 21:32:53 -0700723fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
724 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700725 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700726 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700727}
728
729fn write_rust_function_decl_impl(
730 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700731 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700732 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700733 indirect_call: bool,
734) {
735 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700736 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700737 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700738 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700739 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700740 write!(out, "{}(", link_name);
741 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700742 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700743 if receiver.mutability.is_none() {
744 write!(out, "const ");
745 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700746 write!(
747 out,
748 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700749 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700750 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700751 needs_comma = true;
752 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700753 for arg in &sig.args {
754 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400755 write!(out, ", ");
756 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700757 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700758 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400759 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700760 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700761 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400762 write!(out, ", ");
763 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700764 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400765 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700766 needs_comma = true;
767 }
768 if indirect_call {
769 if needs_comma {
770 write!(out, ", ");
771 }
772 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400773 }
774 writeln!(out, ") noexcept;");
775}
776
David Tolnaya7c2ea12020-10-30 21:32:53 -0700777fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400778 for line in efn.doc.to_string().lines() {
779 writeln!(out, "//{}", line);
780 }
David Tolnaya73853b2020-04-20 01:19:56 -0700781 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700782 None => efn.ident.cxx.ident.to_string(),
783 Some(receiver) => format!(
784 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700785 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700786 efn.ident.cxx.ident
787 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700788 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700789 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700790 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700791 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700792}
793
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700794fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700795 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700796 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700797 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700798 indirect_call: bool,
799) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700800 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700801 write!(out, "{}(", local_name);
802 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400803 if i > 0 {
804 write!(out, ", ");
805 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700806 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400807 write!(out, "{}", arg.ident);
808 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700809 if indirect_call {
810 if !sig.args.is_empty() {
811 write!(out, ", ");
812 }
813 write!(out, "void *extern$");
814 }
David Tolnay1e548172020-03-16 13:37:09 -0700815 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700816 if let Some(receiver) = &sig.receiver {
817 if receiver.mutability.is_none() {
818 write!(out, " const");
819 }
820 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700821 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700822 write!(out, " noexcept");
823 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700824}
825
826fn write_rust_function_shim_impl(
827 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700828 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700829 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700830 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700831 indirect_call: bool,
832) {
833 if out.header && sig.receiver.is_some() {
834 // We've already defined this inside the struct.
835 return;
836 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700837 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400838 if out.header {
839 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700840 return;
David Tolnay7db73692019-10-20 14:51:12 -0400841 }
David Tolnay439cde22020-04-20 00:46:25 -0700842 writeln!(out, " {{");
843 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700844 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700845 out.include.utility = true;
846 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700847 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700848 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
849 }
850 }
851 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700852 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700853 if indirect_return {
854 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700855 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700856 writeln!(out, "> return$;");
857 write!(out, " ");
858 } else if let Some(ret) = &sig.ret {
859 write!(out, "return ");
860 match ret {
861 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700862 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700863 write!(out, "::from_raw(");
864 }
865 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700866 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700867 write!(out, "(");
868 }
869 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700870 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700871 _ => {}
872 }
873 }
874 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700875 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700876 }
877 write!(out, "{}(", invoke);
878 if sig.receiver.is_some() {
879 write!(out, "*this");
880 }
881 for (i, arg) in sig.args.iter().enumerate() {
882 if i > 0 || sig.receiver.is_some() {
883 write!(out, ", ");
884 }
885 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700886 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700887 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700888 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700889 _ => {}
890 }
891 write!(out, "{}", arg.ident);
892 match &arg.ty {
893 Type::RustBox(_) => write!(out, ".into_raw()"),
894 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700895 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700896 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700897 _ => {}
898 }
899 }
900 if indirect_return {
901 if !sig.args.is_empty() {
902 write!(out, ", ");
903 }
904 write!(out, "&return$.value");
905 }
906 if indirect_call {
907 if !sig.args.is_empty() || indirect_return {
908 write!(out, ", ");
909 }
910 write!(out, "extern$");
911 }
912 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400913 if !indirect_return {
914 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700915 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400916 write!(out, ")");
917 }
David Tolnay439cde22020-04-20 00:46:25 -0700918 }
919 }
920 writeln!(out, ";");
921 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700922 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700923 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700924 writeln!(out, " }}");
925 }
926 if indirect_return {
927 out.include.utility = true;
928 writeln!(out, " return ::std::move(return$.value);");
929 }
930 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400931}
932
David Tolnaya7c2ea12020-10-30 21:32:53 -0700933fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400934 match ty {
935 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700936 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400937 }
938}
939
David Tolnay75dca2e2020-03-25 20:17:52 -0700940fn indirect_return(sig: &Signature, types: &Types) -> bool {
941 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700942 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700943 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700944}
945
David Tolnaya7c2ea12020-10-30 21:32:53 -0700946fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700947 match ty {
948 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700949 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700950 write!(out, "*");
951 }
952 Type::Ref(ty) => {
953 if ty.mutability.is_none() {
954 write!(out, "const ");
955 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700956 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700957 write!(out, " *");
958 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700959 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700960 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700961 }
962}
963
David Tolnaya7c2ea12020-10-30 21:32:53 -0700964fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
965 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700966 match ty {
967 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700968 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700969 _ => write_space_after_type(out, ty),
970 }
971}
972
David Tolnaya7c2ea12020-10-30 21:32:53 -0700973fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400974 match ty {
975 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700976 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400977 write!(out, "*");
978 }
David Tolnay4a441222020-01-25 16:24:27 -0800979 Some(Type::Ref(ty)) => {
980 if ty.mutability.is_none() {
981 write!(out, "const ");
982 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700983 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -0800984 write!(out, " *");
985 }
David Tolnay0356d332020-10-31 19:46:41 -0700986 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700987 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700988 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
989 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400990 }
991}
992
David Tolnaya7c2ea12020-10-30 21:32:53 -0700993fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -0400994 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700995 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700996 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400997 write!(out, "*");
998 }
David Tolnay0356d332020-10-31 19:46:41 -0700999 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001000 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001001 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001002 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001003 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001004 write!(out, "*");
1005 }
1006 write!(out, "{}", arg.ident);
1007}
1008
David Tolnaya7c2ea12020-10-30 21:32:53 -07001009fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001010 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001011 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001012 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001013 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001014 },
1015 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001016 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001017 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001018 write!(out, ">");
1019 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001020 Type::RustVec(ty) => {
1021 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001022 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001023 write!(out, ">");
1024 }
David Tolnay7db73692019-10-20 14:51:12 -04001025 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001026 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001027 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001028 write!(out, ">");
1029 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001030 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001031 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001032 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001033 write!(out, ">");
1034 }
David Tolnay7db73692019-10-20 14:51:12 -04001035 Type::Ref(r) => {
1036 if r.mutability.is_none() {
1037 write!(out, "const ");
1038 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001039 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001040 write!(out, " &");
1041 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001042 Type::Slice(_) => {
1043 // For now, only U8 slices are supported, which are covered separately below
1044 unreachable!()
1045 }
David Tolnay7db73692019-10-20 14:51:12 -04001046 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001047 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001048 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001049 Type::SliceRefU8(_) => {
1050 write!(out, "::rust::Slice<uint8_t>");
1051 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001052 Type::Fn(f) => {
1053 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1054 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001055 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001056 None => write!(out, "void"),
1057 }
1058 write!(out, "(");
1059 for (i, arg) in f.args.iter().enumerate() {
1060 if i > 0 {
1061 write!(out, ", ");
1062 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001063 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001064 }
1065 write!(out, ")>");
1066 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001067 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001068 }
1069}
1070
David Tolnayf6a89f22020-05-10 23:39:27 -07001071fn write_atom(out: &mut OutFile, atom: Atom) {
1072 match atom {
1073 Bool => write!(out, "bool"),
1074 U8 => write!(out, "uint8_t"),
1075 U16 => write!(out, "uint16_t"),
1076 U32 => write!(out, "uint32_t"),
1077 U64 => write!(out, "uint64_t"),
1078 Usize => write!(out, "size_t"),
1079 I8 => write!(out, "int8_t"),
1080 I16 => write!(out, "int16_t"),
1081 I32 => write!(out, "int32_t"),
1082 I64 => write!(out, "int64_t"),
1083 Isize => write!(out, "::rust::isize"),
1084 F32 => write!(out, "float"),
1085 F64 => write!(out, "double"),
1086 CxxString => write!(out, "::std::string"),
1087 RustString => write!(out, "::rust::String"),
1088 }
1089}
1090
David Tolnaya7c2ea12020-10-30 21:32:53 -07001091fn write_type_space(out: &mut OutFile, ty: &Type) {
1092 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001093 write_space_after_type(out, ty);
1094}
1095
1096fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001097 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001098 Type::Ident(_)
1099 | Type::RustBox(_)
1100 | Type::UniquePtr(_)
1101 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001102 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001103 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001104 | Type::SliceRefU8(_)
1105 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001106 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001107 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001108 }
1109}
1110
David Tolnaycd08c442020-04-25 10:16:33 -07001111// Only called for legal referent types of unique_ptr and element types of
1112// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001113fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001114 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001115 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1116 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001117 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001118 }
1119}
1120
David Tolnayacdf20a2020-04-25 12:40:53 -07001121// Only called for legal referent types of unique_ptr and element types of
1122// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001123fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001124 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001125 Type::Ident(ident) => ident.to_symbol(types),
1126 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001127 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001128 }
1129}
1130
David Tolnaya7c2ea12020-10-30 21:32:53 -07001131fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001132 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001133 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001134 if let Type::RustBox(ty) = ty {
1135 if let Type::Ident(inner) = &ty.inner {
1136 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001137 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001138 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001139 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001140 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001141 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001142 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001143 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001144 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001145 }
David Tolnay7db73692019-10-20 14:51:12 -04001146 } else if let Type::UniquePtr(ptr) = ty {
1147 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001148 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001149 && (!out.types.aliases.contains_key(&inner.rust)
1150 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001151 {
David Tolnay7db73692019-10-20 14:51:12 -04001152 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001153 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001154 }
1155 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001156 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001157 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001158 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001159 && (!out.types.aliases.contains_key(&inner.rust)
1160 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001161 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001162 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001163 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001164 }
1165 }
1166 }
1167 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001168 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001169
David Tolnay750755e2020-03-01 13:04:08 -08001170 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001171 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001172 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001173 if let Type::RustBox(ty) = ty {
1174 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001175 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001176 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001177 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001178 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001179 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001180 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001181 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001182 }
David Tolnay7db73692019-10-20 14:51:12 -04001183 }
1184 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001185 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001186 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001187}
1188
Adrian Taylorc8713432020-10-21 18:20:55 -07001189fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1190 let inner = ident.to_fully_qualified();
1191 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001192
David Tolnay8f16ae72020-10-08 18:21:13 -07001193 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1194 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001195 writeln!(
1196 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001197 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001198 instance, inner,
1199 );
1200 writeln!(
1201 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001202 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001203 instance, inner,
1204 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001205 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001206}
1207
David Tolnaya7c2ea12020-10-30 21:32:53 -07001208fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001209 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001210 let inner = to_typename(&element, out.types);
1211 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001212
David Tolnay8f16ae72020-10-08 18:21:13 -07001213 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1214 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001215 writeln!(
1216 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001217 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001218 instance, inner,
1219 );
1220 writeln!(
1221 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001222 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001223 instance, inner,
1224 );
1225 writeln!(
1226 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001227 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001228 instance, inner,
1229 );
David Tolnay219c0792020-04-24 20:31:37 -07001230 writeln!(
1231 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001232 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001233 inner, instance,
1234 );
David Tolnay503d0192020-04-24 22:18:56 -07001235 writeln!(
1236 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001237 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001238 instance,
1239 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001240 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001241}
1242
Adrian Taylorc8713432020-10-21 18:20:55 -07001243fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1244 let inner = ident.to_fully_qualified();
1245 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001246
1247 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001248 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001249 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001250 writeln!(out, "}}");
1251
1252 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001253 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001254 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001255 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001256}
1257
David Tolnaya7c2ea12020-10-30 21:32:53 -07001258fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001259 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001260 let inner = to_typename(&element, out.types);
1261 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001262
Myron Ahneba35cf2020-02-05 19:41:51 +07001263 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001264 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001265 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001266 writeln!(out, "}}");
1267
1268 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001269 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1270 writeln!(
1271 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001272 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001273 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001274 );
1275 writeln!(out, "}}");
1276
1277 writeln!(out, "template <>");
1278 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001279 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001280 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001281
1282 writeln!(out, "template <>");
1283 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1284 writeln!(
1285 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001286 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001287 instance,
1288 );
1289 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001290
1291 writeln!(out, "template <>");
1292 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001293 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001294 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001295}
1296
David Tolnaya7c2ea12020-10-30 21:32:53 -07001297fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001298 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001299 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001300
David Tolnay8f16ae72020-10-08 18:21:13 -07001301 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1302 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001303
David Tolnaya7c2ea12020-10-30 21:32:53 -07001304 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001305
David Tolnay8f16ae72020-10-08 18:21:13 -07001306 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001307}
1308
1309// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001310fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001311 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001312 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001313 let inner = to_typename(ty, out.types);
1314 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001315
David Tolnay63da4d32020-04-25 09:41:12 -07001316 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001317 // Some aliases are to opaque types; some are to trivial types. We can't
1318 // know at code generation time, so we generate both C++ and Rust side
1319 // bindings for a "new" method anyway. But the Rust code can't be called
1320 // for Opaque types because the 'new' method is not implemented.
1321 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001322 out.types.structs.contains_key(&ident.rust)
1323 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001324 }
David Tolnay63da4d32020-04-25 09:41:12 -07001325 _ => false,
1326 };
1327
David Tolnay7db73692019-10-20 14:51:12 -04001328 writeln!(
1329 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001330 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001331 inner,
1332 );
1333 writeln!(
1334 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001335 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001336 inner,
1337 );
1338 writeln!(
1339 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001340 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001341 instance, inner,
1342 );
David Tolnay7e219b82020-03-01 13:14:51 -08001343 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001344 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001345 if can_construct_from_value {
1346 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001347 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001348 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001349 instance, inner, inner,
1350 );
David Tolnay63da4d32020-04-25 09:41:12 -07001351 writeln!(
1352 out,
1353 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1354 inner, inner,
1355 );
1356 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001357 }
David Tolnay7db73692019-10-20 14:51:12 -04001358 writeln!(
1359 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001360 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001361 instance, inner, inner,
1362 );
David Tolnay7e219b82020-03-01 13:14:51 -08001363 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001364 writeln!(out, "}}");
1365 writeln!(
1366 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001367 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001368 inner, instance, inner,
1369 );
1370 writeln!(out, " return ptr.get();");
1371 writeln!(out, "}}");
1372 writeln!(
1373 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001374 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001375 inner, instance, inner,
1376 );
1377 writeln!(out, " return ptr.release();");
1378 writeln!(out, "}}");
1379 writeln!(
1380 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001381 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001382 instance, inner,
1383 );
1384 writeln!(out, " ptr->~unique_ptr();");
1385 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001386}
Myron Ahneba35cf2020-02-05 19:41:51 +07001387
David Tolnaya7c2ea12020-10-30 21:32:53 -07001388fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001389 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001390 let inner = to_typename(&element, out.types);
1391 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001392
David Tolnay8f16ae72020-10-08 18:21:13 -07001393 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1394 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001395 writeln!(
1396 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001397 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001398 instance, inner,
1399 );
1400 writeln!(out, " return s.size();");
1401 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001402 writeln!(
1403 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001404 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001405 inner, instance, inner,
1406 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001407 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001408 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001409
David Tolnaya7c2ea12020-10-30 21:32:53 -07001410 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001411
David Tolnay8f16ae72020-10-08 18:21:13 -07001412 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001413}