blob: 69f0144566ce8c7806646043703071804ace8b76 [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,
158 Some(CxxString) => out.include.string = true,
David Tolnayf57f7562020-10-04 19:56:26 -0700159 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700160 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800161 Type::RustBox(_) => out.include.type_traits = true,
162 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700163 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700164 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700165 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400166 }
167 }
David Tolnay7db73692019-10-20 14:51:12 -0400168
David Tolnaya7c2ea12020-10-30 21:32:53 -0700169 for ty in out.types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700170 match ty {
171 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 Tolnayb7a7cb62020-03-17 21:18:40 -0700184 Type::Str(_) => {
185 out.include.cstdint = true;
186 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700187 out.builtin.rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700189 Type::Fn(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700190 out.builtin.rust_fn = true;
David Tolnay75dca2e2020-03-25 20:17:52 -0700191 }
David Tolnay4770b472020-04-14 16:32:59 -0700192 Type::Slice(_) | Type::SliceRefU8(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700193 out.builtin.rust_slice = true;
David Tolnay4770b472020-04-14 16:32:59 -0700194 }
David Tolnay7c295462020-04-25 12:45:07 -0700195 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400196 out.include.basetsd = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700197 out.builtin.rust_isize = true;
David Tolnay7c295462020-04-25 12:45:07 -0700198 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700199 ty if ty == RustString => {
200 out.include.array = true;
201 out.include.cstdint = true;
202 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700203 out.builtin.rust_string = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700204 }
205 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400206 }
207 }
208
David Tolnay09011c32020-03-06 14:40:28 -0800209 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700210 match api {
211 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700212 if efn.throws {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700213 out.builtin.trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700214 } else if let Some(Type::Str(_)) = efn.ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700215 out.builtin.rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700216 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700217 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700218 match arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700219 Type::Str(_) => out.builtin.rust_str_new_unchecked = true,
220 Type::RustVec(_) => out.builtin.unsafe_bitcopy = true,
221 _ => out.builtin.unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700222 }
David Tolnay09011c32020-03-06 14:40:28 -0800223 }
224 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700225 Api::RustFunction(efn) if !out.header => {
226 if efn.throws {
227 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700228 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700229 out.builtin.rust_str = true;
230 out.builtin.rust_error = true;
231 out.builtin.maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700232 }
233 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700234 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700235 out.builtin.manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700236 }
237 if let Type::Str(_) = arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700238 out.builtin.rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 }
240 }
241 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700242 if out.types.needs_indirect_abi(ret) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700243 out.builtin.maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700244 } else if let Type::Str(_) = ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700245 out.builtin.rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700246 }
David Tolnayf51447e2020-03-06 14:14:27 -0800247 }
248 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800250 }
251 }
David Tolnayec66d112020-10-31 21:00:26 -0700252}
David Tolnayf51447e2020-03-06 14:14:27 -0800253
David Tolnayec66d112020-10-31 21:00:26 -0700254fn write_builtins(out: &mut OutFile) {
David Tolnay750755e2020-03-01 13:04:08 -0800255 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700256 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800257
David Tolnay3be0e1f2020-10-31 20:53:00 -0700258 if out.builtin != Default::default() {
David Tolnay736cbca2020-03-11 16:49:18 -0700259 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800260 }
261
David Tolnay3be0e1f2020-10-31 20:53:00 -0700262 include::write(out, out.builtin.panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700263
David Tolnay3be0e1f2020-10-31 20:53:00 -0700264 if out.builtin.rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700265 out.next_section();
266 writeln!(out, "struct unsafe_bitcopy_t;");
267 }
268
David Tolnay3be0e1f2020-10-31 20:53:00 -0700269 if out.builtin.rust_error {
David Tolnay84ddf9e2020-10-31 15:36:48 -0700270 out.begin_block("namespace");
271 writeln!(out, "template <typename T>");
272 writeln!(out, "class impl;");
273 out.end_block("namespace");
274 }
275
David Tolnay3be0e1f2020-10-31 20:53:00 -0700276 include::write(out, out.builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
277 include::write(out, out.builtin.rust_str, "CXXBRIDGE05_RUST_STR");
278 include::write(out, out.builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
279 include::write(out, out.builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
280 include::write(out, out.builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
281 include::write(out, out.builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
282 include::write(out, out.builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
283 include::write(out, out.builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
284 include::write(out, out.builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800285
David Tolnay3be0e1f2020-10-31 20:53:00 -0700286 if out.builtin.manually_drop {
David Tolnayf51447e2020-03-06 14:14:27 -0800287 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700288 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800289 writeln!(out, "template <typename T>");
290 writeln!(out, "union ManuallyDrop {{");
291 writeln!(out, " T value;");
292 writeln!(
293 out,
294 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
295 );
296 writeln!(out, " ~ManuallyDrop() {{}}");
297 writeln!(out, "}};");
298 }
299
David Tolnay3be0e1f2020-10-31 20:53:00 -0700300 if out.builtin.maybe_uninit {
David Tolnay09011c32020-03-06 14:40:28 -0800301 out.next_section();
302 writeln!(out, "template <typename T>");
303 writeln!(out, "union MaybeUninit {{");
304 writeln!(out, " T value;");
305 writeln!(out, " MaybeUninit() {{}}");
306 writeln!(out, " ~MaybeUninit() {{}}");
307 writeln!(out, "}};");
308 }
309
David Tolnayd68dfa82020-10-31 16:01:24 -0700310 out.begin_block("namespace");
311
David Tolnay3be0e1f2020-10-31 20:53:00 -0700312 if out.builtin.trycatch
313 || out.builtin.rust_error
314 || out.builtin.rust_str_new_unchecked
315 || out.builtin.rust_str_repr
316 {
David Tolnayd68dfa82020-10-31 16:01:24 -0700317 out.begin_block("namespace repr");
318 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700319 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700320 writeln!(out, " size_t len;");
321 writeln!(out, "}};");
322 out.end_block("namespace repr");
323 }
324
David Tolnay3be0e1f2020-10-31 20:53:00 -0700325 if out.builtin.rust_str_new_unchecked || out.builtin.rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700326 out.next_section();
327 writeln!(out, "template <>");
328 writeln!(out, "class impl<Str> final {{");
329 writeln!(out, "public:");
David Tolnay3be0e1f2020-10-31 20:53:00 -0700330 if out.builtin.rust_str_new_unchecked {
David Tolnaya4eb9432020-10-31 20:32:19 -0700331 writeln!(
332 out,
333 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
334 );
335 writeln!(out, " Str str;");
336 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
337 writeln!(out, " str.len = repr.len;");
338 writeln!(out, " return str;");
339 writeln!(out, " }}");
340 }
David Tolnay3be0e1f2020-10-31 20:53:00 -0700341 if out.builtin.rust_str_repr {
David Tolnaya4eb9432020-10-31 20:32:19 -0700342 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
343 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
344 writeln!(out, " }}");
345 }
David Tolnay0356d332020-10-31 19:46:41 -0700346 writeln!(out, "}};");
347 }
348
David Tolnay3be0e1f2020-10-31 20:53:00 -0700349 if out.builtin.rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700350 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700351 writeln!(out, "template <>");
352 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700353 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700354 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700355 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700356 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700357 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700358 writeln!(out, " return error;");
359 writeln!(out, " }}");
360 writeln!(out, "}};");
361 }
362
David Tolnayd68dfa82020-10-31 16:01:24 -0700363 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700364 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700365
David Tolnay3be0e1f2020-10-31 20:53:00 -0700366 if out.builtin.trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700367 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700368 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700369 out.include.type_traits = true;
370 out.include.utility = true;
371 writeln!(out, "class missing {{}};");
372 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700373 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700374 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700375 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700376 writeln!(
377 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700378 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700379 );
David Tolnay04722332020-03-18 11:31:54 -0700380 writeln!(out, " missing>::value>::type");
381 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700382 writeln!(out, " func();");
383 writeln!(out, "}} catch (const ::std::exception &e) {{");
384 writeln!(out, " fail(e.what());");
385 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700386 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700387 }
388
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800389 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400390}
391
David Tolnaya7c2ea12020-10-30 21:32:53 -0700392fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700393 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700394 writeln!(out, "#ifndef {}", guard);
395 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400396 for line in strct.doc.to_string().lines() {
397 writeln!(out, "//{}", line);
398 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700399 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400400 for field in &strct.fields {
401 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700402 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400403 writeln!(out, "{};", field.ident);
404 }
405 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700406 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400407}
408
409fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
410 writeln!(out, "struct {};", ident);
411}
412
Adrian Taylorc8713432020-10-21 18:20:55 -0700413fn write_struct_using(out: &mut OutFile, ident: &CppName) {
414 writeln!(
415 out,
416 "using {} = {};",
417 ident.ident,
418 ident.to_fully_qualified()
419 );
David Tolnay8861bee2020-01-20 18:39:24 -0800420}
421
David Tolnaya7c2ea12020-10-30 21:32:53 -0700422fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700423 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700424 writeln!(out, "#ifndef {}", guard);
425 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700426 for line in ety.doc.to_string().lines() {
427 writeln!(out, "//{}", line);
428 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700429 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
430 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
431 writeln!(
432 out,
433 " {}(const {} &) = delete;",
434 ety.ident.cxx.ident, ety.ident.cxx.ident
435 );
Joel Galenson968738f2020-04-15 14:19:33 -0700436 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700437 write!(out, " ");
438 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700439 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700440 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700441 writeln!(out, ";");
442 }
443 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700444 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700445}
446
Joel Galensonc03402a2020-04-23 17:31:09 -0700447fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700448 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700449 writeln!(out, "#ifndef {}", guard);
450 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700451 for line in enm.doc.to_string().lines() {
452 writeln!(out, "//{}", line);
453 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700454 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700455 write_atom(out, enm.repr);
456 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700457 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700458 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700459 }
460 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700461 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700462}
463
Joel Galenson905eb2e2020-05-04 14:58:14 -0700464fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700465 write!(
466 out,
467 "static_assert(sizeof({}) == sizeof(",
468 enm.ident.cxx.ident
469 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700470 write_atom(out, enm.repr);
471 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700472 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700473 write!(out, "static_assert(static_cast<");
474 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700475 writeln!(
476 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700477 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700478 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700479 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700480 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700481}
482
Adrian Taylorc8713432020-10-21 18:20:55 -0700483fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700484 // NOTE: The following two static assertions are just nice-to-have and not
485 // necessary for soundness. That's because triviality is always declared by
486 // the user in the form of an unsafe impl of cxx::ExternType:
487 //
488 // unsafe impl ExternType for MyType {
489 // type Id = cxx::type_id!("...");
490 // type Kind = cxx::kind::Trivial;
491 // }
492 //
493 // Since the user went on the record with their unsafe impl to unsafely
494 // claim they KNOW that the type is trivial, it's fine for that to be on
495 // them if that were wrong.
496 //
497 // There may be a legitimate reason we'll want to remove these assertions
498 // for support of types that the programmer knows are Rust-movable despite
499 // not being recognized as such by the C++ type system due to a move
500 // constructor or destructor.
501
Adrian Taylorc8713432020-10-21 18:20:55 -0700502 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700503 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700504 writeln!(out, "static_assert(");
505 writeln!(
506 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700507 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700508 id,
509 );
510 writeln!(
511 out,
512 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
513 id,
514 );
515 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700516 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700517 writeln!(
518 out,
519 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
520 id,
521 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700522}
523
Adrian Taylorc8713432020-10-21 18:20:55 -0700524fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700525 let mut has_cxx_throws = false;
526 for api in apis {
527 if let Api::CxxFunction(efn) = api {
528 if efn.throws {
529 has_cxx_throws = true;
530 break;
531 }
532 }
533 }
534
535 if has_cxx_throws {
536 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700537 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700538 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700539 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700540 );
541 }
542}
543
David Tolnaya7c2ea12020-10-30 21:32:53 -0700544fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700545 if let Some(annotation) = impl_annotations {
546 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700547 }
David Tolnayebef4a22020-03-17 15:33:47 -0700548 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700549 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700550 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700551 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700552 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700553 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700554 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700555 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700556 if receiver.mutability.is_none() {
557 write!(out, "const ");
558 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700559 write!(
560 out,
561 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700562 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700563 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700564 }
David Tolnay7db73692019-10-20 14:51:12 -0400565 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700566 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400567 write!(out, ", ");
568 }
David Tolnaya46a2372020-03-06 10:03:48 -0800569 if arg.ty == RustString {
570 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700571 } else if let Type::RustVec(_) = arg.ty {
572 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800573 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700574 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400575 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700576 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400577 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700578 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400579 write!(out, ", ");
580 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700581 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400582 write!(out, "*return$");
583 }
584 writeln!(out, ") noexcept {{");
585 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700586 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700587 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700588 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700589 Some(receiver) => write!(
590 out,
591 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700592 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700593 efn.ident.rust
594 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700595 }
David Tolnay7db73692019-10-20 14:51:12 -0400596 for (i, arg) in efn.args.iter().enumerate() {
597 if i > 0 {
598 write!(out, ", ");
599 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700600 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400601 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700602 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700603 if let Some(receiver) = &efn.receiver {
604 if receiver.mutability.is_none() {
605 write!(out, " const");
606 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700607 }
608 write!(out, " = ");
609 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700610 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
611 Some(receiver) => write!(
612 out,
613 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700614 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700615 efn.ident.cxx.ident
616 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700617 }
618 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400619 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700620 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700621 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700622 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700623 writeln!(out, " [&] {{");
624 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700625 }
David Tolnay7db73692019-10-20 14:51:12 -0400626 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700627 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400628 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700629 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400630 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700631 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400632 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700633 }
634 match &efn.ret {
635 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700636 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700637 Some(Type::SliceRefU8(_)) if !indirect_return => {
638 write!(out, "::rust::Slice<uint8_t>::Repr(")
639 }
David Tolnay99642622020-03-25 13:07:35 -0700640 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400641 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700642 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700643 None => write!(out, "{}$(", efn.ident.rust),
644 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700645 }
David Tolnay7db73692019-10-20 14:51:12 -0400646 for (i, arg) in efn.args.iter().enumerate() {
647 if i > 0 {
648 write!(out, ", ");
649 }
650 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700651 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400652 write!(out, "::from_raw({})", arg.ident);
653 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700654 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400655 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700656 } else if let Type::Str(_) = arg.ty {
657 write!(
658 out,
659 "::rust::impl<::rust::Str>::new_unchecked({})",
660 arg.ident,
661 );
David Tolnaya46a2372020-03-06 10:03:48 -0800662 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800663 write!(
664 out,
665 "::rust::String(::rust::unsafe_bitcopy, *{})",
666 arg.ident,
667 );
David Tolnay313b10e2020-04-25 16:30:51 -0700668 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700669 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700670 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700671 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700672 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800673 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400674 } else {
675 write!(out, "{}", arg.ident);
676 }
677 }
678 write!(out, ")");
679 match &efn.ret {
680 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
681 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700682 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400683 _ => {}
684 }
685 if indirect_return {
686 write!(out, ")");
687 }
688 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700689 if efn.throws {
690 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700691 writeln!(out, " throw$.ptr = nullptr;");
692 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700693 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700694 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700695 writeln!(
696 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700697 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700698 );
David Tolnay5d121442020-03-17 22:14:40 -0700699 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700700 writeln!(out, " return throw$;");
701 }
David Tolnay7db73692019-10-20 14:51:12 -0400702 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700703 for arg in &efn.args {
704 if let Type::Fn(f) = &arg.ty {
705 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700706 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700707 }
708 }
709}
710
711fn write_function_pointer_trampoline(
712 out: &mut OutFile,
713 efn: &ExternFn,
714 var: &Ident,
715 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700716) {
717 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700718 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700719 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700720 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700721
722 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700723 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
724 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400725}
726
David Tolnaya7c2ea12020-10-30 21:32:53 -0700727fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
728 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700729 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700730 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700731}
732
733fn write_rust_function_decl_impl(
734 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700735 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700736 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700737 indirect_call: bool,
738) {
739 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700740 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700741 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700742 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700743 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700744 write!(out, "{}(", link_name);
745 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700746 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700747 if receiver.mutability.is_none() {
748 write!(out, "const ");
749 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700750 write!(
751 out,
752 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700753 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700754 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700755 needs_comma = true;
756 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700757 for arg in &sig.args {
758 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400759 write!(out, ", ");
760 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700761 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700762 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400763 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700764 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700765 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400766 write!(out, ", ");
767 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700768 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400769 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700770 needs_comma = true;
771 }
772 if indirect_call {
773 if needs_comma {
774 write!(out, ", ");
775 }
776 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400777 }
778 writeln!(out, ") noexcept;");
779}
780
David Tolnaya7c2ea12020-10-30 21:32:53 -0700781fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400782 for line in efn.doc.to_string().lines() {
783 writeln!(out, "//{}", line);
784 }
David Tolnaya73853b2020-04-20 01:19:56 -0700785 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700786 None => efn.ident.cxx.ident.to_string(),
787 Some(receiver) => format!(
788 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700789 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700790 efn.ident.cxx.ident
791 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700792 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700793 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700794 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700795 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700796}
797
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700798fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700799 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700800 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700801 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700802 indirect_call: bool,
803) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700804 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700805 write!(out, "{}(", local_name);
806 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400807 if i > 0 {
808 write!(out, ", ");
809 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700810 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400811 write!(out, "{}", arg.ident);
812 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700813 if indirect_call {
814 if !sig.args.is_empty() {
815 write!(out, ", ");
816 }
817 write!(out, "void *extern$");
818 }
David Tolnay1e548172020-03-16 13:37:09 -0700819 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700820 if let Some(receiver) = &sig.receiver {
821 if receiver.mutability.is_none() {
822 write!(out, " const");
823 }
824 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700825 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700826 write!(out, " noexcept");
827 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700828}
829
830fn write_rust_function_shim_impl(
831 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700832 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700833 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700834 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700835 indirect_call: bool,
836) {
837 if out.header && sig.receiver.is_some() {
838 // We've already defined this inside the struct.
839 return;
840 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700841 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400842 if out.header {
843 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700844 return;
David Tolnay7db73692019-10-20 14:51:12 -0400845 }
David Tolnay439cde22020-04-20 00:46:25 -0700846 writeln!(out, " {{");
847 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700848 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700849 out.include.utility = true;
850 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700851 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700852 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
853 }
854 }
855 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700856 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700857 if indirect_return {
858 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700859 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700860 writeln!(out, "> return$;");
861 write!(out, " ");
862 } else if let Some(ret) = &sig.ret {
863 write!(out, "return ");
864 match ret {
865 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700866 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700867 write!(out, "::from_raw(");
868 }
869 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700870 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700871 write!(out, "(");
872 }
873 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700874 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700875 _ => {}
876 }
877 }
878 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700879 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700880 }
881 write!(out, "{}(", invoke);
882 if sig.receiver.is_some() {
883 write!(out, "*this");
884 }
885 for (i, arg) in sig.args.iter().enumerate() {
886 if i > 0 || sig.receiver.is_some() {
887 write!(out, ", ");
888 }
889 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700890 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700891 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700892 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700893 _ => {}
894 }
895 write!(out, "{}", arg.ident);
896 match &arg.ty {
897 Type::RustBox(_) => write!(out, ".into_raw()"),
898 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700899 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700900 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700901 _ => {}
902 }
903 }
904 if indirect_return {
905 if !sig.args.is_empty() {
906 write!(out, ", ");
907 }
908 write!(out, "&return$.value");
909 }
910 if indirect_call {
911 if !sig.args.is_empty() || indirect_return {
912 write!(out, ", ");
913 }
914 write!(out, "extern$");
915 }
916 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400917 if !indirect_return {
918 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700919 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400920 write!(out, ")");
921 }
David Tolnay439cde22020-04-20 00:46:25 -0700922 }
923 }
924 writeln!(out, ";");
925 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700926 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700927 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700928 writeln!(out, " }}");
929 }
930 if indirect_return {
931 out.include.utility = true;
932 writeln!(out, " return ::std::move(return$.value);");
933 }
934 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400935}
936
David Tolnaya7c2ea12020-10-30 21:32:53 -0700937fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400938 match ty {
939 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700940 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400941 }
942}
943
David Tolnay75dca2e2020-03-25 20:17:52 -0700944fn indirect_return(sig: &Signature, types: &Types) -> bool {
945 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700946 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700947 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700948}
949
David Tolnaya7c2ea12020-10-30 21:32:53 -0700950fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700951 match ty {
952 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700953 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700954 write!(out, "*");
955 }
956 Type::Ref(ty) => {
957 if ty.mutability.is_none() {
958 write!(out, "const ");
959 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700960 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700961 write!(out, " *");
962 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700963 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700964 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700965 }
966}
967
David Tolnaya7c2ea12020-10-30 21:32:53 -0700968fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
969 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700970 match ty {
971 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700972 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700973 _ => write_space_after_type(out, ty),
974 }
975}
976
David Tolnaya7c2ea12020-10-30 21:32:53 -0700977fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400978 match ty {
979 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700980 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400981 write!(out, "*");
982 }
David Tolnay4a441222020-01-25 16:24:27 -0800983 Some(Type::Ref(ty)) => {
984 if ty.mutability.is_none() {
985 write!(out, "const ");
986 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700987 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -0800988 write!(out, " *");
989 }
David Tolnay0356d332020-10-31 19:46:41 -0700990 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700991 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700992 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
993 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400994 }
995}
996
David Tolnaya7c2ea12020-10-30 21:32:53 -0700997fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -0400998 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700999 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001000 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001001 write!(out, "*");
1002 }
David Tolnay0356d332020-10-31 19:46:41 -07001003 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001004 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001005 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001006 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001007 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001008 write!(out, "*");
1009 }
1010 write!(out, "{}", arg.ident);
1011}
1012
David Tolnaya7c2ea12020-10-30 21:32:53 -07001013fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001014 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001015 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001016 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001017 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001018 },
1019 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001020 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001021 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001022 write!(out, ">");
1023 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001024 Type::RustVec(ty) => {
1025 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001026 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001027 write!(out, ">");
1028 }
David Tolnay7db73692019-10-20 14:51:12 -04001029 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001030 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001031 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001032 write!(out, ">");
1033 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001034 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001035 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001036 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001037 write!(out, ">");
1038 }
David Tolnay7db73692019-10-20 14:51:12 -04001039 Type::Ref(r) => {
1040 if r.mutability.is_none() {
1041 write!(out, "const ");
1042 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001043 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001044 write!(out, " &");
1045 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001046 Type::Slice(_) => {
1047 // For now, only U8 slices are supported, which are covered separately below
1048 unreachable!()
1049 }
David Tolnay7db73692019-10-20 14:51:12 -04001050 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001051 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001052 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001053 Type::SliceRefU8(_) => {
1054 write!(out, "::rust::Slice<uint8_t>");
1055 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001056 Type::Fn(f) => {
1057 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1058 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001059 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001060 None => write!(out, "void"),
1061 }
1062 write!(out, "(");
1063 for (i, arg) in f.args.iter().enumerate() {
1064 if i > 0 {
1065 write!(out, ", ");
1066 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001067 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001068 }
1069 write!(out, ")>");
1070 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001071 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001072 }
1073}
1074
David Tolnayf6a89f22020-05-10 23:39:27 -07001075fn write_atom(out: &mut OutFile, atom: Atom) {
1076 match atom {
1077 Bool => write!(out, "bool"),
1078 U8 => write!(out, "uint8_t"),
1079 U16 => write!(out, "uint16_t"),
1080 U32 => write!(out, "uint32_t"),
1081 U64 => write!(out, "uint64_t"),
1082 Usize => write!(out, "size_t"),
1083 I8 => write!(out, "int8_t"),
1084 I16 => write!(out, "int16_t"),
1085 I32 => write!(out, "int32_t"),
1086 I64 => write!(out, "int64_t"),
1087 Isize => write!(out, "::rust::isize"),
1088 F32 => write!(out, "float"),
1089 F64 => write!(out, "double"),
1090 CxxString => write!(out, "::std::string"),
1091 RustString => write!(out, "::rust::String"),
1092 }
1093}
1094
David Tolnaya7c2ea12020-10-30 21:32:53 -07001095fn write_type_space(out: &mut OutFile, ty: &Type) {
1096 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001097 write_space_after_type(out, ty);
1098}
1099
1100fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001101 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001102 Type::Ident(_)
1103 | Type::RustBox(_)
1104 | Type::UniquePtr(_)
1105 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001106 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001107 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001108 | Type::SliceRefU8(_)
1109 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001110 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001111 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001112 }
1113}
1114
David Tolnaycd08c442020-04-25 10:16:33 -07001115// Only called for legal referent types of unique_ptr and element types of
1116// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001117fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001118 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001119 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1120 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001121 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001122 }
1123}
1124
David Tolnayacdf20a2020-04-25 12:40:53 -07001125// Only called for legal referent types of unique_ptr and element types of
1126// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001127fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001128 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001129 Type::Ident(ident) => ident.to_symbol(types),
1130 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001131 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001132 }
1133}
1134
David Tolnaya7c2ea12020-10-30 21:32:53 -07001135fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001136 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001137 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001138 if let Type::RustBox(ty) = ty {
1139 if let Type::Ident(inner) = &ty.inner {
1140 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001141 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001142 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001143 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001144 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001145 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001146 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001147 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001148 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001149 }
David Tolnay7db73692019-10-20 14:51:12 -04001150 } else if let Type::UniquePtr(ptr) = ty {
1151 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001152 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001153 && (!out.types.aliases.contains_key(&inner.rust)
1154 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001155 {
David Tolnay7db73692019-10-20 14:51:12 -04001156 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001157 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001158 }
1159 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001160 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001161 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001162 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001163 && (!out.types.aliases.contains_key(&inner.rust)
1164 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001165 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001166 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001167 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001168 }
1169 }
1170 }
1171 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001172 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001173
David Tolnay750755e2020-03-01 13:04:08 -08001174 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001175 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001176 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001177 if let Type::RustBox(ty) = ty {
1178 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001179 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001180 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001181 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001182 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001183 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001184 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001185 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001186 }
David Tolnay7db73692019-10-20 14:51:12 -04001187 }
1188 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001189 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001190 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001191}
1192
Adrian Taylorc8713432020-10-21 18:20:55 -07001193fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1194 let inner = ident.to_fully_qualified();
1195 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001196
David Tolnay8f16ae72020-10-08 18:21:13 -07001197 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1198 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001199 writeln!(
1200 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001201 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001202 instance, inner,
1203 );
1204 writeln!(
1205 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001206 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001207 instance, inner,
1208 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001209 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001210}
1211
David Tolnaya7c2ea12020-10-30 21:32:53 -07001212fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001213 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001214 let inner = to_typename(&element, out.types);
1215 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001216
David Tolnay8f16ae72020-10-08 18:21:13 -07001217 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1218 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001219 writeln!(
1220 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001221 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001222 instance, inner,
1223 );
1224 writeln!(
1225 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001226 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001227 instance, inner,
1228 );
1229 writeln!(
1230 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001231 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001232 instance, inner,
1233 );
David Tolnay219c0792020-04-24 20:31:37 -07001234 writeln!(
1235 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001236 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001237 inner, instance,
1238 );
David Tolnay503d0192020-04-24 22:18:56 -07001239 writeln!(
1240 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001241 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001242 instance,
1243 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001244 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001245}
1246
Adrian Taylorc8713432020-10-21 18:20:55 -07001247fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1248 let inner = ident.to_fully_qualified();
1249 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001250
1251 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001252 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001253 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001254 writeln!(out, "}}");
1255
1256 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001257 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001258 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001259 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001260}
1261
David Tolnaya7c2ea12020-10-30 21:32:53 -07001262fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001263 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001264 let inner = to_typename(&element, out.types);
1265 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001266
Myron Ahneba35cf2020-02-05 19:41:51 +07001267 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001268 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001269 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001270 writeln!(out, "}}");
1271
1272 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001273 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1274 writeln!(
1275 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001276 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001277 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001278 );
1279 writeln!(out, "}}");
1280
1281 writeln!(out, "template <>");
1282 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001283 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001284 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001285
1286 writeln!(out, "template <>");
1287 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1288 writeln!(
1289 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001290 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001291 instance,
1292 );
1293 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001294
1295 writeln!(out, "template <>");
1296 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001297 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001298 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001299}
1300
David Tolnaya7c2ea12020-10-30 21:32:53 -07001301fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001302 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001303 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001304
David Tolnay8f16ae72020-10-08 18:21:13 -07001305 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1306 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001307
David Tolnaya7c2ea12020-10-30 21:32:53 -07001308 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001309
David Tolnay8f16ae72020-10-08 18:21:13 -07001310 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001311}
1312
1313// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001314fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001315 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001316 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001317 let inner = to_typename(ty, out.types);
1318 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001319
David Tolnay63da4d32020-04-25 09:41:12 -07001320 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001321 // Some aliases are to opaque types; some are to trivial types. We can't
1322 // know at code generation time, so we generate both C++ and Rust side
1323 // bindings for a "new" method anyway. But the Rust code can't be called
1324 // for Opaque types because the 'new' method is not implemented.
1325 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001326 out.types.structs.contains_key(&ident.rust)
1327 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001328 }
David Tolnay63da4d32020-04-25 09:41:12 -07001329 _ => false,
1330 };
1331
David Tolnay7db73692019-10-20 14:51:12 -04001332 writeln!(
1333 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001334 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001335 inner,
1336 );
1337 writeln!(
1338 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001339 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001340 inner,
1341 );
1342 writeln!(
1343 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001344 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001345 instance, inner,
1346 );
David Tolnay7e219b82020-03-01 13:14:51 -08001347 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001348 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001349 if can_construct_from_value {
1350 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001351 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001352 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001353 instance, inner, inner,
1354 );
David Tolnay63da4d32020-04-25 09:41:12 -07001355 writeln!(
1356 out,
1357 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1358 inner, inner,
1359 );
1360 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001361 }
David Tolnay7db73692019-10-20 14:51:12 -04001362 writeln!(
1363 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001364 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001365 instance, inner, inner,
1366 );
David Tolnay7e219b82020-03-01 13:14:51 -08001367 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001368 writeln!(out, "}}");
1369 writeln!(
1370 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001371 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001372 inner, instance, inner,
1373 );
1374 writeln!(out, " return ptr.get();");
1375 writeln!(out, "}}");
1376 writeln!(
1377 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001378 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001379 inner, instance, inner,
1380 );
1381 writeln!(out, " return ptr.release();");
1382 writeln!(out, "}}");
1383 writeln!(
1384 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001385 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001386 instance, inner,
1387 );
1388 writeln!(out, " ptr->~unique_ptr();");
1389 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001390}
Myron Ahneba35cf2020-02-05 19:41:51 +07001391
David Tolnaya7c2ea12020-10-30 21:32:53 -07001392fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001393 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001394 let inner = to_typename(&element, out.types);
1395 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001396
David Tolnay8f16ae72020-10-08 18:21:13 -07001397 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1398 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001399 writeln!(
1400 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001401 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001402 instance, inner,
1403 );
1404 writeln!(out, " return s.size();");
1405 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001406 writeln!(
1407 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001408 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001409 inner, instance, inner,
1410 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001411 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001412 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001413
David Tolnaya7c2ea12020-10-30 21:32:53 -07001414 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001415
David Tolnay8f16ae72020-10-08 18:21:13 -07001416 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001417}