blob: a0bafc220dbf86c7ade173feef46008d5c0b4674 [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 Tolnayec66d112020-10-31 21:00:26 -070030 write_builtins(out);
David Tolnay7db73692019-10-20 14:51:12 -040031
David Tolnay7db73692019-10-20 14:51:12 -040032 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070033
Adrian Taylor565ddf02020-10-29 21:12:36 -070034 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070035
David Tolnay04b81652020-10-31 22:43:25 -070036 gen_namespace_forward_declarations(out, &apis_by_namespace);
37 gen_namespace_contents(out, &apis_by_namespace, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -070038
39 if !header {
40 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070041 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040042 }
43
Adrian Taylorc8713432020-10-21 18:20:55 -070044 write!(out.front, "{}", out.include);
45
46 out_file
47}
48
David Tolnay04b81652020-10-31 22:43:25 -070049fn gen_namespace_forward_declarations(out: &mut OutFile, ns_entries: &NamespaceEntries) {
Adrian Taylor565ddf02020-10-29 21:12:36 -070050 let apis = ns_entries.entries();
Adrian Taylorc8713432020-10-21 18:20:55 -070051
David Tolnay7db73692019-10-20 14:51:12 -040052 out.next_section();
David Tolnay630af882020-10-31 22:03:47 -070053 for api in apis {
David Tolnay7db73692019-10-20 14:51:12 -040054 match api {
Adrian Taylorc8713432020-10-21 18:20:55 -070055 Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident),
56 Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx),
57 Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident),
David Tolnay7c295462020-04-25 12:45:07 -070058 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040059 }
60 }
61
Adrian Taylorf9213622020-10-31 22:25:42 -070062 out.next_section();
63
64 for (child_ns, child_ns_entries) in ns_entries.children() {
65 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -070066 gen_namespace_forward_declarations(out, child_ns_entries);
Adrian Taylorf9213622020-10-31 22:25:42 -070067 writeln!(out, "}} // namespace {}", child_ns);
68 }
69}
70
David Tolnay04b81652020-10-31 22:43:25 -070071fn gen_namespace_contents(out: &mut OutFile, ns_entries: &NamespaceEntries, opt: &Opt) {
Adrian Taylorf9213622020-10-31 22:25:42 -070072 let apis = ns_entries.entries();
73
David Tolnayf94bef12020-04-17 14:46:42 -070074 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070075 for api in apis {
David Tolnayf94bef12020-04-17 14:46:42 -070076 if let Api::RustFunction(efn) = api {
77 if let Some(receiver) = &efn.sig.receiver {
78 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070079 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070080 .or_insert_with(Vec::new)
81 .push(efn);
82 }
83 }
84 }
Joel Galenson968738f2020-04-15 14:19:33 -070085
David Tolnay7db73692019-10-20 14:51:12 -040086 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070087 match api {
88 Api::Struct(strct) => {
89 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070090 if !out.types.cxx.contains(&strct.ident.rust) {
David Tolnaya7c2ea12020-10-30 21:32:53 -070091 write_struct(out, strct);
David Tolnaya593d6e2020-08-29 19:48:08 -070092 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070093 }
Joel Galensonc03402a2020-04-23 17:31:09 -070094 Api::Enum(enm) => {
95 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070096 if out.types.cxx.contains(&enm.ident.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070097 check_enum(out, enm);
98 } else {
99 write_enum(out, enm);
100 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700101 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700102 Api::RustType(ety) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700103 if let Some(methods) = methods_for_type.get(&ety.ident.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -0700104 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700105 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700106 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700107 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700108 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400109 }
110 }
111
David Tolnayfabca772020-10-03 21:25:41 -0700112 out.next_section();
113 for api in apis {
114 if let Api::TypeAlias(ety) = api {
David Tolnay4d148422020-10-31 22:41:37 -0700115 if out.types.required_trivial.contains_key(&ety.ident.rust) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700116 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700117 }
118 }
119 }
120
David Tolnayce5a91f2020-10-31 22:42:08 -0700121 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400122 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700123 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400124 for api in apis {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700125 let (efn, write): (_, fn(_, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400126 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
127 Api::RustFunction(efn) => (efn, write_rust_function_decl),
128 _ => continue,
129 };
130 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700131 write(out, efn, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400132 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800133 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400134 }
135
136 for api in apis {
137 if let Api::RustFunction(efn) = api {
138 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700139 write_rust_function_shim(out, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400140 }
141 }
142
143 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700144
Adrian Taylor565ddf02020-10-29 21:12:36 -0700145 for (child_ns, child_ns_entries) in ns_entries.children() {
Adrian Taylorc8713432020-10-21 18:20:55 -0700146 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -0700147 gen_namespace_contents(out, child_ns_entries, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -0700148 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400149 }
David Tolnay7db73692019-10-20 14:51:12 -0400150}
151
David Tolnaya7c2ea12020-10-30 21:32:53 -0700152fn write_includes(out: &mut OutFile) {
153 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400154 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700155 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700156 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
157 | Some(I64) => out.include.cstdint = true,
158 Some(Usize) => out.include.cstddef = true,
159 Some(CxxString) => out.include.string = true,
David Tolnayf57f7562020-10-04 19:56:26 -0700160 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700161 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800162 Type::RustBox(_) => out.include.type_traits = true,
163 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700164 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700165 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700166 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400167 }
168 }
David Tolnay7db73692019-10-20 14:51:12 -0400169}
170
David Tolnaya7c2ea12020-10-30 21:32:53 -0700171fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api]) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700172 for ty in out.types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700173 match ty {
174 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700175 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700176 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700177 out.builtin.rust_box = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700179 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700180 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700181 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700182 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700183 out.builtin.panic = true;
184 out.builtin.rust_vec = true;
185 out.builtin.unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700186 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700187 Type::Str(_) => {
188 out.include.cstdint = true;
189 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700190 out.builtin.rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700192 Type::Fn(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700193 out.builtin.rust_fn = true;
David Tolnay75dca2e2020-03-25 20:17:52 -0700194 }
David Tolnay4770b472020-04-14 16:32:59 -0700195 Type::Slice(_) | Type::SliceRefU8(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700196 out.builtin.rust_slice = true;
David Tolnay4770b472020-04-14 16:32:59 -0700197 }
David Tolnay7c295462020-04-25 12:45:07 -0700198 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400199 out.include.basetsd = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700200 out.builtin.rust_isize = true;
David Tolnay7c295462020-04-25 12:45:07 -0700201 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700202 ty if ty == RustString => {
203 out.include.array = true;
204 out.include.cstdint = true;
205 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700206 out.builtin.rust_string = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 }
208 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400209 }
210 }
211
David Tolnay09011c32020-03-06 14:40:28 -0800212 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 match api {
214 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700215 if efn.throws {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700216 out.builtin.trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700217 } else if let Some(Type::Str(_)) = efn.ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700218 out.builtin.rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700219 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700220 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700221 match arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700222 Type::Str(_) => out.builtin.rust_str_new_unchecked = true,
223 Type::RustVec(_) => out.builtin.unsafe_bitcopy = true,
224 _ => out.builtin.unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700225 }
David Tolnay09011c32020-03-06 14:40:28 -0800226 }
227 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 Api::RustFunction(efn) if !out.header => {
229 if efn.throws {
230 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700231 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700232 out.builtin.rust_str = true;
233 out.builtin.rust_error = true;
234 out.builtin.maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 }
236 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700237 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700238 out.builtin.manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700239 }
240 if let Type::Str(_) = arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700241 out.builtin.rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 }
243 }
244 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700245 if out.types.needs_indirect_abi(ret) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700246 out.builtin.maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700247 } else if let Type::Str(_) = ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700248 out.builtin.rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 }
David Tolnayf51447e2020-03-06 14:14:27 -0800250 }
251 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700252 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800253 }
254 }
David Tolnayec66d112020-10-31 21:00:26 -0700255}
David Tolnayf51447e2020-03-06 14:14:27 -0800256
David Tolnayec66d112020-10-31 21:00:26 -0700257fn write_builtins(out: &mut OutFile) {
David Tolnay750755e2020-03-01 13:04:08 -0800258 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700259 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800260
David Tolnay3be0e1f2020-10-31 20:53:00 -0700261 if out.builtin != Default::default() {
David Tolnay736cbca2020-03-11 16:49:18 -0700262 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800263 }
264
David Tolnay3be0e1f2020-10-31 20:53:00 -0700265 include::write(out, out.builtin.panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700266
David Tolnay3be0e1f2020-10-31 20:53:00 -0700267 if out.builtin.rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700268 out.next_section();
269 writeln!(out, "struct unsafe_bitcopy_t;");
270 }
271
David Tolnay3be0e1f2020-10-31 20:53:00 -0700272 if out.builtin.rust_error {
David Tolnay84ddf9e2020-10-31 15:36:48 -0700273 out.begin_block("namespace");
274 writeln!(out, "template <typename T>");
275 writeln!(out, "class impl;");
276 out.end_block("namespace");
277 }
278
David Tolnay3be0e1f2020-10-31 20:53:00 -0700279 include::write(out, out.builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
280 include::write(out, out.builtin.rust_str, "CXXBRIDGE05_RUST_STR");
281 include::write(out, out.builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
282 include::write(out, out.builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
283 include::write(out, out.builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
284 include::write(out, out.builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
285 include::write(out, out.builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
286 include::write(out, out.builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
287 include::write(out, out.builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800288
David Tolnay3be0e1f2020-10-31 20:53:00 -0700289 if out.builtin.manually_drop {
David Tolnayf51447e2020-03-06 14:14:27 -0800290 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700291 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800292 writeln!(out, "template <typename T>");
293 writeln!(out, "union ManuallyDrop {{");
294 writeln!(out, " T value;");
295 writeln!(
296 out,
297 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
298 );
299 writeln!(out, " ~ManuallyDrop() {{}}");
300 writeln!(out, "}};");
301 }
302
David Tolnay3be0e1f2020-10-31 20:53:00 -0700303 if out.builtin.maybe_uninit {
David Tolnay09011c32020-03-06 14:40:28 -0800304 out.next_section();
305 writeln!(out, "template <typename T>");
306 writeln!(out, "union MaybeUninit {{");
307 writeln!(out, " T value;");
308 writeln!(out, " MaybeUninit() {{}}");
309 writeln!(out, " ~MaybeUninit() {{}}");
310 writeln!(out, "}};");
311 }
312
David Tolnayd68dfa82020-10-31 16:01:24 -0700313 out.begin_block("namespace");
314
David Tolnay3be0e1f2020-10-31 20:53:00 -0700315 if out.builtin.trycatch
316 || out.builtin.rust_error
317 || out.builtin.rust_str_new_unchecked
318 || out.builtin.rust_str_repr
319 {
David Tolnayd68dfa82020-10-31 16:01:24 -0700320 out.begin_block("namespace repr");
321 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700322 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700323 writeln!(out, " size_t len;");
324 writeln!(out, "}};");
325 out.end_block("namespace repr");
326 }
327
David Tolnay3be0e1f2020-10-31 20:53:00 -0700328 if out.builtin.rust_str_new_unchecked || out.builtin.rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700329 out.next_section();
330 writeln!(out, "template <>");
331 writeln!(out, "class impl<Str> final {{");
332 writeln!(out, "public:");
David Tolnay3be0e1f2020-10-31 20:53:00 -0700333 if out.builtin.rust_str_new_unchecked {
David Tolnaya4eb9432020-10-31 20:32:19 -0700334 writeln!(
335 out,
336 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
337 );
338 writeln!(out, " Str str;");
339 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
340 writeln!(out, " str.len = repr.len;");
341 writeln!(out, " return str;");
342 writeln!(out, " }}");
343 }
David Tolnay3be0e1f2020-10-31 20:53:00 -0700344 if out.builtin.rust_str_repr {
David Tolnaya4eb9432020-10-31 20:32:19 -0700345 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
346 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
347 writeln!(out, " }}");
348 }
David Tolnay0356d332020-10-31 19:46:41 -0700349 writeln!(out, "}};");
350 }
351
David Tolnay3be0e1f2020-10-31 20:53:00 -0700352 if out.builtin.rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700353 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700354 writeln!(out, "template <>");
355 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700356 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700357 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700358 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700359 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700360 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700361 writeln!(out, " return error;");
362 writeln!(out, " }}");
363 writeln!(out, "}};");
364 }
365
David Tolnayd68dfa82020-10-31 16:01:24 -0700366 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700367 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700368
David Tolnay3be0e1f2020-10-31 20:53:00 -0700369 if out.builtin.trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700370 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700371 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700372 out.include.type_traits = true;
373 out.include.utility = true;
374 writeln!(out, "class missing {{}};");
375 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700376 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700377 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700378 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700379 writeln!(
380 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700381 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700382 );
David Tolnay04722332020-03-18 11:31:54 -0700383 writeln!(out, " missing>::value>::type");
384 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700385 writeln!(out, " func();");
386 writeln!(out, "}} catch (const ::std::exception &e) {{");
387 writeln!(out, " fail(e.what());");
388 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700389 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700390 }
391
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800392 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400393}
394
David Tolnaya7c2ea12020-10-30 21:32:53 -0700395fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700396 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700397 writeln!(out, "#ifndef {}", guard);
398 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400399 for line in strct.doc.to_string().lines() {
400 writeln!(out, "//{}", line);
401 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700402 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400403 for field in &strct.fields {
404 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700405 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400406 writeln!(out, "{};", field.ident);
407 }
408 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700409 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400410}
411
412fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
413 writeln!(out, "struct {};", ident);
414}
415
Adrian Taylorc8713432020-10-21 18:20:55 -0700416fn write_struct_using(out: &mut OutFile, ident: &CppName) {
417 writeln!(
418 out,
419 "using {} = {};",
420 ident.ident,
421 ident.to_fully_qualified()
422 );
David Tolnay8861bee2020-01-20 18:39:24 -0800423}
424
David Tolnaya7c2ea12020-10-30 21:32:53 -0700425fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700426 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700427 writeln!(out, "#ifndef {}", guard);
428 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700429 for line in ety.doc.to_string().lines() {
430 writeln!(out, "//{}", line);
431 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700432 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
433 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
434 writeln!(
435 out,
436 " {}(const {} &) = delete;",
437 ety.ident.cxx.ident, ety.ident.cxx.ident
438 );
Joel Galenson968738f2020-04-15 14:19:33 -0700439 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700440 write!(out, " ");
441 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700442 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700443 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700444 writeln!(out, ";");
445 }
446 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700447 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700448}
449
Joel Galensonc03402a2020-04-23 17:31:09 -0700450fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700451 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700452 writeln!(out, "#ifndef {}", guard);
453 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700454 for line in enm.doc.to_string().lines() {
455 writeln!(out, "//{}", line);
456 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700457 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700458 write_atom(out, enm.repr);
459 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700460 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700461 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700462 }
463 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700464 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700465}
466
Joel Galenson905eb2e2020-05-04 14:58:14 -0700467fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700468 write!(
469 out,
470 "static_assert(sizeof({}) == sizeof(",
471 enm.ident.cxx.ident
472 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700473 write_atom(out, enm.repr);
474 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700475 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700476 write!(out, "static_assert(static_cast<");
477 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700478 writeln!(
479 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700480 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700481 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700482 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700483 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700484}
485
Adrian Taylorc8713432020-10-21 18:20:55 -0700486fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700487 // NOTE: The following two static assertions are just nice-to-have and not
488 // necessary for soundness. That's because triviality is always declared by
489 // the user in the form of an unsafe impl of cxx::ExternType:
490 //
491 // unsafe impl ExternType for MyType {
492 // type Id = cxx::type_id!("...");
493 // type Kind = cxx::kind::Trivial;
494 // }
495 //
496 // Since the user went on the record with their unsafe impl to unsafely
497 // claim they KNOW that the type is trivial, it's fine for that to be on
498 // them if that were wrong.
499 //
500 // There may be a legitimate reason we'll want to remove these assertions
501 // for support of types that the programmer knows are Rust-movable despite
502 // not being recognized as such by the C++ type system due to a move
503 // constructor or destructor.
504
Adrian Taylorc8713432020-10-21 18:20:55 -0700505 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700506 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700507 writeln!(out, "static_assert(");
508 writeln!(
509 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700510 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700511 id,
512 );
513 writeln!(
514 out,
515 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
516 id,
517 );
518 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700519 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700520 writeln!(
521 out,
522 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
523 id,
524 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700525}
526
Adrian Taylorc8713432020-10-21 18:20:55 -0700527fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700528 let mut has_cxx_throws = false;
529 for api in apis {
530 if let Api::CxxFunction(efn) = api {
531 if efn.throws {
532 has_cxx_throws = true;
533 break;
534 }
535 }
536 }
537
538 if has_cxx_throws {
539 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700540 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700541 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700542 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700543 );
544 }
545}
546
David Tolnaya7c2ea12020-10-30 21:32:53 -0700547fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700548 if let Some(annotation) = impl_annotations {
549 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700550 }
David Tolnayebef4a22020-03-17 15:33:47 -0700551 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700552 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700553 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700554 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700555 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700556 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700557 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700558 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700559 if receiver.mutability.is_none() {
560 write!(out, "const ");
561 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700562 write!(
563 out,
564 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700565 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700566 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700567 }
David Tolnay7db73692019-10-20 14:51:12 -0400568 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700569 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400570 write!(out, ", ");
571 }
David Tolnaya46a2372020-03-06 10:03:48 -0800572 if arg.ty == RustString {
573 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700574 } else if let Type::RustVec(_) = arg.ty {
575 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800576 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700577 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400578 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700579 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400580 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700581 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400582 write!(out, ", ");
583 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700584 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400585 write!(out, "*return$");
586 }
587 writeln!(out, ") noexcept {{");
588 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700589 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700590 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700591 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700592 Some(receiver) => write!(
593 out,
594 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700595 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700596 efn.ident.rust
597 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700598 }
David Tolnay7db73692019-10-20 14:51:12 -0400599 for (i, arg) in efn.args.iter().enumerate() {
600 if i > 0 {
601 write!(out, ", ");
602 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700603 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400604 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700605 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700606 if let Some(receiver) = &efn.receiver {
607 if receiver.mutability.is_none() {
608 write!(out, " const");
609 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700610 }
611 write!(out, " = ");
612 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700613 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
614 Some(receiver) => write!(
615 out,
616 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700617 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700618 efn.ident.cxx.ident
619 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700620 }
621 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400622 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700623 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700624 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700625 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700626 writeln!(out, " [&] {{");
627 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700628 }
David Tolnay7db73692019-10-20 14:51:12 -0400629 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700630 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400631 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700632 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400633 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700634 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400635 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700636 }
637 match &efn.ret {
638 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700639 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700640 Some(Type::SliceRefU8(_)) if !indirect_return => {
641 write!(out, "::rust::Slice<uint8_t>::Repr(")
642 }
David Tolnay99642622020-03-25 13:07:35 -0700643 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400644 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700645 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700646 None => write!(out, "{}$(", efn.ident.rust),
647 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700648 }
David Tolnay7db73692019-10-20 14:51:12 -0400649 for (i, arg) in efn.args.iter().enumerate() {
650 if i > 0 {
651 write!(out, ", ");
652 }
653 if let Type::RustBox(_) = &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, "::from_raw({})", arg.ident);
656 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700657 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400658 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700659 } else if let Type::Str(_) = arg.ty {
660 write!(
661 out,
662 "::rust::impl<::rust::Str>::new_unchecked({})",
663 arg.ident,
664 );
David Tolnaya46a2372020-03-06 10:03:48 -0800665 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800666 write!(
667 out,
668 "::rust::String(::rust::unsafe_bitcopy, *{})",
669 arg.ident,
670 );
David Tolnay313b10e2020-04-25 16:30:51 -0700671 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700672 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700673 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700674 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700675 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800676 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400677 } else {
678 write!(out, "{}", arg.ident);
679 }
680 }
681 write!(out, ")");
682 match &efn.ret {
683 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
684 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700685 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400686 _ => {}
687 }
688 if indirect_return {
689 write!(out, ")");
690 }
691 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700692 if efn.throws {
693 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700694 writeln!(out, " throw$.ptr = nullptr;");
695 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700696 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700697 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700698 writeln!(
699 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700700 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700701 );
David Tolnay5d121442020-03-17 22:14:40 -0700702 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700703 writeln!(out, " return throw$;");
704 }
David Tolnay7db73692019-10-20 14:51:12 -0400705 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700706 for arg in &efn.args {
707 if let Type::Fn(f) = &arg.ty {
708 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700709 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700710 }
711 }
712}
713
714fn write_function_pointer_trampoline(
715 out: &mut OutFile,
716 efn: &ExternFn,
717 var: &Ident,
718 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700719) {
720 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700721 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700722 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700723 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700724
725 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700726 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
727 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400728}
729
David Tolnaya7c2ea12020-10-30 21:32:53 -0700730fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
731 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700732 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700733 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700734}
735
736fn write_rust_function_decl_impl(
737 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700738 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700739 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700740 indirect_call: bool,
741) {
742 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700743 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700744 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700745 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700746 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700747 write!(out, "{}(", link_name);
748 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700749 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700750 if receiver.mutability.is_none() {
751 write!(out, "const ");
752 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700753 write!(
754 out,
755 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700756 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700757 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700758 needs_comma = true;
759 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700760 for arg in &sig.args {
761 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400762 write!(out, ", ");
763 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700764 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700765 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400766 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700767 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700768 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400769 write!(out, ", ");
770 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700771 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400772 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700773 needs_comma = true;
774 }
775 if indirect_call {
776 if needs_comma {
777 write!(out, ", ");
778 }
779 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400780 }
781 writeln!(out, ") noexcept;");
782}
783
David Tolnaya7c2ea12020-10-30 21:32:53 -0700784fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400785 for line in efn.doc.to_string().lines() {
786 writeln!(out, "//{}", line);
787 }
David Tolnaya73853b2020-04-20 01:19:56 -0700788 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700789 None => efn.ident.cxx.ident.to_string(),
790 Some(receiver) => format!(
791 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700792 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700793 efn.ident.cxx.ident
794 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700795 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700796 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700797 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700798 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700799}
800
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700801fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700802 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700803 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700804 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700805 indirect_call: bool,
806) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700807 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700808 write!(out, "{}(", local_name);
809 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400810 if i > 0 {
811 write!(out, ", ");
812 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700813 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400814 write!(out, "{}", arg.ident);
815 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700816 if indirect_call {
817 if !sig.args.is_empty() {
818 write!(out, ", ");
819 }
820 write!(out, "void *extern$");
821 }
David Tolnay1e548172020-03-16 13:37:09 -0700822 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700823 if let Some(receiver) = &sig.receiver {
824 if receiver.mutability.is_none() {
825 write!(out, " const");
826 }
827 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700828 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700829 write!(out, " noexcept");
830 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700831}
832
833fn write_rust_function_shim_impl(
834 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700835 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700836 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700837 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700838 indirect_call: bool,
839) {
840 if out.header && sig.receiver.is_some() {
841 // We've already defined this inside the struct.
842 return;
843 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700844 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400845 if out.header {
846 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700847 return;
David Tolnay7db73692019-10-20 14:51:12 -0400848 }
David Tolnay439cde22020-04-20 00:46:25 -0700849 writeln!(out, " {{");
850 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700851 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700852 out.include.utility = true;
853 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700854 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700855 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
856 }
857 }
858 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700859 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700860 if indirect_return {
861 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700862 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700863 writeln!(out, "> return$;");
864 write!(out, " ");
865 } else if let Some(ret) = &sig.ret {
866 write!(out, "return ");
867 match ret {
868 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700869 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700870 write!(out, "::from_raw(");
871 }
872 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700873 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700874 write!(out, "(");
875 }
876 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700877 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700878 _ => {}
879 }
880 }
881 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700882 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700883 }
884 write!(out, "{}(", invoke);
885 if sig.receiver.is_some() {
886 write!(out, "*this");
887 }
888 for (i, arg) in sig.args.iter().enumerate() {
889 if i > 0 || sig.receiver.is_some() {
890 write!(out, ", ");
891 }
892 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700893 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700894 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700895 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700896 _ => {}
897 }
898 write!(out, "{}", arg.ident);
899 match &arg.ty {
900 Type::RustBox(_) => write!(out, ".into_raw()"),
901 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700902 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700903 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700904 _ => {}
905 }
906 }
907 if indirect_return {
908 if !sig.args.is_empty() {
909 write!(out, ", ");
910 }
911 write!(out, "&return$.value");
912 }
913 if indirect_call {
914 if !sig.args.is_empty() || indirect_return {
915 write!(out, ", ");
916 }
917 write!(out, "extern$");
918 }
919 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400920 if !indirect_return {
921 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700922 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400923 write!(out, ")");
924 }
David Tolnay439cde22020-04-20 00:46:25 -0700925 }
926 }
927 writeln!(out, ";");
928 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700929 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700930 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700931 writeln!(out, " }}");
932 }
933 if indirect_return {
934 out.include.utility = true;
935 writeln!(out, " return ::std::move(return$.value);");
936 }
937 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400938}
939
David Tolnaya7c2ea12020-10-30 21:32:53 -0700940fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400941 match ty {
942 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700943 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400944 }
945}
946
David Tolnay75dca2e2020-03-25 20:17:52 -0700947fn indirect_return(sig: &Signature, types: &Types) -> bool {
948 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700949 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700950 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700951}
952
David Tolnaya7c2ea12020-10-30 21:32:53 -0700953fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700954 match ty {
955 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700956 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700957 write!(out, "*");
958 }
959 Type::Ref(ty) => {
960 if ty.mutability.is_none() {
961 write!(out, "const ");
962 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700963 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700964 write!(out, " *");
965 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700966 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700967 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700968 }
969}
970
David Tolnaya7c2ea12020-10-30 21:32:53 -0700971fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
972 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700973 match ty {
974 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700975 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700976 _ => write_space_after_type(out, ty),
977 }
978}
979
David Tolnaya7c2ea12020-10-30 21:32:53 -0700980fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400981 match ty {
982 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700983 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400984 write!(out, "*");
985 }
David Tolnay4a441222020-01-25 16:24:27 -0800986 Some(Type::Ref(ty)) => {
987 if ty.mutability.is_none() {
988 write!(out, "const ");
989 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700990 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -0800991 write!(out, " *");
992 }
David Tolnay0356d332020-10-31 19:46:41 -0700993 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700994 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700995 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
996 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400997 }
998}
999
David Tolnaya7c2ea12020-10-30 21:32:53 -07001000fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001001 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001002 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001003 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001004 write!(out, "*");
1005 }
David Tolnay0356d332020-10-31 19:46:41 -07001006 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001007 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001008 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001009 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001010 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001011 write!(out, "*");
1012 }
1013 write!(out, "{}", arg.ident);
1014}
1015
David Tolnaya7c2ea12020-10-30 21:32:53 -07001016fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001017 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001018 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001019 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001020 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001021 },
1022 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001023 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001024 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001025 write!(out, ">");
1026 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001027 Type::RustVec(ty) => {
1028 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001029 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001030 write!(out, ">");
1031 }
David Tolnay7db73692019-10-20 14:51:12 -04001032 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001033 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001034 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001035 write!(out, ">");
1036 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001037 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001038 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001039 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001040 write!(out, ">");
1041 }
David Tolnay7db73692019-10-20 14:51:12 -04001042 Type::Ref(r) => {
1043 if r.mutability.is_none() {
1044 write!(out, "const ");
1045 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001046 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001047 write!(out, " &");
1048 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001049 Type::Slice(_) => {
1050 // For now, only U8 slices are supported, which are covered separately below
1051 unreachable!()
1052 }
David Tolnay7db73692019-10-20 14:51:12 -04001053 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001054 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001055 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001056 Type::SliceRefU8(_) => {
1057 write!(out, "::rust::Slice<uint8_t>");
1058 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001059 Type::Fn(f) => {
1060 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1061 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001062 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001063 None => write!(out, "void"),
1064 }
1065 write!(out, "(");
1066 for (i, arg) in f.args.iter().enumerate() {
1067 if i > 0 {
1068 write!(out, ", ");
1069 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001070 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001071 }
1072 write!(out, ")>");
1073 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001074 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001075 }
1076}
1077
David Tolnayf6a89f22020-05-10 23:39:27 -07001078fn write_atom(out: &mut OutFile, atom: Atom) {
1079 match atom {
1080 Bool => write!(out, "bool"),
1081 U8 => write!(out, "uint8_t"),
1082 U16 => write!(out, "uint16_t"),
1083 U32 => write!(out, "uint32_t"),
1084 U64 => write!(out, "uint64_t"),
1085 Usize => write!(out, "size_t"),
1086 I8 => write!(out, "int8_t"),
1087 I16 => write!(out, "int16_t"),
1088 I32 => write!(out, "int32_t"),
1089 I64 => write!(out, "int64_t"),
1090 Isize => write!(out, "::rust::isize"),
1091 F32 => write!(out, "float"),
1092 F64 => write!(out, "double"),
1093 CxxString => write!(out, "::std::string"),
1094 RustString => write!(out, "::rust::String"),
1095 }
1096}
1097
David Tolnaya7c2ea12020-10-30 21:32:53 -07001098fn write_type_space(out: &mut OutFile, ty: &Type) {
1099 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001100 write_space_after_type(out, ty);
1101}
1102
1103fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001104 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001105 Type::Ident(_)
1106 | Type::RustBox(_)
1107 | Type::UniquePtr(_)
1108 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001109 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001110 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001111 | Type::SliceRefU8(_)
1112 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001113 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001114 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001115 }
1116}
1117
David Tolnaycd08c442020-04-25 10:16:33 -07001118// Only called for legal referent types of unique_ptr and element types of
1119// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001120fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001121 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001122 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1123 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001124 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001125 }
1126}
1127
David Tolnayacdf20a2020-04-25 12:40:53 -07001128// Only called for legal referent types of unique_ptr and element types of
1129// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001130fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001131 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001132 Type::Ident(ident) => ident.to_symbol(types),
1133 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001134 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001135 }
1136}
1137
David Tolnaya7c2ea12020-10-30 21:32:53 -07001138fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001139 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001140 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001141 if let Type::RustBox(ty) = ty {
1142 if let Type::Ident(inner) = &ty.inner {
1143 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001144 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001145 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001146 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001147 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001148 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001149 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001150 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001151 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001152 }
David Tolnay7db73692019-10-20 14:51:12 -04001153 } else if let Type::UniquePtr(ptr) = ty {
1154 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001155 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001156 && (!out.types.aliases.contains_key(&inner.rust)
1157 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001158 {
David Tolnay7db73692019-10-20 14:51:12 -04001159 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001160 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001161 }
1162 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001163 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001164 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001165 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001166 && (!out.types.aliases.contains_key(&inner.rust)
1167 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001168 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001169 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001170 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001171 }
1172 }
1173 }
1174 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001175 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001176
David Tolnay750755e2020-03-01 13:04:08 -08001177 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001178 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001179 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001180 if let Type::RustBox(ty) = ty {
1181 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001182 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001183 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001184 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001185 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001186 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001187 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001188 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001189 }
David Tolnay7db73692019-10-20 14:51:12 -04001190 }
1191 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001192 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001193 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001194}
1195
Adrian Taylorc8713432020-10-21 18:20:55 -07001196fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1197 let inner = ident.to_fully_qualified();
1198 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001199
David Tolnay8f16ae72020-10-08 18:21:13 -07001200 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1201 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001202 writeln!(
1203 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001204 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001205 instance, inner,
1206 );
1207 writeln!(
1208 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001209 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001210 instance, inner,
1211 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001212 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001213}
1214
David Tolnaya7c2ea12020-10-30 21:32:53 -07001215fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001216 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001217 let inner = to_typename(&element, out.types);
1218 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001219
David Tolnay8f16ae72020-10-08 18:21:13 -07001220 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1221 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001222 writeln!(
1223 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001224 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001225 instance, inner,
1226 );
1227 writeln!(
1228 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001229 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001230 instance, inner,
1231 );
1232 writeln!(
1233 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001234 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001235 instance, inner,
1236 );
David Tolnay219c0792020-04-24 20:31:37 -07001237 writeln!(
1238 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001239 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001240 inner, instance,
1241 );
David Tolnay503d0192020-04-24 22:18:56 -07001242 writeln!(
1243 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001244 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001245 instance,
1246 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001247 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001248}
1249
Adrian Taylorc8713432020-10-21 18:20:55 -07001250fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1251 let inner = ident.to_fully_qualified();
1252 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001253
1254 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001255 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001256 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001257 writeln!(out, "}}");
1258
1259 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001260 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001261 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001262 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001263}
1264
David Tolnaya7c2ea12020-10-30 21:32:53 -07001265fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001266 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001267 let inner = to_typename(&element, out.types);
1268 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001269
Myron Ahneba35cf2020-02-05 19:41:51 +07001270 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001271 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001272 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001273 writeln!(out, "}}");
1274
1275 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001276 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1277 writeln!(
1278 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001279 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001280 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001281 );
1282 writeln!(out, "}}");
1283
1284 writeln!(out, "template <>");
1285 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001286 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001287 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001288
1289 writeln!(out, "template <>");
1290 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1291 writeln!(
1292 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001293 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001294 instance,
1295 );
1296 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001297
1298 writeln!(out, "template <>");
1299 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001300 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001301 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001302}
1303
David Tolnaya7c2ea12020-10-30 21:32:53 -07001304fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001305 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001306 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001307
David Tolnay8f16ae72020-10-08 18:21:13 -07001308 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1309 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001310
David Tolnaya7c2ea12020-10-30 21:32:53 -07001311 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001312
David Tolnay8f16ae72020-10-08 18:21:13 -07001313 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001314}
1315
1316// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001317fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001318 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001319 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001320 let inner = to_typename(ty, out.types);
1321 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001322
David Tolnay63da4d32020-04-25 09:41:12 -07001323 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001324 // Some aliases are to opaque types; some are to trivial types. We can't
1325 // know at code generation time, so we generate both C++ and Rust side
1326 // bindings for a "new" method anyway. But the Rust code can't be called
1327 // for Opaque types because the 'new' method is not implemented.
1328 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001329 out.types.structs.contains_key(&ident.rust)
1330 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001331 }
David Tolnay63da4d32020-04-25 09:41:12 -07001332 _ => false,
1333 };
1334
David Tolnay7db73692019-10-20 14:51:12 -04001335 writeln!(
1336 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001337 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001338 inner,
1339 );
1340 writeln!(
1341 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001342 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001343 inner,
1344 );
1345 writeln!(
1346 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001347 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001348 instance, inner,
1349 );
David Tolnay7e219b82020-03-01 13:14:51 -08001350 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001351 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001352 if can_construct_from_value {
1353 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001354 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001355 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001356 instance, inner, inner,
1357 );
David Tolnay63da4d32020-04-25 09:41:12 -07001358 writeln!(
1359 out,
1360 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1361 inner, inner,
1362 );
1363 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001364 }
David Tolnay7db73692019-10-20 14:51:12 -04001365 writeln!(
1366 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001367 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001368 instance, inner, inner,
1369 );
David Tolnay7e219b82020-03-01 13:14:51 -08001370 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001371 writeln!(out, "}}");
1372 writeln!(
1373 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001374 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001375 inner, instance, inner,
1376 );
1377 writeln!(out, " return ptr.get();");
1378 writeln!(out, "}}");
1379 writeln!(
1380 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001381 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001382 inner, instance, inner,
1383 );
1384 writeln!(out, " return ptr.release();");
1385 writeln!(out, "}}");
1386 writeln!(
1387 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001388 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001389 instance, inner,
1390 );
1391 writeln!(out, " ptr->~unique_ptr();");
1392 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001393}
Myron Ahneba35cf2020-02-05 19:41:51 +07001394
David Tolnaya7c2ea12020-10-30 21:32:53 -07001395fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001396 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001397 let inner = to_typename(&element, out.types);
1398 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001399
David Tolnay8f16ae72020-10-08 18:21:13 -07001400 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1401 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001402 writeln!(
1403 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001404 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001405 instance, inner,
1406 );
1407 writeln!(out, " return s.size();");
1408 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001409 writeln!(
1410 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001411 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001412 inner, instance, inner,
1413 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001414 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001415 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001416
David Tolnaya7c2ea12020-10-30 21:32:53 -07001417 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001418
David Tolnay8f16ae72020-10-08 18:21:13 -07001419 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001420}