blob: 1b30446739face198823e278b80d09f190f78adf [file] [log] [blame]
Adrian Taylor565ddf02020-10-29 21:12:36 -07001use crate::gen::namespace_organizer::NamespaceEntries;
David Tolnay8810a542020-10-31 21:39:22 -07002use crate::gen::out::OutFile;
3use 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::{
David Tolnay12af7e42020-10-31 21:09:23 -07007 mangle, Api, CppName, Enum, ExternFn, ExternType, IncludeKind, ResolvableName, Signature,
8 Struct, Type, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -07009};
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 {
David Tolnay8810a542020-10-31 21:39:22 -070018 writeln!(out.include, "#pragma once");
David Tolnay54702b92020-07-31 11:50:09 -070019 }
20
David Tolnay4aae7c02020-10-28 12:35:42 -070021 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040022 for api in apis {
23 if let Api::Include(include) = api {
David Tolnay353d98c2020-10-28 15:43:35 -070024 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040025 }
26 }
27
David Tolnay528200f2020-10-31 21:02:22 -070028 pick_includes_and_builtins(out, apis);
David Tolnayec66d112020-10-31 21:00:26 -070029 write_builtins(out);
David Tolnay7db73692019-10-20 14:51:12 -040030
David Tolnay7db73692019-10-20 14:51:12 -040031 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070032
Adrian Taylor565ddf02020-10-29 21:12:36 -070033 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070034
David Tolnay04b81652020-10-31 22:43:25 -070035 gen_namespace_forward_declarations(out, &apis_by_namespace);
36 gen_namespace_contents(out, &apis_by_namespace, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -070037
38 if !header {
39 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070040 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040041 }
42
David Tolnay8810a542020-10-31 21:39:22 -070043 write_includes(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070044
45 out_file
46}
47
David Tolnay04b81652020-10-31 22:43:25 -070048fn gen_namespace_forward_declarations(out: &mut OutFile, ns_entries: &NamespaceEntries) {
Adrian Taylor565ddf02020-10-29 21:12:36 -070049 let apis = ns_entries.entries();
Adrian Taylorc8713432020-10-21 18:20:55 -070050
David Tolnay7db73692019-10-20 14:51:12 -040051 out.next_section();
David Tolnay630af882020-10-31 22:03:47 -070052 for api in apis {
David Tolnay7db73692019-10-20 14:51:12 -040053 match api {
Adrian Taylorc8713432020-10-21 18:20:55 -070054 Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident),
55 Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx),
56 Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident),
David Tolnay7c295462020-04-25 12:45:07 -070057 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040058 }
59 }
60
Adrian Taylorf9213622020-10-31 22:25:42 -070061 out.next_section();
62
63 for (child_ns, child_ns_entries) in ns_entries.children() {
64 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -070065 gen_namespace_forward_declarations(out, child_ns_entries);
Adrian Taylorf9213622020-10-31 22:25:42 -070066 writeln!(out, "}} // namespace {}", child_ns);
67 }
68}
69
David Tolnay04b81652020-10-31 22:43:25 -070070fn gen_namespace_contents(out: &mut OutFile, ns_entries: &NamespaceEntries, opt: &Opt) {
Adrian Taylorf9213622020-10-31 22:25:42 -070071 let apis = ns_entries.entries();
72
David Tolnayf94bef12020-04-17 14:46:42 -070073 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070074 for api in apis {
David Tolnayf94bef12020-04-17 14:46:42 -070075 if let Api::RustFunction(efn) = api {
76 if let Some(receiver) = &efn.sig.receiver {
77 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070078 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070079 .or_insert_with(Vec::new)
80 .push(efn);
81 }
82 }
83 }
Joel Galenson968738f2020-04-15 14:19:33 -070084
David Tolnay7db73692019-10-20 14:51:12 -040085 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070086 match api {
87 Api::Struct(strct) => {
88 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070089 if !out.types.cxx.contains(&strct.ident.rust) {
David Tolnaya7c2ea12020-10-30 21:32:53 -070090 write_struct(out, strct);
David Tolnaya593d6e2020-08-29 19:48:08 -070091 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070092 }
Joel Galensonc03402a2020-04-23 17:31:09 -070093 Api::Enum(enm) => {
94 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070095 if out.types.cxx.contains(&enm.ident.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070096 check_enum(out, enm);
97 } else {
98 write_enum(out, enm);
99 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700100 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700101 Api::RustType(ety) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700102 if let Some(methods) = methods_for_type.get(&ety.ident.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -0700103 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700104 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700105 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700106 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700107 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400108 }
109 }
110
David Tolnayfabca772020-10-03 21:25:41 -0700111 out.next_section();
112 for api in apis {
113 if let Api::TypeAlias(ety) = api {
David Tolnay4d148422020-10-31 22:41:37 -0700114 if out.types.required_trivial.contains_key(&ety.ident.rust) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700115 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700116 }
117 }
118 }
119
David Tolnayce5a91f2020-10-31 22:42:08 -0700120 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400121 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700122 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400123 for api in apis {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700124 let (efn, write): (_, fn(_, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400125 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
126 Api::RustFunction(efn) => (efn, write_rust_function_decl),
127 _ => continue,
128 };
129 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700130 write(out, efn, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400131 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800132 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400133 }
134
135 for api in apis {
136 if let Api::RustFunction(efn) = api {
137 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700138 write_rust_function_shim(out, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400139 }
140 }
141
142 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700143
Adrian Taylor565ddf02020-10-29 21:12:36 -0700144 for (child_ns, child_ns_entries) in ns_entries.children() {
Adrian Taylorc8713432020-10-21 18:20:55 -0700145 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -0700146 gen_namespace_contents(out, child_ns_entries, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -0700147 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400148 }
David Tolnay7db73692019-10-20 14:51:12 -0400149}
150
David Tolnay528200f2020-10-31 21:02:22 -0700151fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700152 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400153 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700154 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700155 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
156 | Some(I64) => out.include.cstdint = true,
157 Some(Usize) => out.include.cstddef = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700158 Some(Isize) => {
159 out.include.basetsd = true;
160 out.builtin.rust_isize = true;
161 }
David Tolnay89e386d2020-10-03 19:02:19 -0700162 Some(CxxString) => out.include.string = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700163 Some(RustString) => {
164 out.include.array = true;
165 out.include.cstdint = true;
166 out.include.string = true;
167 out.builtin.rust_string = true;
168 }
169 Some(Bool) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700170 },
David Tolnayb7a7cb62020-03-17 21:18:40 -0700171 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700172 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700173 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700174 out.builtin.rust_box = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700175 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700176 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700177 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700178 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700179 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700180 out.builtin.panic = true;
181 out.builtin.rust_vec = true;
182 out.builtin.unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700183 }
David Tolnayb9da1462020-10-31 21:03:41 -0700184 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700185 Type::Str(_) => {
186 out.include.cstdint = true;
187 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700188 out.builtin.rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700189 }
David Tolnayb9da1462020-10-31 21:03:41 -0700190 Type::CxxVector(_) => out.include.vector = true,
David Tolnay75dca2e2020-03-25 20:17:52 -0700191 Type::Fn(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700192 out.builtin.rust_fn = true;
David Tolnay75dca2e2020-03-25 20:17:52 -0700193 }
David Tolnayb9da1462020-10-31 21:03:41 -0700194 Type::Slice(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700195 out.builtin.rust_slice = true;
David Tolnay4770b472020-04-14 16:32:59 -0700196 }
David Tolnayb9da1462020-10-31 21:03:41 -0700197 Type::SliceRefU8(_) => {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700198 out.include.cstdint = true;
David Tolnayb9da1462020-10-31 21:03:41 -0700199 out.builtin.rust_slice = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700200 }
201 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400202 }
203 }
204
David Tolnay09011c32020-03-06 14:40:28 -0800205 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700206 match api {
207 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700208 if efn.throws {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700209 out.builtin.trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700210 } else if let Some(Type::Str(_)) = efn.ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700211 out.builtin.rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700212 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700214 match arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700215 Type::Str(_) => out.builtin.rust_str_new_unchecked = true,
216 Type::RustVec(_) => out.builtin.unsafe_bitcopy = true,
217 _ => out.builtin.unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700218 }
David Tolnay09011c32020-03-06 14:40:28 -0800219 }
220 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 Api::RustFunction(efn) if !out.header => {
222 if efn.throws {
223 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700224 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700225 out.builtin.rust_str = true;
226 out.builtin.rust_error = true;
227 out.builtin.maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 }
229 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700230 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700231 out.builtin.manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700232 }
233 if let Type::Str(_) = arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700234 out.builtin.rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 }
236 }
237 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700238 if out.types.needs_indirect_abi(ret) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700239 out.builtin.maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700240 } else if let Type::Str(_) = ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700241 out.builtin.rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 }
David Tolnayf51447e2020-03-06 14:14:27 -0800243 }
244 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800246 }
247 }
David Tolnayec66d112020-10-31 21:00:26 -0700248}
David Tolnayf51447e2020-03-06 14:14:27 -0800249
David Tolnay8810a542020-10-31 21:39:22 -0700250fn write_includes(out: &mut OutFile) {
251 let include = &mut out.include;
252 let out = &mut include.content;
253
David Tolnay12af7e42020-10-31 21:09:23 -0700254 for include in &include.custom {
255 match include.kind {
256 IncludeKind::Quoted => {
257 writeln!(out, "#include \"{}\"", include.path.escape_default());
258 }
259 IncludeKind::Bracketed => {
260 writeln!(out, "#include <{}>", include.path);
261 }
262 }
263 }
264
265 if include.array {
266 writeln!(out, "#include <array>");
267 }
268 if include.cstddef {
269 writeln!(out, "#include <cstddef>");
270 }
271 if include.cstdint {
272 writeln!(out, "#include <cstdint>");
273 }
274 if include.cstring {
275 writeln!(out, "#include <cstring>");
276 }
277 if include.exception {
278 writeln!(out, "#include <exception>");
279 }
280 if include.memory {
281 writeln!(out, "#include <memory>");
282 }
283 if include.new {
284 writeln!(out, "#include <new>");
285 }
286 if include.string {
287 writeln!(out, "#include <string>");
288 }
289 if include.type_traits {
290 writeln!(out, "#include <type_traits>");
291 }
292 if include.utility {
293 writeln!(out, "#include <utility>");
294 }
295 if include.vector {
296 writeln!(out, "#include <vector>");
297 }
298 if include.basetsd {
299 writeln!(out, "#if defined(_WIN32)");
300 writeln!(out, "#include <basetsd.h>");
301 writeln!(out, "#endif");
302 }
303}
304
David Tolnayec66d112020-10-31 21:00:26 -0700305fn write_builtins(out: &mut OutFile) {
David Tolnayfd68e562020-10-31 21:59:56 -0700306 if out.builtin == Default::default() {
307 return;
David Tolnayf51447e2020-03-06 14:14:27 -0800308 }
309
David Tolnayfd68e562020-10-31 21:59:56 -0700310 let include = &mut out.include;
311 let builtin = &mut out.builtin;
312 let out = &mut builtin.content;
David Tolnay16ab1462020-09-02 15:10:09 -0700313
David Tolnayfd68e562020-10-31 21:59:56 -0700314 out.begin_block("namespace rust");
315 out.begin_block("inline namespace cxxbridge05");
316 writeln!(out, "// #include \"rust/cxx.h\"");
317
318 include::write(out, builtin.panic, "CXXBRIDGE05_PANIC");
319
320 if builtin.rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700321 out.next_section();
322 writeln!(out, "struct unsafe_bitcopy_t;");
323 }
324
David Tolnayfd68e562020-10-31 21:59:56 -0700325 if builtin.rust_error {
David Tolnay84ddf9e2020-10-31 15:36:48 -0700326 out.begin_block("namespace");
327 writeln!(out, "template <typename T>");
328 writeln!(out, "class impl;");
329 out.end_block("namespace");
330 }
331
David Tolnayfd68e562020-10-31 21:59:56 -0700332 include::write(out, builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
333 include::write(out, builtin.rust_str, "CXXBRIDGE05_RUST_STR");
334 include::write(out, builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
335 include::write(out, builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
336 include::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
337 include::write(out, builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
338 include::write(out, builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
339 include::write(out, builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
340 include::write(out, builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800341
David Tolnayfd68e562020-10-31 21:59:56 -0700342 if builtin.manually_drop {
David Tolnayf51447e2020-03-06 14:14:27 -0800343 out.next_section();
David Tolnayfd68e562020-10-31 21:59:56 -0700344 include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800345 writeln!(out, "template <typename T>");
346 writeln!(out, "union ManuallyDrop {{");
347 writeln!(out, " T value;");
348 writeln!(
349 out,
350 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
351 );
352 writeln!(out, " ~ManuallyDrop() {{}}");
353 writeln!(out, "}};");
354 }
355
David Tolnayfd68e562020-10-31 21:59:56 -0700356 if builtin.maybe_uninit {
David Tolnay09011c32020-03-06 14:40:28 -0800357 out.next_section();
358 writeln!(out, "template <typename T>");
359 writeln!(out, "union MaybeUninit {{");
360 writeln!(out, " T value;");
361 writeln!(out, " MaybeUninit() {{}}");
362 writeln!(out, " ~MaybeUninit() {{}}");
363 writeln!(out, "}};");
364 }
365
David Tolnayd68dfa82020-10-31 16:01:24 -0700366 out.begin_block("namespace");
367
David Tolnayfd68e562020-10-31 21:59:56 -0700368 if builtin.trycatch
369 || builtin.rust_error
370 || builtin.rust_str_new_unchecked
371 || builtin.rust_str_repr
David Tolnay3be0e1f2020-10-31 20:53:00 -0700372 {
David Tolnayd68dfa82020-10-31 16:01:24 -0700373 out.begin_block("namespace repr");
374 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700375 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700376 writeln!(out, " size_t len;");
377 writeln!(out, "}};");
378 out.end_block("namespace repr");
379 }
380
David Tolnayfd68e562020-10-31 21:59:56 -0700381 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700382 out.next_section();
383 writeln!(out, "template <>");
384 writeln!(out, "class impl<Str> final {{");
385 writeln!(out, "public:");
David Tolnayfd68e562020-10-31 21:59:56 -0700386 if builtin.rust_str_new_unchecked {
David Tolnaya4eb9432020-10-31 20:32:19 -0700387 writeln!(
388 out,
389 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
390 );
391 writeln!(out, " Str str;");
392 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
393 writeln!(out, " str.len = repr.len;");
394 writeln!(out, " return str;");
395 writeln!(out, " }}");
396 }
David Tolnayfd68e562020-10-31 21:59:56 -0700397 if builtin.rust_str_repr {
David Tolnaya4eb9432020-10-31 20:32:19 -0700398 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
399 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
400 writeln!(out, " }}");
401 }
David Tolnay0356d332020-10-31 19:46:41 -0700402 writeln!(out, "}};");
403 }
404
David Tolnayfd68e562020-10-31 21:59:56 -0700405 if builtin.rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700406 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700407 writeln!(out, "template <>");
408 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700409 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700410 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700411 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700412 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700413 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700414 writeln!(out, " return error;");
415 writeln!(out, " }}");
416 writeln!(out, "}};");
417 }
418
David Tolnayd68dfa82020-10-31 16:01:24 -0700419 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700420 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700421
David Tolnayfd68e562020-10-31 21:59:56 -0700422 if builtin.trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700423 out.begin_block("namespace behavior");
David Tolnayfd68e562020-10-31 21:59:56 -0700424 include.exception = true;
425 include.type_traits = true;
426 include.utility = true;
David Tolnay04722332020-03-18 11:31:54 -0700427 writeln!(out, "class missing {{}};");
428 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700429 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700430 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700431 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700432 writeln!(
433 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700434 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700435 );
David Tolnay04722332020-03-18 11:31:54 -0700436 writeln!(out, " missing>::value>::type");
437 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700438 writeln!(out, " func();");
439 writeln!(out, "}} catch (const ::std::exception &e) {{");
440 writeln!(out, " fail(e.what());");
441 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700442 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700443 }
444
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800445 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400446}
447
David Tolnaya7c2ea12020-10-30 21:32:53 -0700448fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700449 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700450 writeln!(out, "#ifndef {}", guard);
451 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400452 for line in strct.doc.to_string().lines() {
453 writeln!(out, "//{}", line);
454 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700455 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400456 for field in &strct.fields {
457 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700458 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400459 writeln!(out, "{};", field.ident);
460 }
461 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700462 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400463}
464
465fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
466 writeln!(out, "struct {};", ident);
467}
468
Adrian Taylorc8713432020-10-21 18:20:55 -0700469fn write_struct_using(out: &mut OutFile, ident: &CppName) {
470 writeln!(
471 out,
472 "using {} = {};",
473 ident.ident,
474 ident.to_fully_qualified()
475 );
David Tolnay8861bee2020-01-20 18:39:24 -0800476}
477
David Tolnaya7c2ea12020-10-30 21:32:53 -0700478fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700479 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700480 writeln!(out, "#ifndef {}", guard);
481 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700482 for line in ety.doc.to_string().lines() {
483 writeln!(out, "//{}", line);
484 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700485 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
486 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
487 writeln!(
488 out,
489 " {}(const {} &) = delete;",
490 ety.ident.cxx.ident, ety.ident.cxx.ident
491 );
Joel Galenson968738f2020-04-15 14:19:33 -0700492 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700493 write!(out, " ");
494 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700495 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700496 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700497 writeln!(out, ";");
498 }
499 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700500 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700501}
502
Joel Galensonc03402a2020-04-23 17:31:09 -0700503fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700504 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700505 writeln!(out, "#ifndef {}", guard);
506 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700507 for line in enm.doc.to_string().lines() {
508 writeln!(out, "//{}", line);
509 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700510 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700511 write_atom(out, enm.repr);
512 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700513 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700514 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700515 }
516 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700517 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700518}
519
Joel Galenson905eb2e2020-05-04 14:58:14 -0700520fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700521 write!(
522 out,
523 "static_assert(sizeof({}) == sizeof(",
524 enm.ident.cxx.ident
525 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700526 write_atom(out, enm.repr);
527 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700528 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700529 write!(out, "static_assert(static_cast<");
530 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700531 writeln!(
532 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700533 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700534 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700535 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700536 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700537}
538
Adrian Taylorc8713432020-10-21 18:20:55 -0700539fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700540 // NOTE: The following two static assertions are just nice-to-have and not
541 // necessary for soundness. That's because triviality is always declared by
542 // the user in the form of an unsafe impl of cxx::ExternType:
543 //
544 // unsafe impl ExternType for MyType {
545 // type Id = cxx::type_id!("...");
546 // type Kind = cxx::kind::Trivial;
547 // }
548 //
549 // Since the user went on the record with their unsafe impl to unsafely
550 // claim they KNOW that the type is trivial, it's fine for that to be on
551 // them if that were wrong.
552 //
553 // There may be a legitimate reason we'll want to remove these assertions
554 // for support of types that the programmer knows are Rust-movable despite
555 // not being recognized as such by the C++ type system due to a move
556 // constructor or destructor.
557
Adrian Taylorc8713432020-10-21 18:20:55 -0700558 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700559 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700560 writeln!(out, "static_assert(");
561 writeln!(
562 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700563 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700564 id,
565 );
566 writeln!(
567 out,
568 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
569 id,
570 );
571 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700572 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700573 writeln!(
574 out,
575 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
576 id,
577 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700578}
579
Adrian Taylorc8713432020-10-21 18:20:55 -0700580fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700581 let mut has_cxx_throws = false;
582 for api in apis {
583 if let Api::CxxFunction(efn) = api {
584 if efn.throws {
585 has_cxx_throws = true;
586 break;
587 }
588 }
589 }
590
591 if has_cxx_throws {
592 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700593 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700594 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700595 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700596 );
597 }
598}
599
David Tolnaya7c2ea12020-10-30 21:32:53 -0700600fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700601 if let Some(annotation) = impl_annotations {
602 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700603 }
David Tolnayebef4a22020-03-17 15:33:47 -0700604 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700605 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700606 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700607 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700608 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700609 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700610 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700611 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700612 if receiver.mutability.is_none() {
613 write!(out, "const ");
614 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700615 write!(
616 out,
617 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700618 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700619 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700620 }
David Tolnay7db73692019-10-20 14:51:12 -0400621 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700622 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400623 write!(out, ", ");
624 }
David Tolnaya46a2372020-03-06 10:03:48 -0800625 if arg.ty == RustString {
626 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700627 } else if let Type::RustVec(_) = arg.ty {
628 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800629 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700630 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400631 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700632 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400633 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700634 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400635 write!(out, ", ");
636 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700637 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400638 write!(out, "*return$");
639 }
640 writeln!(out, ") noexcept {{");
641 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700642 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700643 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700644 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700645 Some(receiver) => write!(
646 out,
647 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700648 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700649 efn.ident.rust
650 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700651 }
David Tolnay7db73692019-10-20 14:51:12 -0400652 for (i, arg) in efn.args.iter().enumerate() {
653 if i > 0 {
654 write!(out, ", ");
655 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700656 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400657 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700658 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700659 if let Some(receiver) = &efn.receiver {
660 if receiver.mutability.is_none() {
661 write!(out, " const");
662 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700663 }
664 write!(out, " = ");
665 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700666 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
667 Some(receiver) => write!(
668 out,
669 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700670 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700671 efn.ident.cxx.ident
672 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700673 }
674 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400675 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700676 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700677 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700678 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700679 writeln!(out, " [&] {{");
680 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700681 }
David Tolnay7db73692019-10-20 14:51:12 -0400682 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700683 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400684 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700685 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400686 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700687 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400688 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700689 }
690 match &efn.ret {
691 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700692 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700693 Some(Type::SliceRefU8(_)) if !indirect_return => {
694 write!(out, "::rust::Slice<uint8_t>::Repr(")
695 }
David Tolnay99642622020-03-25 13:07:35 -0700696 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400697 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700698 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700699 None => write!(out, "{}$(", efn.ident.rust),
700 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700701 }
David Tolnay7db73692019-10-20 14:51:12 -0400702 for (i, arg) in efn.args.iter().enumerate() {
703 if i > 0 {
704 write!(out, ", ");
705 }
706 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700707 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400708 write!(out, "::from_raw({})", arg.ident);
709 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700710 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400711 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700712 } else if let Type::Str(_) = arg.ty {
713 write!(
714 out,
715 "::rust::impl<::rust::Str>::new_unchecked({})",
716 arg.ident,
717 );
David Tolnaya46a2372020-03-06 10:03:48 -0800718 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800719 write!(
720 out,
721 "::rust::String(::rust::unsafe_bitcopy, *{})",
722 arg.ident,
723 );
David Tolnay313b10e2020-04-25 16:30:51 -0700724 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700725 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700726 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700727 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700728 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800729 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400730 } else {
731 write!(out, "{}", arg.ident);
732 }
733 }
734 write!(out, ")");
735 match &efn.ret {
736 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
737 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700738 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400739 _ => {}
740 }
741 if indirect_return {
742 write!(out, ")");
743 }
744 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700745 if efn.throws {
746 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700747 writeln!(out, " throw$.ptr = nullptr;");
748 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700749 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700750 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700751 writeln!(
752 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700753 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700754 );
David Tolnay5d121442020-03-17 22:14:40 -0700755 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700756 writeln!(out, " return throw$;");
757 }
David Tolnay7db73692019-10-20 14:51:12 -0400758 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700759 for arg in &efn.args {
760 if let Type::Fn(f) = &arg.ty {
761 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700762 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700763 }
764 }
765}
766
767fn write_function_pointer_trampoline(
768 out: &mut OutFile,
769 efn: &ExternFn,
770 var: &Ident,
771 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700772) {
773 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700774 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700775 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700776 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700777
778 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700779 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
780 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400781}
782
David Tolnaya7c2ea12020-10-30 21:32:53 -0700783fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
784 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700785 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700786 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700787}
788
789fn write_rust_function_decl_impl(
790 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700791 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700792 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700793 indirect_call: bool,
794) {
795 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700796 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700797 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700798 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700799 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700800 write!(out, "{}(", link_name);
801 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700802 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700803 if receiver.mutability.is_none() {
804 write!(out, "const ");
805 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700806 write!(
807 out,
808 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700809 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700810 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700811 needs_comma = true;
812 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700813 for arg in &sig.args {
814 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400815 write!(out, ", ");
816 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700817 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700818 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400819 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700820 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700821 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400822 write!(out, ", ");
823 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700824 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400825 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700826 needs_comma = true;
827 }
828 if indirect_call {
829 if needs_comma {
830 write!(out, ", ");
831 }
832 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400833 }
834 writeln!(out, ") noexcept;");
835}
836
David Tolnaya7c2ea12020-10-30 21:32:53 -0700837fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400838 for line in efn.doc.to_string().lines() {
839 writeln!(out, "//{}", line);
840 }
David Tolnaya73853b2020-04-20 01:19:56 -0700841 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700842 None => efn.ident.cxx.ident.to_string(),
843 Some(receiver) => format!(
844 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700845 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700846 efn.ident.cxx.ident
847 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700848 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700849 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700850 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700851 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700852}
853
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700854fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700855 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700856 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700857 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700858 indirect_call: bool,
859) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700860 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700861 write!(out, "{}(", local_name);
862 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400863 if i > 0 {
864 write!(out, ", ");
865 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700866 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400867 write!(out, "{}", arg.ident);
868 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700869 if indirect_call {
870 if !sig.args.is_empty() {
871 write!(out, ", ");
872 }
873 write!(out, "void *extern$");
874 }
David Tolnay1e548172020-03-16 13:37:09 -0700875 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700876 if let Some(receiver) = &sig.receiver {
877 if receiver.mutability.is_none() {
878 write!(out, " const");
879 }
880 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700881 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700882 write!(out, " noexcept");
883 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700884}
885
886fn write_rust_function_shim_impl(
887 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700888 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700889 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700890 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700891 indirect_call: bool,
892) {
893 if out.header && sig.receiver.is_some() {
894 // We've already defined this inside the struct.
895 return;
896 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700897 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400898 if out.header {
899 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700900 return;
David Tolnay7db73692019-10-20 14:51:12 -0400901 }
David Tolnay439cde22020-04-20 00:46:25 -0700902 writeln!(out, " {{");
903 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700904 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700905 out.include.utility = true;
906 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700907 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700908 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
909 }
910 }
911 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700912 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700913 if indirect_return {
914 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700915 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700916 writeln!(out, "> return$;");
917 write!(out, " ");
918 } else if let Some(ret) = &sig.ret {
919 write!(out, "return ");
920 match ret {
921 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700922 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700923 write!(out, "::from_raw(");
924 }
925 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700926 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700927 write!(out, "(");
928 }
929 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700930 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700931 _ => {}
932 }
933 }
934 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700935 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700936 }
937 write!(out, "{}(", invoke);
938 if sig.receiver.is_some() {
939 write!(out, "*this");
940 }
941 for (i, arg) in sig.args.iter().enumerate() {
942 if i > 0 || sig.receiver.is_some() {
943 write!(out, ", ");
944 }
945 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700946 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700947 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700948 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700949 _ => {}
950 }
951 write!(out, "{}", arg.ident);
952 match &arg.ty {
953 Type::RustBox(_) => write!(out, ".into_raw()"),
954 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700955 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700956 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700957 _ => {}
958 }
959 }
960 if indirect_return {
961 if !sig.args.is_empty() {
962 write!(out, ", ");
963 }
964 write!(out, "&return$.value");
965 }
966 if indirect_call {
967 if !sig.args.is_empty() || indirect_return {
968 write!(out, ", ");
969 }
970 write!(out, "extern$");
971 }
972 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400973 if !indirect_return {
974 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700975 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400976 write!(out, ")");
977 }
David Tolnay439cde22020-04-20 00:46:25 -0700978 }
979 }
980 writeln!(out, ";");
981 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700982 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700983 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700984 writeln!(out, " }}");
985 }
986 if indirect_return {
987 out.include.utility = true;
988 writeln!(out, " return ::std::move(return$.value);");
989 }
990 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400991}
992
David Tolnaya7c2ea12020-10-30 21:32:53 -0700993fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400994 match ty {
995 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700996 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400997 }
998}
999
David Tolnay75dca2e2020-03-25 20:17:52 -07001000fn indirect_return(sig: &Signature, types: &Types) -> bool {
1001 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001002 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001003 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001004}
1005
David Tolnaya7c2ea12020-10-30 21:32:53 -07001006fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001007 match ty {
1008 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001009 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001010 write!(out, "*");
1011 }
1012 Type::Ref(ty) => {
1013 if ty.mutability.is_none() {
1014 write!(out, "const ");
1015 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001016 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001017 write!(out, " *");
1018 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001019 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001020 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001021 }
1022}
1023
David Tolnaya7c2ea12020-10-30 21:32:53 -07001024fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1025 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001026 match ty {
1027 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001028 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001029 _ => write_space_after_type(out, ty),
1030 }
1031}
1032
David Tolnaya7c2ea12020-10-30 21:32:53 -07001033fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001034 match ty {
1035 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001036 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001037 write!(out, "*");
1038 }
David Tolnay4a441222020-01-25 16:24:27 -08001039 Some(Type::Ref(ty)) => {
1040 if ty.mutability.is_none() {
1041 write!(out, "const ");
1042 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001043 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001044 write!(out, " *");
1045 }
David Tolnay0356d332020-10-31 19:46:41 -07001046 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001047 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001048 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1049 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001050 }
1051}
1052
David Tolnaya7c2ea12020-10-30 21:32:53 -07001053fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001054 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001055 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001056 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001057 write!(out, "*");
1058 }
David Tolnay0356d332020-10-31 19:46:41 -07001059 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001060 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001061 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001062 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001063 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001064 write!(out, "*");
1065 }
1066 write!(out, "{}", arg.ident);
1067}
1068
David Tolnaya7c2ea12020-10-30 21:32:53 -07001069fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001070 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001071 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001072 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001073 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001074 },
1075 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001076 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001077 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001078 write!(out, ">");
1079 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001080 Type::RustVec(ty) => {
1081 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001082 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001083 write!(out, ">");
1084 }
David Tolnay7db73692019-10-20 14:51:12 -04001085 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001086 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001087 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001088 write!(out, ">");
1089 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001090 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001091 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001092 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001093 write!(out, ">");
1094 }
David Tolnay7db73692019-10-20 14:51:12 -04001095 Type::Ref(r) => {
1096 if r.mutability.is_none() {
1097 write!(out, "const ");
1098 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001099 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001100 write!(out, " &");
1101 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001102 Type::Slice(_) => {
1103 // For now, only U8 slices are supported, which are covered separately below
1104 unreachable!()
1105 }
David Tolnay7db73692019-10-20 14:51:12 -04001106 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001107 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001108 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001109 Type::SliceRefU8(_) => {
1110 write!(out, "::rust::Slice<uint8_t>");
1111 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001112 Type::Fn(f) => {
1113 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1114 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001115 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001116 None => write!(out, "void"),
1117 }
1118 write!(out, "(");
1119 for (i, arg) in f.args.iter().enumerate() {
1120 if i > 0 {
1121 write!(out, ", ");
1122 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001123 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001124 }
1125 write!(out, ")>");
1126 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001127 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001128 }
1129}
1130
David Tolnayf6a89f22020-05-10 23:39:27 -07001131fn write_atom(out: &mut OutFile, atom: Atom) {
1132 match atom {
1133 Bool => write!(out, "bool"),
1134 U8 => write!(out, "uint8_t"),
1135 U16 => write!(out, "uint16_t"),
1136 U32 => write!(out, "uint32_t"),
1137 U64 => write!(out, "uint64_t"),
1138 Usize => write!(out, "size_t"),
1139 I8 => write!(out, "int8_t"),
1140 I16 => write!(out, "int16_t"),
1141 I32 => write!(out, "int32_t"),
1142 I64 => write!(out, "int64_t"),
1143 Isize => write!(out, "::rust::isize"),
1144 F32 => write!(out, "float"),
1145 F64 => write!(out, "double"),
1146 CxxString => write!(out, "::std::string"),
1147 RustString => write!(out, "::rust::String"),
1148 }
1149}
1150
David Tolnaya7c2ea12020-10-30 21:32:53 -07001151fn write_type_space(out: &mut OutFile, ty: &Type) {
1152 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001153 write_space_after_type(out, ty);
1154}
1155
1156fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001157 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001158 Type::Ident(_)
1159 | Type::RustBox(_)
1160 | Type::UniquePtr(_)
1161 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001162 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001163 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001164 | Type::SliceRefU8(_)
1165 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001166 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001167 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001168 }
1169}
1170
David Tolnaycd08c442020-04-25 10:16:33 -07001171// Only called for legal referent types of unique_ptr and element types of
1172// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001173fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001174 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001175 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1176 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001177 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001178 }
1179}
1180
David Tolnayacdf20a2020-04-25 12:40:53 -07001181// Only called for legal referent types of unique_ptr and element types of
1182// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001183fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001184 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001185 Type::Ident(ident) => ident.to_symbol(types),
1186 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001187 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001188 }
1189}
1190
David Tolnaya7c2ea12020-10-30 21:32:53 -07001191fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001192 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001193 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001194 if let Type::RustBox(ty) = ty {
1195 if let Type::Ident(inner) = &ty.inner {
1196 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001197 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001198 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001199 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001200 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001201 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001202 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001203 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001204 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001205 }
David Tolnay7db73692019-10-20 14:51:12 -04001206 } else if let Type::UniquePtr(ptr) = ty {
1207 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001208 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001209 && (!out.types.aliases.contains_key(&inner.rust)
1210 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001211 {
David Tolnay7db73692019-10-20 14:51:12 -04001212 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001213 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001214 }
1215 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001216 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001217 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001218 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001219 && (!out.types.aliases.contains_key(&inner.rust)
1220 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001221 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001222 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001223 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001224 }
1225 }
1226 }
1227 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001228 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001229
David Tolnay750755e2020-03-01 13:04:08 -08001230 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001231 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001232 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001233 if let Type::RustBox(ty) = ty {
1234 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001235 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001236 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001237 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001238 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001239 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001240 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001241 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001242 }
David Tolnay7db73692019-10-20 14:51:12 -04001243 }
1244 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001245 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001246 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001247}
1248
Adrian Taylorc8713432020-10-21 18:20:55 -07001249fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1250 let inner = ident.to_fully_qualified();
1251 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001252
David Tolnay8f16ae72020-10-08 18:21:13 -07001253 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1254 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001255 writeln!(
1256 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001257 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001258 instance, inner,
1259 );
1260 writeln!(
1261 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001262 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001263 instance, inner,
1264 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001265 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001266}
1267
David Tolnaya7c2ea12020-10-30 21:32:53 -07001268fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001269 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001270 let inner = to_typename(&element, out.types);
1271 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001272
David Tolnay8f16ae72020-10-08 18:21:13 -07001273 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1274 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001275 writeln!(
1276 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001277 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001278 instance, inner,
1279 );
1280 writeln!(
1281 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001282 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001283 instance, inner,
1284 );
1285 writeln!(
1286 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001287 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001288 instance, inner,
1289 );
David Tolnay219c0792020-04-24 20:31:37 -07001290 writeln!(
1291 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001292 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001293 inner, instance,
1294 );
David Tolnay503d0192020-04-24 22:18:56 -07001295 writeln!(
1296 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001297 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001298 instance,
1299 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001300 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001301}
1302
Adrian Taylorc8713432020-10-21 18:20:55 -07001303fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1304 let inner = ident.to_fully_qualified();
1305 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001306
1307 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001308 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001309 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001310 writeln!(out, "}}");
1311
1312 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001313 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001314 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001315 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001316}
1317
David Tolnaya7c2ea12020-10-30 21:32:53 -07001318fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001319 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001320 let inner = to_typename(&element, out.types);
1321 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001322
Myron Ahneba35cf2020-02-05 19:41:51 +07001323 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001324 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001325 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001326 writeln!(out, "}}");
1327
1328 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001329 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1330 writeln!(
1331 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001332 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001333 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001334 );
1335 writeln!(out, "}}");
1336
1337 writeln!(out, "template <>");
1338 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001339 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001340 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001341
1342 writeln!(out, "template <>");
1343 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1344 writeln!(
1345 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001346 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001347 instance,
1348 );
1349 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001350
1351 writeln!(out, "template <>");
1352 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001353 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001354 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001355}
1356
David Tolnaya7c2ea12020-10-30 21:32:53 -07001357fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001358 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001359 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001360
David Tolnay8f16ae72020-10-08 18:21:13 -07001361 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1362 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001363
David Tolnaya7c2ea12020-10-30 21:32:53 -07001364 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001365
David Tolnay8f16ae72020-10-08 18:21:13 -07001366 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001367}
1368
1369// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001370fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001371 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001372 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001373 let inner = to_typename(ty, out.types);
1374 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001375
David Tolnay63da4d32020-04-25 09:41:12 -07001376 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001377 // Some aliases are to opaque types; some are to trivial types. We can't
1378 // know at code generation time, so we generate both C++ and Rust side
1379 // bindings for a "new" method anyway. But the Rust code can't be called
1380 // for Opaque types because the 'new' method is not implemented.
1381 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001382 out.types.structs.contains_key(&ident.rust)
1383 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001384 }
David Tolnay63da4d32020-04-25 09:41:12 -07001385 _ => false,
1386 };
1387
David Tolnay7db73692019-10-20 14:51:12 -04001388 writeln!(
1389 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001390 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001391 inner,
1392 );
1393 writeln!(
1394 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001395 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001396 inner,
1397 );
1398 writeln!(
1399 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001400 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001401 instance, inner,
1402 );
David Tolnay7e219b82020-03-01 13:14:51 -08001403 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001404 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001405 if can_construct_from_value {
1406 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001407 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001408 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001409 instance, inner, inner,
1410 );
David Tolnay63da4d32020-04-25 09:41:12 -07001411 writeln!(
1412 out,
1413 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1414 inner, inner,
1415 );
1416 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001417 }
David Tolnay7db73692019-10-20 14:51:12 -04001418 writeln!(
1419 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001420 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001421 instance, inner, inner,
1422 );
David Tolnay7e219b82020-03-01 13:14:51 -08001423 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001424 writeln!(out, "}}");
1425 writeln!(
1426 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001427 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001428 inner, instance, inner,
1429 );
1430 writeln!(out, " return ptr.get();");
1431 writeln!(out, "}}");
1432 writeln!(
1433 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001434 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001435 inner, instance, inner,
1436 );
1437 writeln!(out, " return ptr.release();");
1438 writeln!(out, "}}");
1439 writeln!(
1440 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001441 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001442 instance, inner,
1443 );
1444 writeln!(out, " ptr->~unique_ptr();");
1445 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001446}
Myron Ahneba35cf2020-02-05 19:41:51 +07001447
David Tolnaya7c2ea12020-10-30 21:32:53 -07001448fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001449 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001450 let inner = to_typename(&element, out.types);
1451 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001452
David Tolnay8f16ae72020-10-08 18:21:13 -07001453 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1454 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001455 writeln!(
1456 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001457 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001458 instance, inner,
1459 );
1460 writeln!(out, " return s.size();");
1461 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001462 writeln!(
1463 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001464 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001465 inner, instance, inner,
1466 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001467 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001468 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001469
David Tolnaya7c2ea12020-10-30 21:32:53 -07001470 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001471
David Tolnay8f16ae72020-10-08 18:21:13 -07001472 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001473}