blob: 9696a494755a1d3ac65481c62077460266fc2c44 [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 Tolnaye629c642020-10-31 22:02:09 -070021 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070022 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023 for api in apis {
24 if let Api::Include(include) = api {
David Tolnay353d98c2020-10-28 15:43:35 -070025 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040026 }
27 }
28
Adrian Taylor565ddf02020-10-29 21:12:36 -070029 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070030
David Tolnay04b81652020-10-31 22:43:25 -070031 gen_namespace_forward_declarations(out, &apis_by_namespace);
32 gen_namespace_contents(out, &apis_by_namespace, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -070033
34 if !header {
35 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070036 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040037 }
38
David Tolnaye629c642020-10-31 22:02:09 -070039 write_builtins(out);
David Tolnay8810a542020-10-31 21:39:22 -070040 write_includes(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070041
42 out_file
43}
44
David Tolnay04b81652020-10-31 22:43:25 -070045fn gen_namespace_forward_declarations(out: &mut OutFile, ns_entries: &NamespaceEntries) {
Adrian Taylor565ddf02020-10-29 21:12:36 -070046 let apis = ns_entries.entries();
Adrian Taylorc8713432020-10-21 18:20:55 -070047
David Tolnay7db73692019-10-20 14:51:12 -040048 out.next_section();
David Tolnay630af882020-10-31 22:03:47 -070049 for api in apis {
David Tolnay7db73692019-10-20 14:51:12 -040050 match api {
Adrian Taylorc8713432020-10-21 18:20:55 -070051 Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident),
52 Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx),
53 Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident),
David Tolnay7c295462020-04-25 12:45:07 -070054 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040055 }
56 }
57
Adrian Taylorf9213622020-10-31 22:25:42 -070058 out.next_section();
59
60 for (child_ns, child_ns_entries) in ns_entries.children() {
61 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -070062 gen_namespace_forward_declarations(out, child_ns_entries);
Adrian Taylorf9213622020-10-31 22:25:42 -070063 writeln!(out, "}} // namespace {}", child_ns);
64 }
65}
66
David Tolnay04b81652020-10-31 22:43:25 -070067fn gen_namespace_contents(out: &mut OutFile, ns_entries: &NamespaceEntries, opt: &Opt) {
Adrian Taylorf9213622020-10-31 22:25:42 -070068 let apis = ns_entries.entries();
69
David Tolnayf94bef12020-04-17 14:46:42 -070070 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070071 for api in apis {
David Tolnayf94bef12020-04-17 14:46:42 -070072 if let Api::RustFunction(efn) = api {
73 if let Some(receiver) = &efn.sig.receiver {
74 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070075 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070076 .or_insert_with(Vec::new)
77 .push(efn);
78 }
79 }
80 }
Joel Galenson968738f2020-04-15 14:19:33 -070081
David Tolnay7db73692019-10-20 14:51:12 -040082 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070083 match api {
84 Api::Struct(strct) => {
85 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070086 if !out.types.cxx.contains(&strct.ident.rust) {
David Tolnaya7c2ea12020-10-30 21:32:53 -070087 write_struct(out, strct);
David Tolnaya593d6e2020-08-29 19:48:08 -070088 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070089 }
Joel Galensonc03402a2020-04-23 17:31:09 -070090 Api::Enum(enm) => {
91 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070092 if out.types.cxx.contains(&enm.ident.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070093 check_enum(out, enm);
94 } else {
95 write_enum(out, enm);
96 }
Joel Galensonc03402a2020-04-23 17:31:09 -070097 }
David Tolnayc1fe0052020-04-17 15:15:06 -070098 Api::RustType(ety) => {
Adrian Taylorc8713432020-10-21 18:20:55 -070099 if let Some(methods) = methods_for_type.get(&ety.ident.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -0700100 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700101 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700102 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700103 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700104 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400105 }
106 }
107
David Tolnayfabca772020-10-03 21:25:41 -0700108 out.next_section();
109 for api in apis {
110 if let Api::TypeAlias(ety) = api {
David Tolnay4d148422020-10-31 22:41:37 -0700111 if out.types.required_trivial.contains_key(&ety.ident.rust) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700112 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700113 }
114 }
115 }
116
David Tolnayce5a91f2020-10-31 22:42:08 -0700117 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400118 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700119 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400120 for api in apis {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700121 let (efn, write): (_, fn(_, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400122 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
123 Api::RustFunction(efn) => (efn, write_rust_function_decl),
124 _ => continue,
125 };
126 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700127 write(out, efn, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400128 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800129 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400130 }
131
132 for api in apis {
133 if let Api::RustFunction(efn) = api {
134 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700135 write_rust_function_shim(out, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400136 }
137 }
138
139 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700140
Adrian Taylor565ddf02020-10-29 21:12:36 -0700141 for (child_ns, child_ns_entries) in ns_entries.children() {
Adrian Taylorc8713432020-10-21 18:20:55 -0700142 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -0700143 gen_namespace_contents(out, child_ns_entries, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -0700144 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400145 }
David Tolnay7db73692019-10-20 14:51:12 -0400146}
147
David Tolnay528200f2020-10-31 21:02:22 -0700148fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700149 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400150 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700151 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700152 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
153 | Some(I64) => out.include.cstdint = true,
154 Some(Usize) => out.include.cstddef = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700155 Some(Isize) => {
156 out.include.basetsd = true;
157 out.builtin.rust_isize = true;
158 }
David Tolnay89e386d2020-10-03 19:02:19 -0700159 Some(CxxString) => out.include.string = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700160 Some(RustString) => {
161 out.include.array = true;
162 out.include.cstdint = true;
163 out.include.string = true;
164 out.builtin.rust_string = true;
165 }
166 Some(Bool) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700167 },
David Tolnayb7a7cb62020-03-17 21:18:40 -0700168 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700169 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700170 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700171 out.builtin.rust_box = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700172 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700173 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700174 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700175 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700176 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700177 out.builtin.panic = true;
178 out.builtin.rust_vec = true;
179 out.builtin.unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700180 }
David Tolnayb9da1462020-10-31 21:03:41 -0700181 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700182 Type::Str(_) => {
183 out.include.cstdint = true;
184 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700185 out.builtin.rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700186 }
David Tolnayb9da1462020-10-31 21:03:41 -0700187 Type::CxxVector(_) => out.include.vector = true,
David Tolnay75dca2e2020-03-25 20:17:52 -0700188 Type::Fn(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700189 out.builtin.rust_fn = true;
David Tolnay75dca2e2020-03-25 20:17:52 -0700190 }
David Tolnayb9da1462020-10-31 21:03:41 -0700191 Type::Slice(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700192 out.builtin.rust_slice = true;
David Tolnay4770b472020-04-14 16:32:59 -0700193 }
David Tolnayb9da1462020-10-31 21:03:41 -0700194 Type::SliceRefU8(_) => {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700195 out.include.cstdint = true;
David Tolnayb9da1462020-10-31 21:03:41 -0700196 out.builtin.rust_slice = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700197 }
198 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400199 }
200 }
201
David Tolnay09011c32020-03-06 14:40:28 -0800202 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 match api {
204 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700205 if efn.throws {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700206 out.builtin.trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700207 } else if let Some(Type::Str(_)) = efn.ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700208 out.builtin.rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700209 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700210 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700211 match arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700212 Type::Str(_) => out.builtin.rust_str_new_unchecked = true,
213 Type::RustVec(_) => out.builtin.unsafe_bitcopy = true,
214 _ => out.builtin.unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700215 }
David Tolnay09011c32020-03-06 14:40:28 -0800216 }
217 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700218 Api::RustFunction(efn) if !out.header => {
219 if efn.throws {
220 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700221 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700222 out.builtin.rust_str = true;
223 out.builtin.rust_error = true;
224 out.builtin.maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700225 }
226 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700227 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700228 out.builtin.manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700229 }
230 if let Type::Str(_) = arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700231 out.builtin.rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700232 }
233 }
234 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700235 if out.types.needs_indirect_abi(ret) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700236 out.builtin.maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700237 } else if let Type::Str(_) = ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700238 out.builtin.rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 }
David Tolnayf51447e2020-03-06 14:14:27 -0800240 }
241 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800243 }
244 }
David Tolnayec66d112020-10-31 21:00:26 -0700245}
David Tolnayf51447e2020-03-06 14:14:27 -0800246
David Tolnay8810a542020-10-31 21:39:22 -0700247fn write_includes(out: &mut OutFile) {
248 let include = &mut out.include;
249 let out = &mut include.content;
250
David Tolnay12af7e42020-10-31 21:09:23 -0700251 for include in &include.custom {
252 match include.kind {
253 IncludeKind::Quoted => {
254 writeln!(out, "#include \"{}\"", include.path.escape_default());
255 }
256 IncludeKind::Bracketed => {
257 writeln!(out, "#include <{}>", include.path);
258 }
259 }
260 }
261
262 if include.array {
263 writeln!(out, "#include <array>");
264 }
265 if include.cstddef {
266 writeln!(out, "#include <cstddef>");
267 }
268 if include.cstdint {
269 writeln!(out, "#include <cstdint>");
270 }
271 if include.cstring {
272 writeln!(out, "#include <cstring>");
273 }
274 if include.exception {
275 writeln!(out, "#include <exception>");
276 }
277 if include.memory {
278 writeln!(out, "#include <memory>");
279 }
280 if include.new {
281 writeln!(out, "#include <new>");
282 }
283 if include.string {
284 writeln!(out, "#include <string>");
285 }
286 if include.type_traits {
287 writeln!(out, "#include <type_traits>");
288 }
289 if include.utility {
290 writeln!(out, "#include <utility>");
291 }
292 if include.vector {
293 writeln!(out, "#include <vector>");
294 }
295 if include.basetsd {
296 writeln!(out, "#if defined(_WIN32)");
297 writeln!(out, "#include <basetsd.h>");
298 writeln!(out, "#endif");
299 }
300}
301
David Tolnayec66d112020-10-31 21:00:26 -0700302fn write_builtins(out: &mut OutFile) {
David Tolnayfd68e562020-10-31 21:59:56 -0700303 if out.builtin == Default::default() {
304 return;
David Tolnayf51447e2020-03-06 14:14:27 -0800305 }
306
David Tolnayfd68e562020-10-31 21:59:56 -0700307 let include = &mut out.include;
308 let builtin = &mut out.builtin;
309 let out = &mut builtin.content;
David Tolnay16ab1462020-09-02 15:10:09 -0700310
David Tolnayfd68e562020-10-31 21:59:56 -0700311 out.begin_block("namespace rust");
312 out.begin_block("inline namespace cxxbridge05");
313 writeln!(out, "// #include \"rust/cxx.h\"");
314
315 include::write(out, builtin.panic, "CXXBRIDGE05_PANIC");
316
317 if builtin.rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700318 out.next_section();
319 writeln!(out, "struct unsafe_bitcopy_t;");
320 }
321
David Tolnayfd68e562020-10-31 21:59:56 -0700322 if builtin.rust_error {
David Tolnay84ddf9e2020-10-31 15:36:48 -0700323 out.begin_block("namespace");
324 writeln!(out, "template <typename T>");
325 writeln!(out, "class impl;");
326 out.end_block("namespace");
327 }
328
David Tolnayfd68e562020-10-31 21:59:56 -0700329 include::write(out, builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
330 include::write(out, builtin.rust_str, "CXXBRIDGE05_RUST_STR");
331 include::write(out, builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
332 include::write(out, builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
333 include::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
334 include::write(out, builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
335 include::write(out, builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
336 include::write(out, builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
337 include::write(out, builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800338
David Tolnayfd68e562020-10-31 21:59:56 -0700339 if builtin.manually_drop {
David Tolnayf51447e2020-03-06 14:14:27 -0800340 out.next_section();
David Tolnayfd68e562020-10-31 21:59:56 -0700341 include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800342 writeln!(out, "template <typename T>");
343 writeln!(out, "union ManuallyDrop {{");
344 writeln!(out, " T value;");
345 writeln!(
346 out,
347 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
348 );
349 writeln!(out, " ~ManuallyDrop() {{}}");
350 writeln!(out, "}};");
351 }
352
David Tolnayfd68e562020-10-31 21:59:56 -0700353 if builtin.maybe_uninit {
David Tolnay09011c32020-03-06 14:40:28 -0800354 out.next_section();
355 writeln!(out, "template <typename T>");
356 writeln!(out, "union MaybeUninit {{");
357 writeln!(out, " T value;");
358 writeln!(out, " MaybeUninit() {{}}");
359 writeln!(out, " ~MaybeUninit() {{}}");
360 writeln!(out, "}};");
361 }
362
David Tolnayd68dfa82020-10-31 16:01:24 -0700363 out.begin_block("namespace");
364
David Tolnayfd68e562020-10-31 21:59:56 -0700365 if builtin.trycatch
366 || builtin.rust_error
367 || builtin.rust_str_new_unchecked
368 || builtin.rust_str_repr
David Tolnay3be0e1f2020-10-31 20:53:00 -0700369 {
David Tolnayd68dfa82020-10-31 16:01:24 -0700370 out.begin_block("namespace repr");
371 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700372 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700373 writeln!(out, " size_t len;");
374 writeln!(out, "}};");
375 out.end_block("namespace repr");
376 }
377
David Tolnayfd68e562020-10-31 21:59:56 -0700378 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700379 out.next_section();
380 writeln!(out, "template <>");
381 writeln!(out, "class impl<Str> final {{");
382 writeln!(out, "public:");
David Tolnayfd68e562020-10-31 21:59:56 -0700383 if builtin.rust_str_new_unchecked {
David Tolnaya4eb9432020-10-31 20:32:19 -0700384 writeln!(
385 out,
386 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
387 );
388 writeln!(out, " Str str;");
389 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
390 writeln!(out, " str.len = repr.len;");
391 writeln!(out, " return str;");
392 writeln!(out, " }}");
393 }
David Tolnayfd68e562020-10-31 21:59:56 -0700394 if builtin.rust_str_repr {
David Tolnaya4eb9432020-10-31 20:32:19 -0700395 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
396 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
397 writeln!(out, " }}");
398 }
David Tolnay0356d332020-10-31 19:46:41 -0700399 writeln!(out, "}};");
400 }
401
David Tolnayfd68e562020-10-31 21:59:56 -0700402 if builtin.rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700403 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700404 writeln!(out, "template <>");
405 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700406 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700407 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700408 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700409 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700410 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700411 writeln!(out, " return error;");
412 writeln!(out, " }}");
413 writeln!(out, "}};");
414 }
415
David Tolnayd68dfa82020-10-31 16:01:24 -0700416 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700417 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700418
David Tolnayfd68e562020-10-31 21:59:56 -0700419 if builtin.trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700420 out.begin_block("namespace behavior");
David Tolnayfd68e562020-10-31 21:59:56 -0700421 include.exception = true;
422 include.type_traits = true;
423 include.utility = true;
David Tolnay04722332020-03-18 11:31:54 -0700424 writeln!(out, "class missing {{}};");
425 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700426 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700427 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700428 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700429 writeln!(
430 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700431 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700432 );
David Tolnay04722332020-03-18 11:31:54 -0700433 writeln!(out, " missing>::value>::type");
434 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700435 writeln!(out, " func();");
436 writeln!(out, "}} catch (const ::std::exception &e) {{");
437 writeln!(out, " fail(e.what());");
438 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700439 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700440 }
441
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800442 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400443}
444
David Tolnaya7c2ea12020-10-30 21:32:53 -0700445fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700446 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700447 writeln!(out, "#ifndef {}", guard);
448 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400449 for line in strct.doc.to_string().lines() {
450 writeln!(out, "//{}", line);
451 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700452 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400453 for field in &strct.fields {
454 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700455 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400456 writeln!(out, "{};", field.ident);
457 }
458 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700459 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400460}
461
462fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
463 writeln!(out, "struct {};", ident);
464}
465
Adrian Taylorc8713432020-10-21 18:20:55 -0700466fn write_struct_using(out: &mut OutFile, ident: &CppName) {
467 writeln!(
468 out,
469 "using {} = {};",
470 ident.ident,
471 ident.to_fully_qualified()
472 );
David Tolnay8861bee2020-01-20 18:39:24 -0800473}
474
David Tolnaya7c2ea12020-10-30 21:32:53 -0700475fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700476 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700477 writeln!(out, "#ifndef {}", guard);
478 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700479 for line in ety.doc.to_string().lines() {
480 writeln!(out, "//{}", line);
481 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700482 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
483 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
484 writeln!(
485 out,
486 " {}(const {} &) = delete;",
487 ety.ident.cxx.ident, ety.ident.cxx.ident
488 );
Joel Galenson968738f2020-04-15 14:19:33 -0700489 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700490 write!(out, " ");
491 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700492 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700493 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700494 writeln!(out, ";");
495 }
496 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700497 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700498}
499
Joel Galensonc03402a2020-04-23 17:31:09 -0700500fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700501 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700502 writeln!(out, "#ifndef {}", guard);
503 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700504 for line in enm.doc.to_string().lines() {
505 writeln!(out, "//{}", line);
506 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700507 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700508 write_atom(out, enm.repr);
509 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700510 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700511 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700512 }
513 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700514 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700515}
516
Joel Galenson905eb2e2020-05-04 14:58:14 -0700517fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700518 write!(
519 out,
520 "static_assert(sizeof({}) == sizeof(",
521 enm.ident.cxx.ident
522 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700523 write_atom(out, enm.repr);
524 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700525 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700526 write!(out, "static_assert(static_cast<");
527 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700528 writeln!(
529 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700530 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700531 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700532 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700533 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700534}
535
Adrian Taylorc8713432020-10-21 18:20:55 -0700536fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700537 // NOTE: The following two static assertions are just nice-to-have and not
538 // necessary for soundness. That's because triviality is always declared by
539 // the user in the form of an unsafe impl of cxx::ExternType:
540 //
541 // unsafe impl ExternType for MyType {
542 // type Id = cxx::type_id!("...");
543 // type Kind = cxx::kind::Trivial;
544 // }
545 //
546 // Since the user went on the record with their unsafe impl to unsafely
547 // claim they KNOW that the type is trivial, it's fine for that to be on
548 // them if that were wrong.
549 //
550 // There may be a legitimate reason we'll want to remove these assertions
551 // for support of types that the programmer knows are Rust-movable despite
552 // not being recognized as such by the C++ type system due to a move
553 // constructor or destructor.
554
Adrian Taylorc8713432020-10-21 18:20:55 -0700555 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700556 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700557 writeln!(out, "static_assert(");
558 writeln!(
559 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700560 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700561 id,
562 );
563 writeln!(
564 out,
565 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
566 id,
567 );
568 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700569 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700570 writeln!(
571 out,
572 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
573 id,
574 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700575}
576
Adrian Taylorc8713432020-10-21 18:20:55 -0700577fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700578 let mut has_cxx_throws = false;
579 for api in apis {
580 if let Api::CxxFunction(efn) = api {
581 if efn.throws {
582 has_cxx_throws = true;
583 break;
584 }
585 }
586 }
587
588 if has_cxx_throws {
589 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700590 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700591 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700592 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700593 );
594 }
595}
596
David Tolnaya7c2ea12020-10-30 21:32:53 -0700597fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700598 if let Some(annotation) = impl_annotations {
599 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700600 }
David Tolnayebef4a22020-03-17 15:33:47 -0700601 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700602 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700603 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700604 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700605 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700606 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700607 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700608 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700609 if receiver.mutability.is_none() {
610 write!(out, "const ");
611 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700612 write!(
613 out,
614 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700615 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700616 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700617 }
David Tolnay7db73692019-10-20 14:51:12 -0400618 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700619 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400620 write!(out, ", ");
621 }
David Tolnaya46a2372020-03-06 10:03:48 -0800622 if arg.ty == RustString {
623 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700624 } else if let Type::RustVec(_) = arg.ty {
625 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800626 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700627 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400628 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700629 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400630 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700631 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400632 write!(out, ", ");
633 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700634 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400635 write!(out, "*return$");
636 }
637 writeln!(out, ") noexcept {{");
638 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700639 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700640 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700641 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700642 Some(receiver) => write!(
643 out,
644 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700645 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700646 efn.ident.rust
647 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700648 }
David Tolnay7db73692019-10-20 14:51:12 -0400649 for (i, arg) in efn.args.iter().enumerate() {
650 if i > 0 {
651 write!(out, ", ");
652 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700653 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400654 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700655 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700656 if let Some(receiver) = &efn.receiver {
657 if receiver.mutability.is_none() {
658 write!(out, " const");
659 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700660 }
661 write!(out, " = ");
662 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700663 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
664 Some(receiver) => write!(
665 out,
666 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700667 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700668 efn.ident.cxx.ident
669 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700670 }
671 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400672 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700673 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700674 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700675 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700676 writeln!(out, " [&] {{");
677 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700678 }
David Tolnay7db73692019-10-20 14:51:12 -0400679 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700680 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400681 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700682 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400683 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700684 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400685 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700686 }
687 match &efn.ret {
688 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700689 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700690 Some(Type::SliceRefU8(_)) if !indirect_return => {
691 write!(out, "::rust::Slice<uint8_t>::Repr(")
692 }
David Tolnay99642622020-03-25 13:07:35 -0700693 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400694 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700695 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700696 None => write!(out, "{}$(", efn.ident.rust),
697 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700698 }
David Tolnay7db73692019-10-20 14:51:12 -0400699 for (i, arg) in efn.args.iter().enumerate() {
700 if i > 0 {
701 write!(out, ", ");
702 }
703 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700704 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400705 write!(out, "::from_raw({})", arg.ident);
706 } else if let Type::UniquePtr(_) = &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, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700709 } else if let Type::Str(_) = arg.ty {
710 write!(
711 out,
712 "::rust::impl<::rust::Str>::new_unchecked({})",
713 arg.ident,
714 );
David Tolnaya46a2372020-03-06 10:03:48 -0800715 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800716 write!(
717 out,
718 "::rust::String(::rust::unsafe_bitcopy, *{})",
719 arg.ident,
720 );
David Tolnay313b10e2020-04-25 16:30:51 -0700721 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700722 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700723 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700724 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700725 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800726 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400727 } else {
728 write!(out, "{}", arg.ident);
729 }
730 }
731 write!(out, ")");
732 match &efn.ret {
733 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
734 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700735 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400736 _ => {}
737 }
738 if indirect_return {
739 write!(out, ")");
740 }
741 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700742 if efn.throws {
743 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700744 writeln!(out, " throw$.ptr = nullptr;");
745 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700746 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700747 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700748 writeln!(
749 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700750 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700751 );
David Tolnay5d121442020-03-17 22:14:40 -0700752 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700753 writeln!(out, " return throw$;");
754 }
David Tolnay7db73692019-10-20 14:51:12 -0400755 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700756 for arg in &efn.args {
757 if let Type::Fn(f) = &arg.ty {
758 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700759 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700760 }
761 }
762}
763
764fn write_function_pointer_trampoline(
765 out: &mut OutFile,
766 efn: &ExternFn,
767 var: &Ident,
768 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700769) {
770 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700771 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700772 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700773 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700774
775 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700776 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
777 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400778}
779
David Tolnaya7c2ea12020-10-30 21:32:53 -0700780fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
781 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700782 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700783 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700784}
785
786fn write_rust_function_decl_impl(
787 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700788 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700789 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700790 indirect_call: bool,
791) {
792 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700793 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700794 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700795 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700796 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700797 write!(out, "{}(", link_name);
798 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700799 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700800 if receiver.mutability.is_none() {
801 write!(out, "const ");
802 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700803 write!(
804 out,
805 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700806 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700807 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700808 needs_comma = true;
809 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700810 for arg in &sig.args {
811 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400812 write!(out, ", ");
813 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700814 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700815 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400816 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700817 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700818 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400819 write!(out, ", ");
820 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700821 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400822 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700823 needs_comma = true;
824 }
825 if indirect_call {
826 if needs_comma {
827 write!(out, ", ");
828 }
829 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400830 }
831 writeln!(out, ") noexcept;");
832}
833
David Tolnaya7c2ea12020-10-30 21:32:53 -0700834fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400835 for line in efn.doc.to_string().lines() {
836 writeln!(out, "//{}", line);
837 }
David Tolnaya73853b2020-04-20 01:19:56 -0700838 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700839 None => efn.ident.cxx.ident.to_string(),
840 Some(receiver) => format!(
841 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700842 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700843 efn.ident.cxx.ident
844 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700845 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700846 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700847 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700848 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700849}
850
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700851fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700852 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700853 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700854 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700855 indirect_call: bool,
856) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700857 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700858 write!(out, "{}(", local_name);
859 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400860 if i > 0 {
861 write!(out, ", ");
862 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700863 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400864 write!(out, "{}", arg.ident);
865 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700866 if indirect_call {
867 if !sig.args.is_empty() {
868 write!(out, ", ");
869 }
870 write!(out, "void *extern$");
871 }
David Tolnay1e548172020-03-16 13:37:09 -0700872 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700873 if let Some(receiver) = &sig.receiver {
874 if receiver.mutability.is_none() {
875 write!(out, " const");
876 }
877 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700878 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700879 write!(out, " noexcept");
880 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700881}
882
883fn write_rust_function_shim_impl(
884 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700885 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700886 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700887 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700888 indirect_call: bool,
889) {
890 if out.header && sig.receiver.is_some() {
891 // We've already defined this inside the struct.
892 return;
893 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700894 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400895 if out.header {
896 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700897 return;
David Tolnay7db73692019-10-20 14:51:12 -0400898 }
David Tolnay439cde22020-04-20 00:46:25 -0700899 writeln!(out, " {{");
900 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700901 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700902 out.include.utility = true;
903 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700904 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700905 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
906 }
907 }
908 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700909 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700910 if indirect_return {
911 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700912 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700913 writeln!(out, "> return$;");
914 write!(out, " ");
915 } else if let Some(ret) = &sig.ret {
916 write!(out, "return ");
917 match ret {
918 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700919 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700920 write!(out, "::from_raw(");
921 }
922 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700923 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700924 write!(out, "(");
925 }
926 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700927 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700928 _ => {}
929 }
930 }
931 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700932 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700933 }
934 write!(out, "{}(", invoke);
935 if sig.receiver.is_some() {
936 write!(out, "*this");
937 }
938 for (i, arg) in sig.args.iter().enumerate() {
939 if i > 0 || sig.receiver.is_some() {
940 write!(out, ", ");
941 }
942 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700943 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700944 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700945 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700946 _ => {}
947 }
948 write!(out, "{}", arg.ident);
949 match &arg.ty {
950 Type::RustBox(_) => write!(out, ".into_raw()"),
951 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700952 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700953 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700954 _ => {}
955 }
956 }
957 if indirect_return {
958 if !sig.args.is_empty() {
959 write!(out, ", ");
960 }
961 write!(out, "&return$.value");
962 }
963 if indirect_call {
964 if !sig.args.is_empty() || indirect_return {
965 write!(out, ", ");
966 }
967 write!(out, "extern$");
968 }
969 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400970 if !indirect_return {
971 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700972 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400973 write!(out, ")");
974 }
David Tolnay439cde22020-04-20 00:46:25 -0700975 }
976 }
977 writeln!(out, ";");
978 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700979 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700980 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700981 writeln!(out, " }}");
982 }
983 if indirect_return {
984 out.include.utility = true;
985 writeln!(out, " return ::std::move(return$.value);");
986 }
987 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400988}
989
David Tolnaya7c2ea12020-10-30 21:32:53 -0700990fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400991 match ty {
992 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700993 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400994 }
995}
996
David Tolnay75dca2e2020-03-25 20:17:52 -0700997fn indirect_return(sig: &Signature, types: &Types) -> bool {
998 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700999 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001000 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001001}
1002
David Tolnaya7c2ea12020-10-30 21:32:53 -07001003fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001004 match ty {
1005 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001006 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001007 write!(out, "*");
1008 }
1009 Type::Ref(ty) => {
1010 if ty.mutability.is_none() {
1011 write!(out, "const ");
1012 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001013 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001014 write!(out, " *");
1015 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001016 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001017 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001018 }
1019}
1020
David Tolnaya7c2ea12020-10-30 21:32:53 -07001021fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1022 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001023 match ty {
1024 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001025 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001026 _ => write_space_after_type(out, ty),
1027 }
1028}
1029
David Tolnaya7c2ea12020-10-30 21:32:53 -07001030fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001031 match ty {
1032 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001033 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001034 write!(out, "*");
1035 }
David Tolnay4a441222020-01-25 16:24:27 -08001036 Some(Type::Ref(ty)) => {
1037 if ty.mutability.is_none() {
1038 write!(out, "const ");
1039 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001040 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001041 write!(out, " *");
1042 }
David Tolnay0356d332020-10-31 19:46:41 -07001043 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001044 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001045 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1046 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001047 }
1048}
1049
David Tolnaya7c2ea12020-10-30 21:32:53 -07001050fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001051 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001052 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001053 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001054 write!(out, "*");
1055 }
David Tolnay0356d332020-10-31 19:46:41 -07001056 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001057 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001058 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001059 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001060 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001061 write!(out, "*");
1062 }
1063 write!(out, "{}", arg.ident);
1064}
1065
David Tolnaya7c2ea12020-10-30 21:32:53 -07001066fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001067 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001068 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001069 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001070 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001071 },
1072 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001073 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001074 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001075 write!(out, ">");
1076 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001077 Type::RustVec(ty) => {
1078 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001079 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001080 write!(out, ">");
1081 }
David Tolnay7db73692019-10-20 14:51:12 -04001082 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001083 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001084 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001085 write!(out, ">");
1086 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001087 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001088 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001089 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001090 write!(out, ">");
1091 }
David Tolnay7db73692019-10-20 14:51:12 -04001092 Type::Ref(r) => {
1093 if r.mutability.is_none() {
1094 write!(out, "const ");
1095 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001096 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001097 write!(out, " &");
1098 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001099 Type::Slice(_) => {
1100 // For now, only U8 slices are supported, which are covered separately below
1101 unreachable!()
1102 }
David Tolnay7db73692019-10-20 14:51:12 -04001103 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001104 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001105 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001106 Type::SliceRefU8(_) => {
1107 write!(out, "::rust::Slice<uint8_t>");
1108 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001109 Type::Fn(f) => {
1110 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1111 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001112 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001113 None => write!(out, "void"),
1114 }
1115 write!(out, "(");
1116 for (i, arg) in f.args.iter().enumerate() {
1117 if i > 0 {
1118 write!(out, ", ");
1119 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001120 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001121 }
1122 write!(out, ")>");
1123 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001124 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001125 }
1126}
1127
David Tolnayf6a89f22020-05-10 23:39:27 -07001128fn write_atom(out: &mut OutFile, atom: Atom) {
1129 match atom {
1130 Bool => write!(out, "bool"),
1131 U8 => write!(out, "uint8_t"),
1132 U16 => write!(out, "uint16_t"),
1133 U32 => write!(out, "uint32_t"),
1134 U64 => write!(out, "uint64_t"),
1135 Usize => write!(out, "size_t"),
1136 I8 => write!(out, "int8_t"),
1137 I16 => write!(out, "int16_t"),
1138 I32 => write!(out, "int32_t"),
1139 I64 => write!(out, "int64_t"),
1140 Isize => write!(out, "::rust::isize"),
1141 F32 => write!(out, "float"),
1142 F64 => write!(out, "double"),
1143 CxxString => write!(out, "::std::string"),
1144 RustString => write!(out, "::rust::String"),
1145 }
1146}
1147
David Tolnaya7c2ea12020-10-30 21:32:53 -07001148fn write_type_space(out: &mut OutFile, ty: &Type) {
1149 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001150 write_space_after_type(out, ty);
1151}
1152
1153fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001154 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001155 Type::Ident(_)
1156 | Type::RustBox(_)
1157 | Type::UniquePtr(_)
1158 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001159 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001160 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001161 | Type::SliceRefU8(_)
1162 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001163 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001164 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001165 }
1166}
1167
David Tolnaycd08c442020-04-25 10:16:33 -07001168// Only called for legal referent types of unique_ptr and element types of
1169// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001170fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001171 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001172 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1173 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001174 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001175 }
1176}
1177
David Tolnayacdf20a2020-04-25 12:40:53 -07001178// Only called for legal referent types of unique_ptr and element types of
1179// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001180fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001181 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001182 Type::Ident(ident) => ident.to_symbol(types),
1183 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001184 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001185 }
1186}
1187
David Tolnaya7c2ea12020-10-30 21:32:53 -07001188fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001189 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001190 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001191 if let Type::RustBox(ty) = ty {
1192 if let Type::Ident(inner) = &ty.inner {
1193 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001194 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001195 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001196 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001197 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001198 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001199 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001200 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001201 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001202 }
David Tolnay7db73692019-10-20 14:51:12 -04001203 } else if let Type::UniquePtr(ptr) = ty {
1204 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001205 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001206 && (!out.types.aliases.contains_key(&inner.rust)
1207 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001208 {
David Tolnay7db73692019-10-20 14:51:12 -04001209 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001210 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001211 }
1212 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001213 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001214 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001215 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001216 && (!out.types.aliases.contains_key(&inner.rust)
1217 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001218 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001219 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001220 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001221 }
1222 }
1223 }
1224 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001225 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001226
David Tolnay750755e2020-03-01 13:04:08 -08001227 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001228 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001229 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001230 if let Type::RustBox(ty) = ty {
1231 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001232 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001233 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001234 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001235 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001236 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001237 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001238 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001239 }
David Tolnay7db73692019-10-20 14:51:12 -04001240 }
1241 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001242 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001243 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001244}
1245
Adrian Taylorc8713432020-10-21 18:20:55 -07001246fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1247 let inner = ident.to_fully_qualified();
1248 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001249
David Tolnay8f16ae72020-10-08 18:21:13 -07001250 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1251 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001252 writeln!(
1253 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001254 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001255 instance, inner,
1256 );
1257 writeln!(
1258 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001259 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001260 instance, inner,
1261 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001262 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001263}
1264
David Tolnaya7c2ea12020-10-30 21:32:53 -07001265fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001266 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001267 let inner = to_typename(&element, out.types);
1268 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001269
David Tolnay8f16ae72020-10-08 18:21:13 -07001270 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1271 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001272 writeln!(
1273 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001274 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001275 instance, inner,
1276 );
1277 writeln!(
1278 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001279 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001280 instance, inner,
1281 );
1282 writeln!(
1283 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001284 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001285 instance, inner,
1286 );
David Tolnay219c0792020-04-24 20:31:37 -07001287 writeln!(
1288 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001289 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001290 inner, instance,
1291 );
David Tolnay503d0192020-04-24 22:18:56 -07001292 writeln!(
1293 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001294 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001295 instance,
1296 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001297 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001298}
1299
Adrian Taylorc8713432020-10-21 18:20:55 -07001300fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1301 let inner = ident.to_fully_qualified();
1302 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001303
1304 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001305 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001306 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001307 writeln!(out, "}}");
1308
1309 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001310 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001311 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001312 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001313}
1314
David Tolnaya7c2ea12020-10-30 21:32:53 -07001315fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001316 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001317 let inner = to_typename(&element, out.types);
1318 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001319
Myron Ahneba35cf2020-02-05 19:41:51 +07001320 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001321 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001322 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001323 writeln!(out, "}}");
1324
1325 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001326 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1327 writeln!(
1328 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001329 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001330 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001331 );
1332 writeln!(out, "}}");
1333
1334 writeln!(out, "template <>");
1335 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001336 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001337 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001338
1339 writeln!(out, "template <>");
1340 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1341 writeln!(
1342 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001343 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001344 instance,
1345 );
1346 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001347
1348 writeln!(out, "template <>");
1349 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001350 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001351 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001352}
1353
David Tolnaya7c2ea12020-10-30 21:32:53 -07001354fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001355 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001356 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001357
David Tolnay8f16ae72020-10-08 18:21:13 -07001358 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1359 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001360
David Tolnaya7c2ea12020-10-30 21:32:53 -07001361 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001362
David Tolnay8f16ae72020-10-08 18:21:13 -07001363 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001364}
1365
1366// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001367fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001368 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001369 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001370 let inner = to_typename(ty, out.types);
1371 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001372
David Tolnay63da4d32020-04-25 09:41:12 -07001373 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001374 // Some aliases are to opaque types; some are to trivial types. We can't
1375 // know at code generation time, so we generate both C++ and Rust side
1376 // bindings for a "new" method anyway. But the Rust code can't be called
1377 // for Opaque types because the 'new' method is not implemented.
1378 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001379 out.types.structs.contains_key(&ident.rust)
1380 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001381 }
David Tolnay63da4d32020-04-25 09:41:12 -07001382 _ => false,
1383 };
1384
David Tolnay7db73692019-10-20 14:51:12 -04001385 writeln!(
1386 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001387 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001388 inner,
1389 );
1390 writeln!(
1391 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001392 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001393 inner,
1394 );
1395 writeln!(
1396 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001397 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001398 instance, inner,
1399 );
David Tolnay7e219b82020-03-01 13:14:51 -08001400 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001401 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001402 if can_construct_from_value {
1403 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001404 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001405 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001406 instance, inner, inner,
1407 );
David Tolnay63da4d32020-04-25 09:41:12 -07001408 writeln!(
1409 out,
1410 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1411 inner, inner,
1412 );
1413 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001414 }
David Tolnay7db73692019-10-20 14:51:12 -04001415 writeln!(
1416 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001417 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001418 instance, inner, inner,
1419 );
David Tolnay7e219b82020-03-01 13:14:51 -08001420 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001421 writeln!(out, "}}");
1422 writeln!(
1423 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001424 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001425 inner, instance, inner,
1426 );
1427 writeln!(out, " return ptr.get();");
1428 writeln!(out, "}}");
1429 writeln!(
1430 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001431 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001432 inner, instance, inner,
1433 );
1434 writeln!(out, " return ptr.release();");
1435 writeln!(out, "}}");
1436 writeln!(
1437 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001438 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001439 instance, inner,
1440 );
1441 writeln!(out, " ptr->~unique_ptr();");
1442 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001443}
Myron Ahneba35cf2020-02-05 19:41:51 +07001444
David Tolnaya7c2ea12020-10-30 21:32:53 -07001445fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001446 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001447 let inner = to_typename(&element, out.types);
1448 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001449
David Tolnay8f16ae72020-10-08 18:21:13 -07001450 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1451 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001452 writeln!(
1453 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001454 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001455 instance, inner,
1456 );
1457 writeln!(out, " return s.size();");
1458 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001459 writeln!(
1460 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001461 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001462 inner, instance, inner,
1463 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001464 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001465 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001466
David Tolnaya7c2ea12020-10-30 21:32:53 -07001467 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001468
David Tolnay8f16ae72020-10-08 18:21:13 -07001469 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001470}