blob: 99d5e7d17d75da7276b4d71c36404361dd1acf5a [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 Tolnaya7c2ea12020-10-30 21:32:53 -070028 write_includes(out);
29 write_include_cxxbridge(out, apis);
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
Adrian Taylorf9213622020-10-31 22:25:42 -070035 gen_namespace_forward_declarations(&apis_by_namespace, out);
Adrian Taylorc8713432020-10-21 18:20:55 -070036 gen_namespace_contents(&apis_by_namespace, types, opt, header, out);
37
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
Adrian Taylorf9213622020-10-31 22:25:42 -070048fn gen_namespace_forward_declarations(ns_entries: &NamespaceEntries, out: &mut OutFile) {
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);
65 gen_namespace_forward_declarations(&child_ns_entries, out);
66 writeln!(out, "}} // namespace {}", child_ns);
67 }
68}
69
70fn gen_namespace_contents(
71 ns_entries: &NamespaceEntries,
72 types: &Types,
73 opt: &Opt,
74 header: bool,
75 out: &mut OutFile,
76) {
77 let apis = ns_entries.entries();
78
David Tolnayf94bef12020-04-17 14:46:42 -070079 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070080 for api in apis {
David Tolnayf94bef12020-04-17 14:46:42 -070081 if let Api::RustFunction(efn) = api {
82 if let Some(receiver) = &efn.sig.receiver {
83 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070084 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070085 .or_insert_with(Vec::new)
86 .push(efn);
87 }
88 }
89 }
Joel Galenson968738f2020-04-15 14:19:33 -070090
David Tolnay7db73692019-10-20 14:51:12 -040091 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070092 match api {
93 Api::Struct(strct) => {
94 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070095 if !types.cxx.contains(&strct.ident.rust) {
David Tolnaya7c2ea12020-10-30 21:32:53 -070096 write_struct(out, strct);
David Tolnaya593d6e2020-08-29 19:48:08 -070097 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070098 }
Joel Galensonc03402a2020-04-23 17:31:09 -070099 Api::Enum(enm) => {
100 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700101 if types.cxx.contains(&enm.ident.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700102 check_enum(out, enm);
103 } else {
104 write_enum(out, enm);
105 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700106 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700107 Api::RustType(ety) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700108 if let Some(methods) = methods_for_type.get(&ety.ident.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -0700109 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700110 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700111 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700112 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700113 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400114 }
115 }
116
David Tolnayfabca772020-10-03 21:25:41 -0700117 out.next_section();
118 for api in apis {
119 if let Api::TypeAlias(ety) = api {
Adrian Taylorc8713432020-10-21 18:20:55 -0700120 if types.required_trivial.contains_key(&ety.ident.rust) {
121 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700122 }
123 }
124 }
125
David Tolnay7db73692019-10-20 14:51:12 -0400126 if !header {
127 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700128 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400129 for api in apis {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700130 let (efn, write): (_, fn(_, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400131 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
132 Api::RustFunction(efn) => (efn, write_rust_function_decl),
133 _ => continue,
134 };
135 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700136 write(out, efn, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400137 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800138 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400139 }
140
141 for api in apis {
142 if let Api::RustFunction(efn) = api {
143 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700144 write_rust_function_shim(out, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400145 }
146 }
147
148 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700149
Adrian Taylor565ddf02020-10-29 21:12:36 -0700150 for (child_ns, child_ns_entries) in ns_entries.children() {
Adrian Taylorc8713432020-10-21 18:20:55 -0700151 writeln!(out, "namespace {} {{", child_ns);
152 gen_namespace_contents(&child_ns_entries, types, opt, header, out);
153 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400154 }
David Tolnay7db73692019-10-20 14:51:12 -0400155}
156
David Tolnaya7c2ea12020-10-30 21:32:53 -0700157fn write_includes(out: &mut OutFile) {
158 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400159 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700160 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700161 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
162 | Some(I64) => out.include.cstdint = true,
163 Some(Usize) => out.include.cstddef = true,
164 Some(CxxString) => out.include.string = true,
David Tolnayf57f7562020-10-04 19:56:26 -0700165 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700166 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800167 Type::RustBox(_) => out.include.type_traits = true,
168 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700169 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700170 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700171 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400172 }
173 }
David Tolnay7db73692019-10-20 14:51:12 -0400174}
175
David Tolnaya7c2ea12020-10-30 21:32:53 -0700176fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api]) {
David Tolnay16ab1462020-09-02 15:10:09 -0700177 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 let mut needs_rust_string = false;
179 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700180 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400181 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700182 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700183 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700184 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700185 let mut needs_unsafe_bitcopy = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700186 for ty in out.types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700187 match ty {
188 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700189 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700190 out.include.type_traits = true;
191 needs_rust_box = true;
192 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700193 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700194 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700195 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700196 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700197 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700198 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700199 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700200 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700201 Type::Str(_) => {
202 out.include.cstdint = true;
203 out.include.string = true;
204 needs_rust_str = true;
205 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700206 Type::Fn(_) => {
207 needs_rust_fn = true;
208 }
David Tolnay4770b472020-04-14 16:32:59 -0700209 Type::Slice(_) | Type::SliceRefU8(_) => {
210 needs_rust_slice = true;
211 }
David Tolnay7c295462020-04-25 12:45:07 -0700212 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400213 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700214 needs_rust_isize = true;
215 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700216 ty if ty == RustString => {
217 out.include.array = true;
218 out.include.cstdint = true;
219 out.include.string = true;
220 needs_rust_string = true;
221 }
222 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400223 }
224 }
225
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800227 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800228 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700229 let mut needs_trycatch = false;
David Tolnaya4eb9432020-10-31 20:32:19 -0700230 let mut needs_rust_str_new_unchecked = false;
231 let mut needs_rust_str_repr = false;
David Tolnay09011c32020-03-06 14:40:28 -0800232 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 match api {
234 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700235 if efn.throws {
236 needs_trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700237 } else if let Some(Type::Str(_)) = efn.ret {
238 needs_rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700239 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700240 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700241 match arg.ty {
242 Type::Str(_) => needs_rust_str_new_unchecked = true,
243 Type::RustVec(_) => needs_unsafe_bitcopy = true,
244 _ => needs_unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 }
David Tolnay09011c32020-03-06 14:40:28 -0800246 }
247 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700248 Api::RustFunction(efn) if !out.header => {
249 if efn.throws {
250 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700251 out.include.string = true;
252 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700253 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200254 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700255 }
256 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700257 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700258 needs_manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700259 }
260 if let Type::Str(_) = arg.ty {
261 needs_rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700262 }
263 }
264 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700265 if out.types.needs_indirect_abi(ret) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700266 needs_maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700267 } else if let Type::Str(_) = ret {
268 needs_rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700269 }
David Tolnayf51447e2020-03-06 14:14:27 -0800270 }
271 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700272 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800273 }
274 }
275
David Tolnay750755e2020-03-01 13:04:08 -0800276 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700277 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800278
David Tolnay16ab1462020-09-02 15:10:09 -0700279 if needs_panic
280 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700281 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700282 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700283 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700284 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700285 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700286 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700287 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700288 || needs_unsafe_bitcopy
289 || needs_manually_drop
290 || needs_maybe_uninit
291 {
David Tolnay736cbca2020-03-11 16:49:18 -0700292 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800293 }
294
David Tolnay8f16ae72020-10-08 18:21:13 -0700295 include::write(out, needs_panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700296
David Tolnay99a95e62020-09-02 15:05:53 -0700297 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700298 out.next_section();
299 writeln!(out, "struct unsafe_bitcopy_t;");
300 }
301
David Tolnay84ddf9e2020-10-31 15:36:48 -0700302 if needs_rust_error {
303 out.begin_block("namespace");
304 writeln!(out, "template <typename T>");
305 writeln!(out, "class impl;");
306 out.end_block("namespace");
307 }
308
David Tolnay8f16ae72020-10-08 18:21:13 -0700309 include::write(out, needs_rust_string, "CXXBRIDGE05_RUST_STRING");
310 include::write(out, needs_rust_str, "CXXBRIDGE05_RUST_STR");
311 include::write(out, needs_rust_slice, "CXXBRIDGE05_RUST_SLICE");
312 include::write(out, needs_rust_box, "CXXBRIDGE05_RUST_BOX");
313 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
314 include::write(out, needs_rust_vec, "CXXBRIDGE05_RUST_VEC");
315 include::write(out, needs_rust_fn, "CXXBRIDGE05_RUST_FN");
316 include::write(out, needs_rust_error, "CXXBRIDGE05_RUST_ERROR");
317 include::write(out, needs_rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800318
319 if needs_manually_drop {
320 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700321 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800322 writeln!(out, "template <typename T>");
323 writeln!(out, "union ManuallyDrop {{");
324 writeln!(out, " T value;");
325 writeln!(
326 out,
327 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
328 );
329 writeln!(out, " ~ManuallyDrop() {{}}");
330 writeln!(out, "}};");
331 }
332
David Tolnay09011c32020-03-06 14:40:28 -0800333 if needs_maybe_uninit {
334 out.next_section();
335 writeln!(out, "template <typename T>");
336 writeln!(out, "union MaybeUninit {{");
337 writeln!(out, " T value;");
338 writeln!(out, " MaybeUninit() {{}}");
339 writeln!(out, " ~MaybeUninit() {{}}");
340 writeln!(out, "}};");
341 }
342
David Tolnayd68dfa82020-10-31 16:01:24 -0700343 out.begin_block("namespace");
344
David Tolnaya4eb9432020-10-31 20:32:19 -0700345 if needs_trycatch || needs_rust_error || needs_rust_str_new_unchecked || needs_rust_str_repr {
David Tolnayd68dfa82020-10-31 16:01:24 -0700346 out.begin_block("namespace repr");
347 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700348 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700349 writeln!(out, " size_t len;");
350 writeln!(out, "}};");
351 out.end_block("namespace repr");
352 }
353
David Tolnaya4eb9432020-10-31 20:32:19 -0700354 if needs_rust_str_new_unchecked || needs_rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700355 out.next_section();
356 writeln!(out, "template <>");
357 writeln!(out, "class impl<Str> final {{");
358 writeln!(out, "public:");
David Tolnaya4eb9432020-10-31 20:32:19 -0700359 if needs_rust_str_new_unchecked {
360 writeln!(
361 out,
362 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
363 );
364 writeln!(out, " Str str;");
365 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
366 writeln!(out, " str.len = repr.len;");
367 writeln!(out, " return str;");
368 writeln!(out, " }}");
369 }
370 if needs_rust_str_repr {
371 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
372 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
373 writeln!(out, " }}");
374 }
David Tolnay0356d332020-10-31 19:46:41 -0700375 writeln!(out, "}};");
376 }
377
David Tolnaya0c9bc72020-10-31 14:37:14 -0700378 if needs_rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700379 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700380 writeln!(out, "template <>");
381 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700382 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700383 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700384 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700385 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700386 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700387 writeln!(out, " return error;");
388 writeln!(out, " }}");
389 writeln!(out, "}};");
390 }
391
David Tolnayd68dfa82020-10-31 16:01:24 -0700392 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700393 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700394
David Tolnay5d121442020-03-17 22:14:40 -0700395 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700396 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700397 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700398 out.include.type_traits = true;
399 out.include.utility = true;
400 writeln!(out, "class missing {{}};");
401 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700402 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700403 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700404 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700405 writeln!(
406 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700407 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700408 );
David Tolnay04722332020-03-18 11:31:54 -0700409 writeln!(out, " missing>::value>::type");
410 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700411 writeln!(out, " func();");
412 writeln!(out, "}} catch (const ::std::exception &e) {{");
413 writeln!(out, " fail(e.what());");
414 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700415 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700416 }
417
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800418 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400419}
420
David Tolnaya7c2ea12020-10-30 21:32:53 -0700421fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700422 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700423 writeln!(out, "#ifndef {}", guard);
424 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400425 for line in strct.doc.to_string().lines() {
426 writeln!(out, "//{}", line);
427 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700428 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400429 for field in &strct.fields {
430 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700431 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400432 writeln!(out, "{};", field.ident);
433 }
434 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700435 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400436}
437
438fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
439 writeln!(out, "struct {};", ident);
440}
441
Adrian Taylorc8713432020-10-21 18:20:55 -0700442fn write_struct_using(out: &mut OutFile, ident: &CppName) {
443 writeln!(
444 out,
445 "using {} = {};",
446 ident.ident,
447 ident.to_fully_qualified()
448 );
David Tolnay8861bee2020-01-20 18:39:24 -0800449}
450
David Tolnaya7c2ea12020-10-30 21:32:53 -0700451fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700452 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700453 writeln!(out, "#ifndef {}", guard);
454 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700455 for line in ety.doc.to_string().lines() {
456 writeln!(out, "//{}", line);
457 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700458 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
459 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
460 writeln!(
461 out,
462 " {}(const {} &) = delete;",
463 ety.ident.cxx.ident, ety.ident.cxx.ident
464 );
Joel Galenson968738f2020-04-15 14:19:33 -0700465 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700466 write!(out, " ");
467 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700468 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700469 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700470 writeln!(out, ";");
471 }
472 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700473 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700474}
475
Joel Galensonc03402a2020-04-23 17:31:09 -0700476fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700477 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700478 writeln!(out, "#ifndef {}", guard);
479 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700480 for line in enm.doc.to_string().lines() {
481 writeln!(out, "//{}", line);
482 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700483 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700484 write_atom(out, enm.repr);
485 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700486 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700487 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700488 }
489 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700490 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700491}
492
Joel Galenson905eb2e2020-05-04 14:58:14 -0700493fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700494 write!(
495 out,
496 "static_assert(sizeof({}) == sizeof(",
497 enm.ident.cxx.ident
498 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700499 write_atom(out, enm.repr);
500 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700501 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700502 write!(out, "static_assert(static_cast<");
503 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700504 writeln!(
505 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700506 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700507 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700508 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700509 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700510}
511
Adrian Taylorc8713432020-10-21 18:20:55 -0700512fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700513 // NOTE: The following two static assertions are just nice-to-have and not
514 // necessary for soundness. That's because triviality is always declared by
515 // the user in the form of an unsafe impl of cxx::ExternType:
516 //
517 // unsafe impl ExternType for MyType {
518 // type Id = cxx::type_id!("...");
519 // type Kind = cxx::kind::Trivial;
520 // }
521 //
522 // Since the user went on the record with their unsafe impl to unsafely
523 // claim they KNOW that the type is trivial, it's fine for that to be on
524 // them if that were wrong.
525 //
526 // There may be a legitimate reason we'll want to remove these assertions
527 // for support of types that the programmer knows are Rust-movable despite
528 // not being recognized as such by the C++ type system due to a move
529 // constructor or destructor.
530
Adrian Taylorc8713432020-10-21 18:20:55 -0700531 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700532 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700533 writeln!(out, "static_assert(");
534 writeln!(
535 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700536 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700537 id,
538 );
539 writeln!(
540 out,
541 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
542 id,
543 );
544 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700545 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700546 writeln!(
547 out,
548 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
549 id,
550 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700551}
552
Adrian Taylorc8713432020-10-21 18:20:55 -0700553fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700554 let mut has_cxx_throws = false;
555 for api in apis {
556 if let Api::CxxFunction(efn) = api {
557 if efn.throws {
558 has_cxx_throws = true;
559 break;
560 }
561 }
562 }
563
564 if has_cxx_throws {
565 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700566 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700567 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700568 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700569 );
570 }
571}
572
David Tolnaya7c2ea12020-10-30 21:32:53 -0700573fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700574 if let Some(annotation) = impl_annotations {
575 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700576 }
David Tolnayebef4a22020-03-17 15:33:47 -0700577 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700578 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700579 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700580 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700581 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700582 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700583 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700584 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700585 if receiver.mutability.is_none() {
586 write!(out, "const ");
587 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700588 write!(
589 out,
590 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700591 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700592 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700593 }
David Tolnay7db73692019-10-20 14:51:12 -0400594 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700595 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400596 write!(out, ", ");
597 }
David Tolnaya46a2372020-03-06 10:03:48 -0800598 if arg.ty == RustString {
599 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700600 } else if let Type::RustVec(_) = arg.ty {
601 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800602 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700603 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400604 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700605 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400606 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700607 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400608 write!(out, ", ");
609 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700610 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400611 write!(out, "*return$");
612 }
613 writeln!(out, ") noexcept {{");
614 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700615 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700616 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700617 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700618 Some(receiver) => write!(
619 out,
620 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700621 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700622 efn.ident.rust
623 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700624 }
David Tolnay7db73692019-10-20 14:51:12 -0400625 for (i, arg) in efn.args.iter().enumerate() {
626 if i > 0 {
627 write!(out, ", ");
628 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700629 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400630 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700631 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700632 if let Some(receiver) = &efn.receiver {
633 if receiver.mutability.is_none() {
634 write!(out, " const");
635 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700636 }
637 write!(out, " = ");
638 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700639 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
640 Some(receiver) => write!(
641 out,
642 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700643 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700644 efn.ident.cxx.ident
645 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700646 }
647 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400648 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700649 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700650 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700651 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700652 writeln!(out, " [&] {{");
653 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700654 }
David Tolnay7db73692019-10-20 14:51:12 -0400655 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700656 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400657 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700658 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400659 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700660 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400661 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700662 }
663 match &efn.ret {
664 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700665 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700666 Some(Type::SliceRefU8(_)) if !indirect_return => {
667 write!(out, "::rust::Slice<uint8_t>::Repr(")
668 }
David Tolnay99642622020-03-25 13:07:35 -0700669 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400670 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700671 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700672 None => write!(out, "{}$(", efn.ident.rust),
673 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700674 }
David Tolnay7db73692019-10-20 14:51:12 -0400675 for (i, arg) in efn.args.iter().enumerate() {
676 if i > 0 {
677 write!(out, ", ");
678 }
679 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700680 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400681 write!(out, "::from_raw({})", arg.ident);
682 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700683 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400684 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700685 } else if let Type::Str(_) = arg.ty {
686 write!(
687 out,
688 "::rust::impl<::rust::Str>::new_unchecked({})",
689 arg.ident,
690 );
David Tolnaya46a2372020-03-06 10:03:48 -0800691 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800692 write!(
693 out,
694 "::rust::String(::rust::unsafe_bitcopy, *{})",
695 arg.ident,
696 );
David Tolnay313b10e2020-04-25 16:30:51 -0700697 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700698 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700699 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700700 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700701 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800702 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400703 } else {
704 write!(out, "{}", arg.ident);
705 }
706 }
707 write!(out, ")");
708 match &efn.ret {
709 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
710 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700711 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400712 _ => {}
713 }
714 if indirect_return {
715 write!(out, ")");
716 }
717 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700718 if efn.throws {
719 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700720 writeln!(out, " throw$.ptr = nullptr;");
721 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700722 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700723 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700724 writeln!(
725 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700726 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700727 );
David Tolnay5d121442020-03-17 22:14:40 -0700728 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700729 writeln!(out, " return throw$;");
730 }
David Tolnay7db73692019-10-20 14:51:12 -0400731 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700732 for arg in &efn.args {
733 if let Type::Fn(f) = &arg.ty {
734 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700735 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700736 }
737 }
738}
739
740fn write_function_pointer_trampoline(
741 out: &mut OutFile,
742 efn: &ExternFn,
743 var: &Ident,
744 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700745) {
746 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700747 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700748 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700749 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700750
751 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700752 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
753 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400754}
755
David Tolnaya7c2ea12020-10-30 21:32:53 -0700756fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
757 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700758 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700759 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700760}
761
762fn write_rust_function_decl_impl(
763 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700764 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700765 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700766 indirect_call: bool,
767) {
768 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700769 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700770 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700771 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700772 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700773 write!(out, "{}(", link_name);
774 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700775 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700776 if receiver.mutability.is_none() {
777 write!(out, "const ");
778 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700779 write!(
780 out,
781 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700782 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700783 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700784 needs_comma = true;
785 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700786 for arg in &sig.args {
787 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400788 write!(out, ", ");
789 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700790 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700791 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400792 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700793 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700794 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400795 write!(out, ", ");
796 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700797 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400798 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700799 needs_comma = true;
800 }
801 if indirect_call {
802 if needs_comma {
803 write!(out, ", ");
804 }
805 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400806 }
807 writeln!(out, ") noexcept;");
808}
809
David Tolnaya7c2ea12020-10-30 21:32:53 -0700810fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400811 for line in efn.doc.to_string().lines() {
812 writeln!(out, "//{}", line);
813 }
David Tolnaya73853b2020-04-20 01:19:56 -0700814 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700815 None => efn.ident.cxx.ident.to_string(),
816 Some(receiver) => format!(
817 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700818 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700819 efn.ident.cxx.ident
820 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700821 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700822 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700823 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700824 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700825}
826
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700827fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700828 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700829 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700830 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700831 indirect_call: bool,
832) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700833 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700834 write!(out, "{}(", local_name);
835 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400836 if i > 0 {
837 write!(out, ", ");
838 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700839 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400840 write!(out, "{}", arg.ident);
841 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700842 if indirect_call {
843 if !sig.args.is_empty() {
844 write!(out, ", ");
845 }
846 write!(out, "void *extern$");
847 }
David Tolnay1e548172020-03-16 13:37:09 -0700848 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700849 if let Some(receiver) = &sig.receiver {
850 if receiver.mutability.is_none() {
851 write!(out, " const");
852 }
853 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700854 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700855 write!(out, " noexcept");
856 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700857}
858
859fn write_rust_function_shim_impl(
860 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700861 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700862 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700863 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700864 indirect_call: bool,
865) {
866 if out.header && sig.receiver.is_some() {
867 // We've already defined this inside the struct.
868 return;
869 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700870 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400871 if out.header {
872 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700873 return;
David Tolnay7db73692019-10-20 14:51:12 -0400874 }
David Tolnay439cde22020-04-20 00:46:25 -0700875 writeln!(out, " {{");
876 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700877 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700878 out.include.utility = true;
879 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700880 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700881 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
882 }
883 }
884 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700885 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700886 if indirect_return {
887 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700888 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700889 writeln!(out, "> return$;");
890 write!(out, " ");
891 } else if let Some(ret) = &sig.ret {
892 write!(out, "return ");
893 match ret {
894 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700895 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700896 write!(out, "::from_raw(");
897 }
898 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700899 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700900 write!(out, "(");
901 }
902 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700903 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700904 _ => {}
905 }
906 }
907 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700908 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700909 }
910 write!(out, "{}(", invoke);
911 if sig.receiver.is_some() {
912 write!(out, "*this");
913 }
914 for (i, arg) in sig.args.iter().enumerate() {
915 if i > 0 || sig.receiver.is_some() {
916 write!(out, ", ");
917 }
918 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700919 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700920 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700921 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700922 _ => {}
923 }
924 write!(out, "{}", arg.ident);
925 match &arg.ty {
926 Type::RustBox(_) => write!(out, ".into_raw()"),
927 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700928 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700929 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700930 _ => {}
931 }
932 }
933 if indirect_return {
934 if !sig.args.is_empty() {
935 write!(out, ", ");
936 }
937 write!(out, "&return$.value");
938 }
939 if indirect_call {
940 if !sig.args.is_empty() || indirect_return {
941 write!(out, ", ");
942 }
943 write!(out, "extern$");
944 }
945 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400946 if !indirect_return {
947 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700948 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400949 write!(out, ")");
950 }
David Tolnay439cde22020-04-20 00:46:25 -0700951 }
952 }
953 writeln!(out, ";");
954 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700955 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700956 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700957 writeln!(out, " }}");
958 }
959 if indirect_return {
960 out.include.utility = true;
961 writeln!(out, " return ::std::move(return$.value);");
962 }
963 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400964}
965
David Tolnaya7c2ea12020-10-30 21:32:53 -0700966fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400967 match ty {
968 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700969 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400970 }
971}
972
David Tolnay75dca2e2020-03-25 20:17:52 -0700973fn indirect_return(sig: &Signature, types: &Types) -> bool {
974 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700975 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700976 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700977}
978
David Tolnaya7c2ea12020-10-30 21:32:53 -0700979fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700980 match ty {
981 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700982 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700983 write!(out, "*");
984 }
985 Type::Ref(ty) => {
986 if ty.mutability.is_none() {
987 write!(out, "const ");
988 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700989 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700990 write!(out, " *");
991 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700992 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700993 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700994 }
995}
996
David Tolnaya7c2ea12020-10-30 21:32:53 -0700997fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
998 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700999 match ty {
1000 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001001 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001002 _ => write_space_after_type(out, ty),
1003 }
1004}
1005
David Tolnaya7c2ea12020-10-30 21:32:53 -07001006fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001007 match ty {
1008 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001009 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001010 write!(out, "*");
1011 }
David Tolnay4a441222020-01-25 16:24:27 -08001012 Some(Type::Ref(ty)) => {
1013 if ty.mutability.is_none() {
1014 write!(out, "const ");
1015 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001016 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001017 write!(out, " *");
1018 }
David Tolnay0356d332020-10-31 19:46:41 -07001019 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001020 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001021 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1022 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001023 }
1024}
1025
David Tolnaya7c2ea12020-10-30 21:32:53 -07001026fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001027 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001028 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001029 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001030 write!(out, "*");
1031 }
David Tolnay0356d332020-10-31 19:46:41 -07001032 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001033 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001034 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001035 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001036 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001037 write!(out, "*");
1038 }
1039 write!(out, "{}", arg.ident);
1040}
1041
David Tolnaya7c2ea12020-10-30 21:32:53 -07001042fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001043 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001044 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001045 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001046 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001047 },
1048 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001049 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001050 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001051 write!(out, ">");
1052 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001053 Type::RustVec(ty) => {
1054 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001055 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001056 write!(out, ">");
1057 }
David Tolnay7db73692019-10-20 14:51:12 -04001058 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001059 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001060 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001061 write!(out, ">");
1062 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001063 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001064 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001065 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001066 write!(out, ">");
1067 }
David Tolnay7db73692019-10-20 14:51:12 -04001068 Type::Ref(r) => {
1069 if r.mutability.is_none() {
1070 write!(out, "const ");
1071 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001072 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001073 write!(out, " &");
1074 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001075 Type::Slice(_) => {
1076 // For now, only U8 slices are supported, which are covered separately below
1077 unreachable!()
1078 }
David Tolnay7db73692019-10-20 14:51:12 -04001079 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001080 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001081 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001082 Type::SliceRefU8(_) => {
1083 write!(out, "::rust::Slice<uint8_t>");
1084 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001085 Type::Fn(f) => {
1086 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1087 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001088 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001089 None => write!(out, "void"),
1090 }
1091 write!(out, "(");
1092 for (i, arg) in f.args.iter().enumerate() {
1093 if i > 0 {
1094 write!(out, ", ");
1095 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001096 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001097 }
1098 write!(out, ")>");
1099 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001100 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001101 }
1102}
1103
David Tolnayf6a89f22020-05-10 23:39:27 -07001104fn write_atom(out: &mut OutFile, atom: Atom) {
1105 match atom {
1106 Bool => write!(out, "bool"),
1107 U8 => write!(out, "uint8_t"),
1108 U16 => write!(out, "uint16_t"),
1109 U32 => write!(out, "uint32_t"),
1110 U64 => write!(out, "uint64_t"),
1111 Usize => write!(out, "size_t"),
1112 I8 => write!(out, "int8_t"),
1113 I16 => write!(out, "int16_t"),
1114 I32 => write!(out, "int32_t"),
1115 I64 => write!(out, "int64_t"),
1116 Isize => write!(out, "::rust::isize"),
1117 F32 => write!(out, "float"),
1118 F64 => write!(out, "double"),
1119 CxxString => write!(out, "::std::string"),
1120 RustString => write!(out, "::rust::String"),
1121 }
1122}
1123
David Tolnaya7c2ea12020-10-30 21:32:53 -07001124fn write_type_space(out: &mut OutFile, ty: &Type) {
1125 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001126 write_space_after_type(out, ty);
1127}
1128
1129fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001130 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001131 Type::Ident(_)
1132 | Type::RustBox(_)
1133 | Type::UniquePtr(_)
1134 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001135 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001136 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001137 | Type::SliceRefU8(_)
1138 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001139 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001140 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001141 }
1142}
1143
David Tolnaycd08c442020-04-25 10:16:33 -07001144// Only called for legal referent types of unique_ptr and element types of
1145// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001146fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001147 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001148 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1149 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001150 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001151 }
1152}
1153
David Tolnayacdf20a2020-04-25 12:40:53 -07001154// Only called for legal referent types of unique_ptr and element types of
1155// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001156fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001157 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001158 Type::Ident(ident) => ident.to_symbol(types),
1159 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001160 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001161 }
1162}
1163
David Tolnaya7c2ea12020-10-30 21:32:53 -07001164fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001165 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001166 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001167 if let Type::RustBox(ty) = ty {
1168 if let Type::Ident(inner) = &ty.inner {
1169 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001170 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001171 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001172 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001173 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001174 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001175 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001176 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001177 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001178 }
David Tolnay7db73692019-10-20 14:51:12 -04001179 } else if let Type::UniquePtr(ptr) = ty {
1180 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001181 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001182 && (!out.types.aliases.contains_key(&inner.rust)
1183 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001184 {
David Tolnay7db73692019-10-20 14:51:12 -04001185 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001186 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001187 }
1188 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001189 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001190 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001191 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001192 && (!out.types.aliases.contains_key(&inner.rust)
1193 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001194 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001195 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001196 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001197 }
1198 }
1199 }
1200 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001201 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001202
David Tolnay750755e2020-03-01 13:04:08 -08001203 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001204 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001205 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001206 if let Type::RustBox(ty) = ty {
1207 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001208 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001209 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001210 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001211 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001212 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001213 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001214 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001215 }
David Tolnay7db73692019-10-20 14:51:12 -04001216 }
1217 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001218 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001219 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001220}
1221
Adrian Taylorc8713432020-10-21 18:20:55 -07001222fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1223 let inner = ident.to_fully_qualified();
1224 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001225
David Tolnay8f16ae72020-10-08 18:21:13 -07001226 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1227 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001228 writeln!(
1229 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001230 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001231 instance, inner,
1232 );
1233 writeln!(
1234 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001235 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001236 instance, inner,
1237 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001238 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001239}
1240
David Tolnaya7c2ea12020-10-30 21:32:53 -07001241fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001242 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001243 let inner = to_typename(&element, out.types);
1244 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001245
David Tolnay8f16ae72020-10-08 18:21:13 -07001246 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1247 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001248 writeln!(
1249 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001250 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001251 instance, inner,
1252 );
1253 writeln!(
1254 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001255 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001256 instance, inner,
1257 );
1258 writeln!(
1259 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001260 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001261 instance, inner,
1262 );
David Tolnay219c0792020-04-24 20:31:37 -07001263 writeln!(
1264 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001265 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001266 inner, instance,
1267 );
David Tolnay503d0192020-04-24 22:18:56 -07001268 writeln!(
1269 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001270 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001271 instance,
1272 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001273 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001274}
1275
Adrian Taylorc8713432020-10-21 18:20:55 -07001276fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1277 let inner = ident.to_fully_qualified();
1278 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001279
1280 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001281 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001282 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001283 writeln!(out, "}}");
1284
1285 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001286 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001287 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001288 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001289}
1290
David Tolnaya7c2ea12020-10-30 21:32:53 -07001291fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001292 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001293 let inner = to_typename(&element, out.types);
1294 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001295
Myron Ahneba35cf2020-02-05 19:41:51 +07001296 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001297 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001298 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001299 writeln!(out, "}}");
1300
1301 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001302 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1303 writeln!(
1304 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001305 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001306 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001307 );
1308 writeln!(out, "}}");
1309
1310 writeln!(out, "template <>");
1311 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001312 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001313 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001314
1315 writeln!(out, "template <>");
1316 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1317 writeln!(
1318 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001319 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001320 instance,
1321 );
1322 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001323
1324 writeln!(out, "template <>");
1325 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001326 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001327 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001328}
1329
David Tolnaya7c2ea12020-10-30 21:32:53 -07001330fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001331 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001332 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001333
David Tolnay8f16ae72020-10-08 18:21:13 -07001334 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1335 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001336
David Tolnaya7c2ea12020-10-30 21:32:53 -07001337 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001338
David Tolnay8f16ae72020-10-08 18:21:13 -07001339 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001340}
1341
1342// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001343fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001344 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001345 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001346 let inner = to_typename(ty, out.types);
1347 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001348
David Tolnay63da4d32020-04-25 09:41:12 -07001349 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001350 // Some aliases are to opaque types; some are to trivial types. We can't
1351 // know at code generation time, so we generate both C++ and Rust side
1352 // bindings for a "new" method anyway. But the Rust code can't be called
1353 // for Opaque types because the 'new' method is not implemented.
1354 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001355 out.types.structs.contains_key(&ident.rust)
1356 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001357 }
David Tolnay63da4d32020-04-25 09:41:12 -07001358 _ => false,
1359 };
1360
David Tolnay7db73692019-10-20 14:51:12 -04001361 writeln!(
1362 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001363 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001364 inner,
1365 );
1366 writeln!(
1367 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001368 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001369 inner,
1370 );
1371 writeln!(
1372 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001373 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001374 instance, inner,
1375 );
David Tolnay7e219b82020-03-01 13:14:51 -08001376 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001377 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001378 if can_construct_from_value {
1379 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001380 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001381 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001382 instance, inner, inner,
1383 );
David Tolnay63da4d32020-04-25 09:41:12 -07001384 writeln!(
1385 out,
1386 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1387 inner, inner,
1388 );
1389 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001390 }
David Tolnay7db73692019-10-20 14:51:12 -04001391 writeln!(
1392 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001393 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001394 instance, inner, inner,
1395 );
David Tolnay7e219b82020-03-01 13:14:51 -08001396 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001397 writeln!(out, "}}");
1398 writeln!(
1399 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001400 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001401 inner, instance, inner,
1402 );
1403 writeln!(out, " return ptr.get();");
1404 writeln!(out, "}}");
1405 writeln!(
1406 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001407 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001408 inner, instance, inner,
1409 );
1410 writeln!(out, " return ptr.release();");
1411 writeln!(out, "}}");
1412 writeln!(
1413 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001414 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001415 instance, inner,
1416 );
1417 writeln!(out, " ptr->~unique_ptr();");
1418 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001419}
Myron Ahneba35cf2020-02-05 19:41:51 +07001420
David Tolnaya7c2ea12020-10-30 21:32:53 -07001421fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001422 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001423 let inner = to_typename(&element, out.types);
1424 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001425
David Tolnay8f16ae72020-10-08 18:21:13 -07001426 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1427 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001428 writeln!(
1429 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001430 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001431 instance, inner,
1432 );
1433 writeln!(out, " return s.size();");
1434 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001435 writeln!(
1436 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001437 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001438 inner, instance, inner,
1439 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001440 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001441 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001442
David Tolnaya7c2ea12020-10-30 21:32:53 -07001443 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001444
David Tolnay8f16ae72020-10-08 18:21:13 -07001445 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001446}