blob: 6ee6ad7330399cf1a2aaf6adea4ef71b8d6350a7 [file] [log] [blame]
Adrian Taylor565ddf02020-10-29 21:12:36 -07001use crate::gen::namespace_organizer::NamespaceEntries;
David Tolnay7db73692019-10-20 14:51:12 -04002use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07003use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04004use crate::syntax::atom::Atom::{self, *};
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Adrian Taylorc8713432020-10-21 18:20:55 -07006use crate::syntax::{
7 mangle, Api, CppName, Enum, ExternFn, ExternType, ResolvableName, Signature, Struct, Type,
8 Types, Var,
9};
David Tolnay7db73692019-10-20 14:51:12 -040010use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -070011use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040012
David Tolnayb560a0f2020-10-30 21:28:45 -070013pub(super) fn gen<'a>(apis: &[Api], types: &'a Types, opt: &Opt, header: bool) -> OutFile<'a> {
14 let mut out_file = OutFile::new(header, types);
David Tolnay7db73692019-10-20 14:51:12 -040015 let out = &mut out_file;
16
David Tolnay54702b92020-07-31 11:50:09 -070017 if header {
18 writeln!(out.front, "#pragma once");
19 }
20
David Tolnay4aae7c02020-10-28 12:35:42 -070021 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040022 for api in apis {
23 if let Api::Include(include) = api {
David Tolnay353d98c2020-10-28 15:43:35 -070024 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040025 }
26 }
27
David Tolnaya7c2ea12020-10-30 21:32:53 -070028 write_includes(out);
29 write_include_cxxbridge(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040030
David Tolnay7db73692019-10-20 14:51:12 -040031 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070032
Adrian Taylor565ddf02020-10-29 21:12:36 -070033 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070034
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 Tolnaya7c2ea12020-10-30 21:32:53 -0700151fn write_includes(out: &mut OutFile) {
152 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}
169
David Tolnaya7c2ea12020-10-30 21:32:53 -0700170fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api]) {
David Tolnay16ab1462020-09-02 15:10:09 -0700171 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700172 let mut needs_rust_string = false;
173 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700174 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400175 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700176 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700177 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700178 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700179 let mut needs_unsafe_bitcopy = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700180 for ty in out.types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700181 match ty {
182 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700183 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700184 out.include.type_traits = true;
185 needs_rust_box = true;
186 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700187 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700188 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700189 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700190 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700191 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700192 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700193 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700194 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700195 Type::Str(_) => {
196 out.include.cstdint = true;
197 out.include.string = true;
198 needs_rust_str = true;
199 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700200 Type::Fn(_) => {
201 needs_rust_fn = true;
202 }
David Tolnay4770b472020-04-14 16:32:59 -0700203 Type::Slice(_) | Type::SliceRefU8(_) => {
204 needs_rust_slice = true;
205 }
David Tolnay7c295462020-04-25 12:45:07 -0700206 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400207 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700208 needs_rust_isize = true;
209 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700210 ty if ty == RustString => {
211 out.include.array = true;
212 out.include.cstdint = true;
213 out.include.string = true;
214 needs_rust_string = true;
215 }
216 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400217 }
218 }
219
David Tolnayb7a7cb62020-03-17 21:18:40 -0700220 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800221 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800222 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700223 let mut needs_trycatch = false;
David Tolnaya4eb9432020-10-31 20:32:19 -0700224 let mut needs_rust_str_new_unchecked = false;
225 let mut needs_rust_str_repr = false;
David Tolnay09011c32020-03-06 14:40:28 -0800226 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700227 match api {
228 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700229 if efn.throws {
230 needs_trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700231 } else if let Some(Type::Str(_)) = efn.ret {
232 needs_rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700233 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700234 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700235 match arg.ty {
236 Type::Str(_) => needs_rust_str_new_unchecked = true,
237 Type::RustVec(_) => needs_unsafe_bitcopy = true,
238 _ => needs_unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 }
David Tolnay09011c32020-03-06 14:40:28 -0800240 }
241 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 Api::RustFunction(efn) if !out.header => {
243 if efn.throws {
244 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700245 out.include.string = true;
246 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700247 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200248 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 }
250 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700251 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700252 needs_manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700253 }
254 if let Type::Str(_) = arg.ty {
255 needs_rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700256 }
257 }
258 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700259 if out.types.needs_indirect_abi(ret) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700260 needs_maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700261 } else if let Type::Str(_) = ret {
262 needs_rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700263 }
David Tolnayf51447e2020-03-06 14:14:27 -0800264 }
265 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700266 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800267 }
268 }
269
David Tolnay750755e2020-03-01 13:04:08 -0800270 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700271 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800272
David Tolnay16ab1462020-09-02 15:10:09 -0700273 if needs_panic
274 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700275 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700276 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700277 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700278 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700279 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700280 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700281 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700282 || needs_unsafe_bitcopy
283 || needs_manually_drop
284 || needs_maybe_uninit
285 {
David Tolnay736cbca2020-03-11 16:49:18 -0700286 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800287 }
288
David Tolnay8f16ae72020-10-08 18:21:13 -0700289 include::write(out, needs_panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700290
David Tolnay99a95e62020-09-02 15:05:53 -0700291 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700292 out.next_section();
293 writeln!(out, "struct unsafe_bitcopy_t;");
294 }
295
David Tolnay84ddf9e2020-10-31 15:36:48 -0700296 if needs_rust_error {
297 out.begin_block("namespace");
298 writeln!(out, "template <typename T>");
299 writeln!(out, "class impl;");
300 out.end_block("namespace");
301 }
302
David Tolnay8f16ae72020-10-08 18:21:13 -0700303 include::write(out, needs_rust_string, "CXXBRIDGE05_RUST_STRING");
304 include::write(out, needs_rust_str, "CXXBRIDGE05_RUST_STR");
305 include::write(out, needs_rust_slice, "CXXBRIDGE05_RUST_SLICE");
306 include::write(out, needs_rust_box, "CXXBRIDGE05_RUST_BOX");
307 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
308 include::write(out, needs_rust_vec, "CXXBRIDGE05_RUST_VEC");
309 include::write(out, needs_rust_fn, "CXXBRIDGE05_RUST_FN");
310 include::write(out, needs_rust_error, "CXXBRIDGE05_RUST_ERROR");
311 include::write(out, needs_rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800312
313 if needs_manually_drop {
314 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700315 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800316 writeln!(out, "template <typename T>");
317 writeln!(out, "union ManuallyDrop {{");
318 writeln!(out, " T value;");
319 writeln!(
320 out,
321 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
322 );
323 writeln!(out, " ~ManuallyDrop() {{}}");
324 writeln!(out, "}};");
325 }
326
David Tolnay09011c32020-03-06 14:40:28 -0800327 if needs_maybe_uninit {
328 out.next_section();
329 writeln!(out, "template <typename T>");
330 writeln!(out, "union MaybeUninit {{");
331 writeln!(out, " T value;");
332 writeln!(out, " MaybeUninit() {{}}");
333 writeln!(out, " ~MaybeUninit() {{}}");
334 writeln!(out, "}};");
335 }
336
David Tolnayd68dfa82020-10-31 16:01:24 -0700337 out.begin_block("namespace");
338
David Tolnaya4eb9432020-10-31 20:32:19 -0700339 if needs_trycatch || needs_rust_error || needs_rust_str_new_unchecked || needs_rust_str_repr {
David Tolnayd68dfa82020-10-31 16:01:24 -0700340 out.begin_block("namespace repr");
341 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700342 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700343 writeln!(out, " size_t len;");
344 writeln!(out, "}};");
345 out.end_block("namespace repr");
346 }
347
David Tolnaya4eb9432020-10-31 20:32:19 -0700348 if needs_rust_str_new_unchecked || needs_rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700349 out.next_section();
350 writeln!(out, "template <>");
351 writeln!(out, "class impl<Str> final {{");
352 writeln!(out, "public:");
David Tolnaya4eb9432020-10-31 20:32:19 -0700353 if needs_rust_str_new_unchecked {
354 writeln!(
355 out,
356 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
357 );
358 writeln!(out, " Str str;");
359 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
360 writeln!(out, " str.len = repr.len;");
361 writeln!(out, " return str;");
362 writeln!(out, " }}");
363 }
364 if needs_rust_str_repr {
365 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
366 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
367 writeln!(out, " }}");
368 }
David Tolnay0356d332020-10-31 19:46:41 -0700369 writeln!(out, "}};");
370 }
371
David Tolnaya0c9bc72020-10-31 14:37:14 -0700372 if needs_rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700373 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700374 writeln!(out, "template <>");
375 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700376 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700377 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700378 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700379 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700380 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700381 writeln!(out, " return error;");
382 writeln!(out, " }}");
383 writeln!(out, "}};");
384 }
385
David Tolnayd68dfa82020-10-31 16:01:24 -0700386 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700387 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700388
David Tolnay5d121442020-03-17 22:14:40 -0700389 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700390 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700391 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700392 out.include.type_traits = true;
393 out.include.utility = true;
394 writeln!(out, "class missing {{}};");
395 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700396 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700397 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700398 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700399 writeln!(
400 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700401 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700402 );
David Tolnay04722332020-03-18 11:31:54 -0700403 writeln!(out, " missing>::value>::type");
404 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700405 writeln!(out, " func();");
406 writeln!(out, "}} catch (const ::std::exception &e) {{");
407 writeln!(out, " fail(e.what());");
408 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700409 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700410 }
411
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800412 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400413}
414
David Tolnaya7c2ea12020-10-30 21:32:53 -0700415fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700416 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700417 writeln!(out, "#ifndef {}", guard);
418 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400419 for line in strct.doc.to_string().lines() {
420 writeln!(out, "//{}", line);
421 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700422 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400423 for field in &strct.fields {
424 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700425 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400426 writeln!(out, "{};", field.ident);
427 }
428 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700429 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400430}
431
432fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
433 writeln!(out, "struct {};", ident);
434}
435
Adrian Taylorc8713432020-10-21 18:20:55 -0700436fn write_struct_using(out: &mut OutFile, ident: &CppName) {
437 writeln!(
438 out,
439 "using {} = {};",
440 ident.ident,
441 ident.to_fully_qualified()
442 );
David Tolnay8861bee2020-01-20 18:39:24 -0800443}
444
David Tolnaya7c2ea12020-10-30 21:32:53 -0700445fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700446 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700447 writeln!(out, "#ifndef {}", guard);
448 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700449 for line in ety.doc.to_string().lines() {
450 writeln!(out, "//{}", line);
451 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700452 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
453 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
454 writeln!(
455 out,
456 " {}(const {} &) = delete;",
457 ety.ident.cxx.ident, ety.ident.cxx.ident
458 );
Joel Galenson968738f2020-04-15 14:19:33 -0700459 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700460 write!(out, " ");
461 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700462 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700463 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700464 writeln!(out, ";");
465 }
466 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700467 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700468}
469
Joel Galensonc03402a2020-04-23 17:31:09 -0700470fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700471 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700472 writeln!(out, "#ifndef {}", guard);
473 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700474 for line in enm.doc.to_string().lines() {
475 writeln!(out, "//{}", line);
476 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700477 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700478 write_atom(out, enm.repr);
479 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700480 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700481 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700482 }
483 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700484 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700485}
486
Joel Galenson905eb2e2020-05-04 14:58:14 -0700487fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700488 write!(
489 out,
490 "static_assert(sizeof({}) == sizeof(",
491 enm.ident.cxx.ident
492 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700493 write_atom(out, enm.repr);
494 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700495 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700496 write!(out, "static_assert(static_cast<");
497 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700498 writeln!(
499 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700500 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700501 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700502 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700503 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700504}
505
Adrian Taylorc8713432020-10-21 18:20:55 -0700506fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700507 // NOTE: The following two static assertions are just nice-to-have and not
508 // necessary for soundness. That's because triviality is always declared by
509 // the user in the form of an unsafe impl of cxx::ExternType:
510 //
511 // unsafe impl ExternType for MyType {
512 // type Id = cxx::type_id!("...");
513 // type Kind = cxx::kind::Trivial;
514 // }
515 //
516 // Since the user went on the record with their unsafe impl to unsafely
517 // claim they KNOW that the type is trivial, it's fine for that to be on
518 // them if that were wrong.
519 //
520 // There may be a legitimate reason we'll want to remove these assertions
521 // for support of types that the programmer knows are Rust-movable despite
522 // not being recognized as such by the C++ type system due to a move
523 // constructor or destructor.
524
Adrian Taylorc8713432020-10-21 18:20:55 -0700525 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700526 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700527 writeln!(out, "static_assert(");
528 writeln!(
529 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700530 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700531 id,
532 );
533 writeln!(
534 out,
535 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
536 id,
537 );
538 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700539 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700540 writeln!(
541 out,
542 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
543 id,
544 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700545}
546
Adrian Taylorc8713432020-10-21 18:20:55 -0700547fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700548 let mut has_cxx_throws = false;
549 for api in apis {
550 if let Api::CxxFunction(efn) = api {
551 if efn.throws {
552 has_cxx_throws = true;
553 break;
554 }
555 }
556 }
557
558 if has_cxx_throws {
559 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700560 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700561 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700562 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700563 );
564 }
565}
566
David Tolnaya7c2ea12020-10-30 21:32:53 -0700567fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700568 if let Some(annotation) = impl_annotations {
569 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700570 }
David Tolnayebef4a22020-03-17 15:33:47 -0700571 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700572 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700573 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700574 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700575 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700576 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700577 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700578 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700579 if receiver.mutability.is_none() {
580 write!(out, "const ");
581 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700582 write!(
583 out,
584 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700585 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700586 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700587 }
David Tolnay7db73692019-10-20 14:51:12 -0400588 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700589 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400590 write!(out, ", ");
591 }
David Tolnaya46a2372020-03-06 10:03:48 -0800592 if arg.ty == RustString {
593 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700594 } else if let Type::RustVec(_) = arg.ty {
595 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800596 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700597 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400598 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700599 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400600 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700601 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400602 write!(out, ", ");
603 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700604 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400605 write!(out, "*return$");
606 }
607 writeln!(out, ") noexcept {{");
608 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700609 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700610 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700611 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700612 Some(receiver) => write!(
613 out,
614 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700615 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700616 efn.ident.rust
617 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700618 }
David Tolnay7db73692019-10-20 14:51:12 -0400619 for (i, arg) in efn.args.iter().enumerate() {
620 if i > 0 {
621 write!(out, ", ");
622 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700623 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400624 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700625 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700626 if let Some(receiver) = &efn.receiver {
627 if receiver.mutability.is_none() {
628 write!(out, " const");
629 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700630 }
631 write!(out, " = ");
632 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700633 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
634 Some(receiver) => write!(
635 out,
636 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700637 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700638 efn.ident.cxx.ident
639 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700640 }
641 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400642 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700643 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700644 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700645 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700646 writeln!(out, " [&] {{");
647 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700648 }
David Tolnay7db73692019-10-20 14:51:12 -0400649 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700650 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400651 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700652 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400653 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700654 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400655 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700656 }
657 match &efn.ret {
658 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700659 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700660 Some(Type::SliceRefU8(_)) if !indirect_return => {
661 write!(out, "::rust::Slice<uint8_t>::Repr(")
662 }
David Tolnay99642622020-03-25 13:07:35 -0700663 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400664 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700665 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700666 None => write!(out, "{}$(", efn.ident.rust),
667 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700668 }
David Tolnay7db73692019-10-20 14:51:12 -0400669 for (i, arg) in efn.args.iter().enumerate() {
670 if i > 0 {
671 write!(out, ", ");
672 }
673 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700674 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400675 write!(out, "::from_raw({})", arg.ident);
676 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700677 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400678 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700679 } else if let Type::Str(_) = arg.ty {
680 write!(
681 out,
682 "::rust::impl<::rust::Str>::new_unchecked({})",
683 arg.ident,
684 );
David Tolnaya46a2372020-03-06 10:03:48 -0800685 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800686 write!(
687 out,
688 "::rust::String(::rust::unsafe_bitcopy, *{})",
689 arg.ident,
690 );
David Tolnay313b10e2020-04-25 16:30:51 -0700691 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700692 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700693 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700694 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700695 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800696 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400697 } else {
698 write!(out, "{}", arg.ident);
699 }
700 }
701 write!(out, ")");
702 match &efn.ret {
703 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
704 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700705 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400706 _ => {}
707 }
708 if indirect_return {
709 write!(out, ")");
710 }
711 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700712 if efn.throws {
713 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700714 writeln!(out, " throw$.ptr = nullptr;");
715 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700716 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700717 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700718 writeln!(
719 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700720 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700721 );
David Tolnay5d121442020-03-17 22:14:40 -0700722 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700723 writeln!(out, " return throw$;");
724 }
David Tolnay7db73692019-10-20 14:51:12 -0400725 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700726 for arg in &efn.args {
727 if let Type::Fn(f) = &arg.ty {
728 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700729 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700730 }
731 }
732}
733
734fn write_function_pointer_trampoline(
735 out: &mut OutFile,
736 efn: &ExternFn,
737 var: &Ident,
738 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700739) {
740 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700741 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700742 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700743 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700744
745 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700746 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
747 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400748}
749
David Tolnaya7c2ea12020-10-30 21:32:53 -0700750fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
751 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700752 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700753 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700754}
755
756fn write_rust_function_decl_impl(
757 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700758 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700759 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700760 indirect_call: bool,
761) {
762 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700763 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700764 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700765 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700766 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700767 write!(out, "{}(", link_name);
768 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700769 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700770 if receiver.mutability.is_none() {
771 write!(out, "const ");
772 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700773 write!(
774 out,
775 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700776 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700777 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700778 needs_comma = true;
779 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700780 for arg in &sig.args {
781 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400782 write!(out, ", ");
783 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700784 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700785 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400786 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700787 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700788 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400789 write!(out, ", ");
790 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700791 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400792 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700793 needs_comma = true;
794 }
795 if indirect_call {
796 if needs_comma {
797 write!(out, ", ");
798 }
799 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400800 }
801 writeln!(out, ") noexcept;");
802}
803
David Tolnaya7c2ea12020-10-30 21:32:53 -0700804fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400805 for line in efn.doc.to_string().lines() {
806 writeln!(out, "//{}", line);
807 }
David Tolnaya73853b2020-04-20 01:19:56 -0700808 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700809 None => efn.ident.cxx.ident.to_string(),
810 Some(receiver) => format!(
811 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700812 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700813 efn.ident.cxx.ident
814 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700815 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700816 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700817 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700818 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700819}
820
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700821fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700822 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700823 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700824 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700825 indirect_call: bool,
826) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700827 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700828 write!(out, "{}(", local_name);
829 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400830 if i > 0 {
831 write!(out, ", ");
832 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700833 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400834 write!(out, "{}", arg.ident);
835 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700836 if indirect_call {
837 if !sig.args.is_empty() {
838 write!(out, ", ");
839 }
840 write!(out, "void *extern$");
841 }
David Tolnay1e548172020-03-16 13:37:09 -0700842 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700843 if let Some(receiver) = &sig.receiver {
844 if receiver.mutability.is_none() {
845 write!(out, " const");
846 }
847 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700848 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700849 write!(out, " noexcept");
850 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700851}
852
853fn write_rust_function_shim_impl(
854 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700855 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700856 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700857 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700858 indirect_call: bool,
859) {
860 if out.header && sig.receiver.is_some() {
861 // We've already defined this inside the struct.
862 return;
863 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700864 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400865 if out.header {
866 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700867 return;
David Tolnay7db73692019-10-20 14:51:12 -0400868 }
David Tolnay439cde22020-04-20 00:46:25 -0700869 writeln!(out, " {{");
870 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700871 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700872 out.include.utility = true;
873 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700874 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700875 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
876 }
877 }
878 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700879 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700880 if indirect_return {
881 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700882 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700883 writeln!(out, "> return$;");
884 write!(out, " ");
885 } else if let Some(ret) = &sig.ret {
886 write!(out, "return ");
887 match ret {
888 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700889 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700890 write!(out, "::from_raw(");
891 }
892 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700893 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700894 write!(out, "(");
895 }
896 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700897 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700898 _ => {}
899 }
900 }
901 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700902 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700903 }
904 write!(out, "{}(", invoke);
905 if sig.receiver.is_some() {
906 write!(out, "*this");
907 }
908 for (i, arg) in sig.args.iter().enumerate() {
909 if i > 0 || sig.receiver.is_some() {
910 write!(out, ", ");
911 }
912 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700913 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700914 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700915 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700916 _ => {}
917 }
918 write!(out, "{}", arg.ident);
919 match &arg.ty {
920 Type::RustBox(_) => write!(out, ".into_raw()"),
921 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700922 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700923 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700924 _ => {}
925 }
926 }
927 if indirect_return {
928 if !sig.args.is_empty() {
929 write!(out, ", ");
930 }
931 write!(out, "&return$.value");
932 }
933 if indirect_call {
934 if !sig.args.is_empty() || indirect_return {
935 write!(out, ", ");
936 }
937 write!(out, "extern$");
938 }
939 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400940 if !indirect_return {
941 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700942 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400943 write!(out, ")");
944 }
David Tolnay439cde22020-04-20 00:46:25 -0700945 }
946 }
947 writeln!(out, ";");
948 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700949 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700950 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700951 writeln!(out, " }}");
952 }
953 if indirect_return {
954 out.include.utility = true;
955 writeln!(out, " return ::std::move(return$.value);");
956 }
957 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400958}
959
David Tolnaya7c2ea12020-10-30 21:32:53 -0700960fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400961 match ty {
962 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700963 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400964 }
965}
966
David Tolnay75dca2e2020-03-25 20:17:52 -0700967fn indirect_return(sig: &Signature, types: &Types) -> bool {
968 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700969 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700970 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700971}
972
David Tolnaya7c2ea12020-10-30 21:32:53 -0700973fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700974 match ty {
975 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700976 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700977 write!(out, "*");
978 }
979 Type::Ref(ty) => {
980 if ty.mutability.is_none() {
981 write!(out, "const ");
982 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700983 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700984 write!(out, " *");
985 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700986 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700987 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700988 }
989}
990
David Tolnaya7c2ea12020-10-30 21:32:53 -0700991fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
992 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700993 match ty {
994 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700995 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700996 _ => write_space_after_type(out, ty),
997 }
998}
999
David Tolnaya7c2ea12020-10-30 21:32:53 -07001000fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001001 match ty {
1002 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(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 Tolnay4a441222020-01-25 16:24:27 -08001006 Some(Type::Ref(ty)) => {
1007 if ty.mutability.is_none() {
1008 write!(out, "const ");
1009 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001010 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001011 write!(out, " *");
1012 }
David Tolnay0356d332020-10-31 19:46:41 -07001013 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001014 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001015 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1016 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001017 }
1018}
1019
David Tolnaya7c2ea12020-10-30 21:32:53 -07001020fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001021 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001022 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001023 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001024 write!(out, "*");
1025 }
David Tolnay0356d332020-10-31 19:46:41 -07001026 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001027 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001028 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001029 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001030 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001031 write!(out, "*");
1032 }
1033 write!(out, "{}", arg.ident);
1034}
1035
David Tolnaya7c2ea12020-10-30 21:32:53 -07001036fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001037 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001038 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001039 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001040 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001041 },
1042 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001043 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001044 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001045 write!(out, ">");
1046 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001047 Type::RustVec(ty) => {
1048 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001049 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001050 write!(out, ">");
1051 }
David Tolnay7db73692019-10-20 14:51:12 -04001052 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001053 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001054 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001055 write!(out, ">");
1056 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001057 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001058 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001059 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001060 write!(out, ">");
1061 }
David Tolnay7db73692019-10-20 14:51:12 -04001062 Type::Ref(r) => {
1063 if r.mutability.is_none() {
1064 write!(out, "const ");
1065 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001066 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001067 write!(out, " &");
1068 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001069 Type::Slice(_) => {
1070 // For now, only U8 slices are supported, which are covered separately below
1071 unreachable!()
1072 }
David Tolnay7db73692019-10-20 14:51:12 -04001073 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001074 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001075 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001076 Type::SliceRefU8(_) => {
1077 write!(out, "::rust::Slice<uint8_t>");
1078 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001079 Type::Fn(f) => {
1080 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1081 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001082 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001083 None => write!(out, "void"),
1084 }
1085 write!(out, "(");
1086 for (i, arg) in f.args.iter().enumerate() {
1087 if i > 0 {
1088 write!(out, ", ");
1089 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001090 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001091 }
1092 write!(out, ")>");
1093 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001094 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001095 }
1096}
1097
David Tolnayf6a89f22020-05-10 23:39:27 -07001098fn write_atom(out: &mut OutFile, atom: Atom) {
1099 match atom {
1100 Bool => write!(out, "bool"),
1101 U8 => write!(out, "uint8_t"),
1102 U16 => write!(out, "uint16_t"),
1103 U32 => write!(out, "uint32_t"),
1104 U64 => write!(out, "uint64_t"),
1105 Usize => write!(out, "size_t"),
1106 I8 => write!(out, "int8_t"),
1107 I16 => write!(out, "int16_t"),
1108 I32 => write!(out, "int32_t"),
1109 I64 => write!(out, "int64_t"),
1110 Isize => write!(out, "::rust::isize"),
1111 F32 => write!(out, "float"),
1112 F64 => write!(out, "double"),
1113 CxxString => write!(out, "::std::string"),
1114 RustString => write!(out, "::rust::String"),
1115 }
1116}
1117
David Tolnaya7c2ea12020-10-30 21:32:53 -07001118fn write_type_space(out: &mut OutFile, ty: &Type) {
1119 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001120 write_space_after_type(out, ty);
1121}
1122
1123fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001124 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001125 Type::Ident(_)
1126 | Type::RustBox(_)
1127 | Type::UniquePtr(_)
1128 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001129 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001130 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001131 | Type::SliceRefU8(_)
1132 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001133 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001134 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001135 }
1136}
1137
David Tolnaycd08c442020-04-25 10:16:33 -07001138// Only called for legal referent types of unique_ptr and element types of
1139// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001140fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001141 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001142 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1143 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001144 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001145 }
1146}
1147
David Tolnayacdf20a2020-04-25 12:40:53 -07001148// Only called for legal referent types of unique_ptr and element types of
1149// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001150fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001151 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001152 Type::Ident(ident) => ident.to_symbol(types),
1153 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001154 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001155 }
1156}
1157
David Tolnaya7c2ea12020-10-30 21:32:53 -07001158fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001159 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001160 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001161 if let Type::RustBox(ty) = ty {
1162 if let Type::Ident(inner) = &ty.inner {
1163 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001164 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001165 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001166 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001167 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001168 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001169 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001170 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001171 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001172 }
David Tolnay7db73692019-10-20 14:51:12 -04001173 } else if let Type::UniquePtr(ptr) = ty {
1174 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001175 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001176 && (!out.types.aliases.contains_key(&inner.rust)
1177 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001178 {
David Tolnay7db73692019-10-20 14:51:12 -04001179 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001180 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001181 }
1182 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001183 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001184 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001185 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001186 && (!out.types.aliases.contains_key(&inner.rust)
1187 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001188 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001189 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001190 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001191 }
1192 }
1193 }
1194 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001195 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001196
David Tolnay750755e2020-03-01 13:04:08 -08001197 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001198 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001199 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001200 if let Type::RustBox(ty) = ty {
1201 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001202 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001203 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001204 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001205 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001206 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001207 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001208 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001209 }
David Tolnay7db73692019-10-20 14:51:12 -04001210 }
1211 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001212 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001213 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001214}
1215
Adrian Taylorc8713432020-10-21 18:20:55 -07001216fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1217 let inner = ident.to_fully_qualified();
1218 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001219
David Tolnay8f16ae72020-10-08 18:21:13 -07001220 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1221 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001222 writeln!(
1223 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001224 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001225 instance, inner,
1226 );
1227 writeln!(
1228 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001229 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001230 instance, inner,
1231 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001232 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001233}
1234
David Tolnaya7c2ea12020-10-30 21:32:53 -07001235fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001236 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001237 let inner = to_typename(&element, out.types);
1238 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001239
David Tolnay8f16ae72020-10-08 18:21:13 -07001240 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1241 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001242 writeln!(
1243 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001244 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001245 instance, inner,
1246 );
1247 writeln!(
1248 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001249 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001250 instance, inner,
1251 );
1252 writeln!(
1253 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001254 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001255 instance, inner,
1256 );
David Tolnay219c0792020-04-24 20:31:37 -07001257 writeln!(
1258 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001259 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001260 inner, instance,
1261 );
David Tolnay503d0192020-04-24 22:18:56 -07001262 writeln!(
1263 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001264 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001265 instance,
1266 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001267 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001268}
1269
Adrian Taylorc8713432020-10-21 18:20:55 -07001270fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1271 let inner = ident.to_fully_qualified();
1272 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001273
1274 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001275 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001276 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001277 writeln!(out, "}}");
1278
1279 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001280 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001281 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001282 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001283}
1284
David Tolnaya7c2ea12020-10-30 21:32:53 -07001285fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001286 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001287 let inner = to_typename(&element, out.types);
1288 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001289
Myron Ahneba35cf2020-02-05 19:41:51 +07001290 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001291 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001292 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001293 writeln!(out, "}}");
1294
1295 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001296 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1297 writeln!(
1298 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001299 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001300 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001301 );
1302 writeln!(out, "}}");
1303
1304 writeln!(out, "template <>");
1305 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001306 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001307 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001308
1309 writeln!(out, "template <>");
1310 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1311 writeln!(
1312 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001313 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001314 instance,
1315 );
1316 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001317
1318 writeln!(out, "template <>");
1319 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001320 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001321 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001322}
1323
David Tolnaya7c2ea12020-10-30 21:32:53 -07001324fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001325 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001326 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001327
David Tolnay8f16ae72020-10-08 18:21:13 -07001328 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1329 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001330
David Tolnaya7c2ea12020-10-30 21:32:53 -07001331 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001332
David Tolnay8f16ae72020-10-08 18:21:13 -07001333 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001334}
1335
1336// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001337fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001338 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001339 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001340 let inner = to_typename(ty, out.types);
1341 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001342
David Tolnay63da4d32020-04-25 09:41:12 -07001343 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001344 // Some aliases are to opaque types; some are to trivial types. We can't
1345 // know at code generation time, so we generate both C++ and Rust side
1346 // bindings for a "new" method anyway. But the Rust code can't be called
1347 // for Opaque types because the 'new' method is not implemented.
1348 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001349 out.types.structs.contains_key(&ident.rust)
1350 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001351 }
David Tolnay63da4d32020-04-25 09:41:12 -07001352 _ => false,
1353 };
1354
David Tolnay7db73692019-10-20 14:51:12 -04001355 writeln!(
1356 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001357 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001358 inner,
1359 );
1360 writeln!(
1361 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001362 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001363 inner,
1364 );
1365 writeln!(
1366 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001367 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001368 instance, inner,
1369 );
David Tolnay7e219b82020-03-01 13:14:51 -08001370 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001371 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001372 if can_construct_from_value {
1373 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001374 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001375 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001376 instance, inner, inner,
1377 );
David Tolnay63da4d32020-04-25 09:41:12 -07001378 writeln!(
1379 out,
1380 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1381 inner, inner,
1382 );
1383 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001384 }
David Tolnay7db73692019-10-20 14:51:12 -04001385 writeln!(
1386 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001387 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001388 instance, inner, inner,
1389 );
David Tolnay7e219b82020-03-01 13:14:51 -08001390 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001391 writeln!(out, "}}");
1392 writeln!(
1393 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001394 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001395 inner, instance, inner,
1396 );
1397 writeln!(out, " return ptr.get();");
1398 writeln!(out, "}}");
1399 writeln!(
1400 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001401 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001402 inner, instance, inner,
1403 );
1404 writeln!(out, " return ptr.release();");
1405 writeln!(out, "}}");
1406 writeln!(
1407 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001408 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001409 instance, inner,
1410 );
1411 writeln!(out, " ptr->~unique_ptr();");
1412 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001413}
Myron Ahneba35cf2020-02-05 19:41:51 +07001414
David Tolnaya7c2ea12020-10-30 21:32:53 -07001415fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001416 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001417 let inner = to_typename(&element, out.types);
1418 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001419
David Tolnay8f16ae72020-10-08 18:21:13 -07001420 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1421 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001422 writeln!(
1423 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001424 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001425 instance, inner,
1426 );
1427 writeln!(out, " return s.size();");
1428 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001429 writeln!(
1430 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001431 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001432 inner, instance, inner,
1433 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001434 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001435 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001436
David Tolnaya7c2ea12020-10-30 21:32:53 -07001437 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001438
David Tolnay8f16ae72020-10-08 18:21:13 -07001439 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001440}