blob: f32cb5b49185251d83193ee65aee2d6e391b1c99 [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 Tolnay750755e2020-03-01 13:04:08 -0800306 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700307 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800308
David Tolnay3be0e1f2020-10-31 20:53:00 -0700309 if out.builtin != Default::default() {
David Tolnay736cbca2020-03-11 16:49:18 -0700310 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800311 }
312
David Tolnay3be0e1f2020-10-31 20:53:00 -0700313 include::write(out, out.builtin.panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700314
David Tolnay3be0e1f2020-10-31 20:53:00 -0700315 if out.builtin.rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700316 out.next_section();
317 writeln!(out, "struct unsafe_bitcopy_t;");
318 }
319
David Tolnay3be0e1f2020-10-31 20:53:00 -0700320 if out.builtin.rust_error {
David Tolnay84ddf9e2020-10-31 15:36:48 -0700321 out.begin_block("namespace");
322 writeln!(out, "template <typename T>");
323 writeln!(out, "class impl;");
324 out.end_block("namespace");
325 }
326
David Tolnay3be0e1f2020-10-31 20:53:00 -0700327 include::write(out, out.builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
328 include::write(out, out.builtin.rust_str, "CXXBRIDGE05_RUST_STR");
329 include::write(out, out.builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
330 include::write(out, out.builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
331 include::write(out, out.builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
332 include::write(out, out.builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
333 include::write(out, out.builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
334 include::write(out, out.builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
335 include::write(out, out.builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800336
David Tolnay3be0e1f2020-10-31 20:53:00 -0700337 if out.builtin.manually_drop {
David Tolnayf51447e2020-03-06 14:14:27 -0800338 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700339 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800340 writeln!(out, "template <typename T>");
341 writeln!(out, "union ManuallyDrop {{");
342 writeln!(out, " T value;");
343 writeln!(
344 out,
345 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
346 );
347 writeln!(out, " ~ManuallyDrop() {{}}");
348 writeln!(out, "}};");
349 }
350
David Tolnay3be0e1f2020-10-31 20:53:00 -0700351 if out.builtin.maybe_uninit {
David Tolnay09011c32020-03-06 14:40:28 -0800352 out.next_section();
353 writeln!(out, "template <typename T>");
354 writeln!(out, "union MaybeUninit {{");
355 writeln!(out, " T value;");
356 writeln!(out, " MaybeUninit() {{}}");
357 writeln!(out, " ~MaybeUninit() {{}}");
358 writeln!(out, "}};");
359 }
360
David Tolnayd68dfa82020-10-31 16:01:24 -0700361 out.begin_block("namespace");
362
David Tolnay3be0e1f2020-10-31 20:53:00 -0700363 if out.builtin.trycatch
364 || out.builtin.rust_error
365 || out.builtin.rust_str_new_unchecked
366 || out.builtin.rust_str_repr
367 {
David Tolnayd68dfa82020-10-31 16:01:24 -0700368 out.begin_block("namespace repr");
369 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700370 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700371 writeln!(out, " size_t len;");
372 writeln!(out, "}};");
373 out.end_block("namespace repr");
374 }
375
David Tolnay3be0e1f2020-10-31 20:53:00 -0700376 if out.builtin.rust_str_new_unchecked || out.builtin.rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700377 out.next_section();
378 writeln!(out, "template <>");
379 writeln!(out, "class impl<Str> final {{");
380 writeln!(out, "public:");
David Tolnay3be0e1f2020-10-31 20:53:00 -0700381 if out.builtin.rust_str_new_unchecked {
David Tolnaya4eb9432020-10-31 20:32:19 -0700382 writeln!(
383 out,
384 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
385 );
386 writeln!(out, " Str str;");
387 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
388 writeln!(out, " str.len = repr.len;");
389 writeln!(out, " return str;");
390 writeln!(out, " }}");
391 }
David Tolnay3be0e1f2020-10-31 20:53:00 -0700392 if out.builtin.rust_str_repr {
David Tolnaya4eb9432020-10-31 20:32:19 -0700393 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
394 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
395 writeln!(out, " }}");
396 }
David Tolnay0356d332020-10-31 19:46:41 -0700397 writeln!(out, "}};");
398 }
399
David Tolnay3be0e1f2020-10-31 20:53:00 -0700400 if out.builtin.rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700401 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700402 writeln!(out, "template <>");
403 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700404 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700405 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700406 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700407 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700408 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700409 writeln!(out, " return error;");
410 writeln!(out, " }}");
411 writeln!(out, "}};");
412 }
413
David Tolnayd68dfa82020-10-31 16:01:24 -0700414 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700415 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700416
David Tolnay3be0e1f2020-10-31 20:53:00 -0700417 if out.builtin.trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700418 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700419 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700420 out.include.type_traits = true;
421 out.include.utility = true;
422 writeln!(out, "class missing {{}};");
423 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700424 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700425 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700426 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700427 writeln!(
428 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700429 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700430 );
David Tolnay04722332020-03-18 11:31:54 -0700431 writeln!(out, " missing>::value>::type");
432 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700433 writeln!(out, " func();");
434 writeln!(out, "}} catch (const ::std::exception &e) {{");
435 writeln!(out, " fail(e.what());");
436 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700437 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700438 }
439
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800440 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400441}
442
David Tolnaya7c2ea12020-10-30 21:32:53 -0700443fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700444 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700445 writeln!(out, "#ifndef {}", guard);
446 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400447 for line in strct.doc.to_string().lines() {
448 writeln!(out, "//{}", line);
449 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700450 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400451 for field in &strct.fields {
452 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700453 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400454 writeln!(out, "{};", field.ident);
455 }
456 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700457 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400458}
459
460fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
461 writeln!(out, "struct {};", ident);
462}
463
Adrian Taylorc8713432020-10-21 18:20:55 -0700464fn write_struct_using(out: &mut OutFile, ident: &CppName) {
465 writeln!(
466 out,
467 "using {} = {};",
468 ident.ident,
469 ident.to_fully_qualified()
470 );
David Tolnay8861bee2020-01-20 18:39:24 -0800471}
472
David Tolnaya7c2ea12020-10-30 21:32:53 -0700473fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700474 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700475 writeln!(out, "#ifndef {}", guard);
476 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700477 for line in ety.doc.to_string().lines() {
478 writeln!(out, "//{}", line);
479 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700480 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
481 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
482 writeln!(
483 out,
484 " {}(const {} &) = delete;",
485 ety.ident.cxx.ident, ety.ident.cxx.ident
486 );
Joel Galenson968738f2020-04-15 14:19:33 -0700487 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700488 write!(out, " ");
489 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700490 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700491 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700492 writeln!(out, ";");
493 }
494 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700495 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700496}
497
Joel Galensonc03402a2020-04-23 17:31:09 -0700498fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700499 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700500 writeln!(out, "#ifndef {}", guard);
501 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700502 for line in enm.doc.to_string().lines() {
503 writeln!(out, "//{}", line);
504 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700505 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700506 write_atom(out, enm.repr);
507 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700508 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700509 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700510 }
511 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700512 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700513}
514
Joel Galenson905eb2e2020-05-04 14:58:14 -0700515fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700516 write!(
517 out,
518 "static_assert(sizeof({}) == sizeof(",
519 enm.ident.cxx.ident
520 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700521 write_atom(out, enm.repr);
522 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700523 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700524 write!(out, "static_assert(static_cast<");
525 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700526 writeln!(
527 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700528 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700529 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700530 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700531 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700532}
533
Adrian Taylorc8713432020-10-21 18:20:55 -0700534fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700535 // NOTE: The following two static assertions are just nice-to-have and not
536 // necessary for soundness. That's because triviality is always declared by
537 // the user in the form of an unsafe impl of cxx::ExternType:
538 //
539 // unsafe impl ExternType for MyType {
540 // type Id = cxx::type_id!("...");
541 // type Kind = cxx::kind::Trivial;
542 // }
543 //
544 // Since the user went on the record with their unsafe impl to unsafely
545 // claim they KNOW that the type is trivial, it's fine for that to be on
546 // them if that were wrong.
547 //
548 // There may be a legitimate reason we'll want to remove these assertions
549 // for support of types that the programmer knows are Rust-movable despite
550 // not being recognized as such by the C++ type system due to a move
551 // constructor or destructor.
552
Adrian Taylorc8713432020-10-21 18:20:55 -0700553 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700554 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700555 writeln!(out, "static_assert(");
556 writeln!(
557 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700558 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700559 id,
560 );
561 writeln!(
562 out,
563 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
564 id,
565 );
566 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700567 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700568 writeln!(
569 out,
570 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
571 id,
572 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700573}
574
Adrian Taylorc8713432020-10-21 18:20:55 -0700575fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700576 let mut has_cxx_throws = false;
577 for api in apis {
578 if let Api::CxxFunction(efn) = api {
579 if efn.throws {
580 has_cxx_throws = true;
581 break;
582 }
583 }
584 }
585
586 if has_cxx_throws {
587 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700588 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700589 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700590 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700591 );
592 }
593}
594
David Tolnaya7c2ea12020-10-30 21:32:53 -0700595fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700596 if let Some(annotation) = impl_annotations {
597 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700598 }
David Tolnayebef4a22020-03-17 15:33:47 -0700599 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700600 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700601 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700602 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700603 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700604 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700605 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700606 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700607 if receiver.mutability.is_none() {
608 write!(out, "const ");
609 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700610 write!(
611 out,
612 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700613 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700614 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700615 }
David Tolnay7db73692019-10-20 14:51:12 -0400616 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700617 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400618 write!(out, ", ");
619 }
David Tolnaya46a2372020-03-06 10:03:48 -0800620 if arg.ty == RustString {
621 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700622 } else if let Type::RustVec(_) = arg.ty {
623 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800624 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700625 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400626 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700627 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400628 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700629 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400630 write!(out, ", ");
631 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700632 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400633 write!(out, "*return$");
634 }
635 writeln!(out, ") noexcept {{");
636 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700637 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700638 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700639 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700640 Some(receiver) => write!(
641 out,
642 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700643 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700644 efn.ident.rust
645 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700646 }
David Tolnay7db73692019-10-20 14:51:12 -0400647 for (i, arg) in efn.args.iter().enumerate() {
648 if i > 0 {
649 write!(out, ", ");
650 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700651 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400652 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700653 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700654 if let Some(receiver) = &efn.receiver {
655 if receiver.mutability.is_none() {
656 write!(out, " const");
657 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700658 }
659 write!(out, " = ");
660 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700661 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
662 Some(receiver) => write!(
663 out,
664 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700665 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700666 efn.ident.cxx.ident
667 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700668 }
669 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400670 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700671 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700672 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700673 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700674 writeln!(out, " [&] {{");
675 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700676 }
David Tolnay7db73692019-10-20 14:51:12 -0400677 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700678 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400679 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700680 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400681 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700682 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400683 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700684 }
685 match &efn.ret {
686 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700687 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700688 Some(Type::SliceRefU8(_)) if !indirect_return => {
689 write!(out, "::rust::Slice<uint8_t>::Repr(")
690 }
David Tolnay99642622020-03-25 13:07:35 -0700691 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400692 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700693 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700694 None => write!(out, "{}$(", efn.ident.rust),
695 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700696 }
David Tolnay7db73692019-10-20 14:51:12 -0400697 for (i, arg) in efn.args.iter().enumerate() {
698 if i > 0 {
699 write!(out, ", ");
700 }
701 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700702 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400703 write!(out, "::from_raw({})", arg.ident);
704 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700705 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400706 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700707 } else if let Type::Str(_) = arg.ty {
708 write!(
709 out,
710 "::rust::impl<::rust::Str>::new_unchecked({})",
711 arg.ident,
712 );
David Tolnaya46a2372020-03-06 10:03:48 -0800713 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800714 write!(
715 out,
716 "::rust::String(::rust::unsafe_bitcopy, *{})",
717 arg.ident,
718 );
David Tolnay313b10e2020-04-25 16:30:51 -0700719 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700720 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700721 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700722 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700723 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800724 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400725 } else {
726 write!(out, "{}", arg.ident);
727 }
728 }
729 write!(out, ")");
730 match &efn.ret {
731 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
732 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700733 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400734 _ => {}
735 }
736 if indirect_return {
737 write!(out, ")");
738 }
739 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700740 if efn.throws {
741 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700742 writeln!(out, " throw$.ptr = nullptr;");
743 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700744 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700745 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700746 writeln!(
747 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700748 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700749 );
David Tolnay5d121442020-03-17 22:14:40 -0700750 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700751 writeln!(out, " return throw$;");
752 }
David Tolnay7db73692019-10-20 14:51:12 -0400753 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700754 for arg in &efn.args {
755 if let Type::Fn(f) = &arg.ty {
756 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700757 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700758 }
759 }
760}
761
762fn write_function_pointer_trampoline(
763 out: &mut OutFile,
764 efn: &ExternFn,
765 var: &Ident,
766 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700767) {
768 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700769 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700770 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700771 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700772
773 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700774 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
775 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400776}
777
David Tolnaya7c2ea12020-10-30 21:32:53 -0700778fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
779 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700780 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700781 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700782}
783
784fn write_rust_function_decl_impl(
785 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700786 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700787 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700788 indirect_call: bool,
789) {
790 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700791 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700792 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700793 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700794 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700795 write!(out, "{}(", link_name);
796 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700797 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700798 if receiver.mutability.is_none() {
799 write!(out, "const ");
800 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700801 write!(
802 out,
803 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700804 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700805 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700806 needs_comma = true;
807 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700808 for arg in &sig.args {
809 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400810 write!(out, ", ");
811 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700812 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700813 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400814 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700815 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700816 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400817 write!(out, ", ");
818 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700819 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400820 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700821 needs_comma = true;
822 }
823 if indirect_call {
824 if needs_comma {
825 write!(out, ", ");
826 }
827 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400828 }
829 writeln!(out, ") noexcept;");
830}
831
David Tolnaya7c2ea12020-10-30 21:32:53 -0700832fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400833 for line in efn.doc.to_string().lines() {
834 writeln!(out, "//{}", line);
835 }
David Tolnaya73853b2020-04-20 01:19:56 -0700836 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700837 None => efn.ident.cxx.ident.to_string(),
838 Some(receiver) => format!(
839 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700840 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700841 efn.ident.cxx.ident
842 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700843 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700844 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700845 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700846 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700847}
848
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700849fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700850 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700851 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700852 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 indirect_call: bool,
854) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700855 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700856 write!(out, "{}(", local_name);
857 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400858 if i > 0 {
859 write!(out, ", ");
860 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700861 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400862 write!(out, "{}", arg.ident);
863 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700864 if indirect_call {
865 if !sig.args.is_empty() {
866 write!(out, ", ");
867 }
868 write!(out, "void *extern$");
869 }
David Tolnay1e548172020-03-16 13:37:09 -0700870 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700871 if let Some(receiver) = &sig.receiver {
872 if receiver.mutability.is_none() {
873 write!(out, " const");
874 }
875 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700876 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700877 write!(out, " noexcept");
878 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700879}
880
881fn write_rust_function_shim_impl(
882 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700883 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700884 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700885 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700886 indirect_call: bool,
887) {
888 if out.header && sig.receiver.is_some() {
889 // We've already defined this inside the struct.
890 return;
891 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700892 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400893 if out.header {
894 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700895 return;
David Tolnay7db73692019-10-20 14:51:12 -0400896 }
David Tolnay439cde22020-04-20 00:46:25 -0700897 writeln!(out, " {{");
898 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700899 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700900 out.include.utility = true;
901 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700902 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700903 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
904 }
905 }
906 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700907 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700908 if indirect_return {
909 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700910 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700911 writeln!(out, "> return$;");
912 write!(out, " ");
913 } else if let Some(ret) = &sig.ret {
914 write!(out, "return ");
915 match ret {
916 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700917 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700918 write!(out, "::from_raw(");
919 }
920 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700921 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700922 write!(out, "(");
923 }
924 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700925 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700926 _ => {}
927 }
928 }
929 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700930 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700931 }
932 write!(out, "{}(", invoke);
933 if sig.receiver.is_some() {
934 write!(out, "*this");
935 }
936 for (i, arg) in sig.args.iter().enumerate() {
937 if i > 0 || sig.receiver.is_some() {
938 write!(out, ", ");
939 }
940 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700941 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700942 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700943 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700944 _ => {}
945 }
946 write!(out, "{}", arg.ident);
947 match &arg.ty {
948 Type::RustBox(_) => write!(out, ".into_raw()"),
949 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700950 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700951 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700952 _ => {}
953 }
954 }
955 if indirect_return {
956 if !sig.args.is_empty() {
957 write!(out, ", ");
958 }
959 write!(out, "&return$.value");
960 }
961 if indirect_call {
962 if !sig.args.is_empty() || indirect_return {
963 write!(out, ", ");
964 }
965 write!(out, "extern$");
966 }
967 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400968 if !indirect_return {
969 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700970 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400971 write!(out, ")");
972 }
David Tolnay439cde22020-04-20 00:46:25 -0700973 }
974 }
975 writeln!(out, ";");
976 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700977 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700978 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700979 writeln!(out, " }}");
980 }
981 if indirect_return {
982 out.include.utility = true;
983 writeln!(out, " return ::std::move(return$.value);");
984 }
985 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400986}
987
David Tolnaya7c2ea12020-10-30 21:32:53 -0700988fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400989 match ty {
990 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700991 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400992 }
993}
994
David Tolnay75dca2e2020-03-25 20:17:52 -0700995fn indirect_return(sig: &Signature, types: &Types) -> bool {
996 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700997 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700998 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700999}
1000
David Tolnaya7c2ea12020-10-30 21:32:53 -07001001fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001002 match ty {
1003 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001004 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001005 write!(out, "*");
1006 }
1007 Type::Ref(ty) => {
1008 if ty.mutability.is_none() {
1009 write!(out, "const ");
1010 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001011 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001012 write!(out, " *");
1013 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001014 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001015 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001016 }
1017}
1018
David Tolnaya7c2ea12020-10-30 21:32:53 -07001019fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1020 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001021 match ty {
1022 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001023 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001024 _ => write_space_after_type(out, ty),
1025 }
1026}
1027
David Tolnaya7c2ea12020-10-30 21:32:53 -07001028fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001029 match ty {
1030 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001031 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001032 write!(out, "*");
1033 }
David Tolnay4a441222020-01-25 16:24:27 -08001034 Some(Type::Ref(ty)) => {
1035 if ty.mutability.is_none() {
1036 write!(out, "const ");
1037 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001038 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001039 write!(out, " *");
1040 }
David Tolnay0356d332020-10-31 19:46:41 -07001041 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001042 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001043 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1044 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001045 }
1046}
1047
David Tolnaya7c2ea12020-10-30 21:32:53 -07001048fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001049 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001050 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001051 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001052 write!(out, "*");
1053 }
David Tolnay0356d332020-10-31 19:46:41 -07001054 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001055 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001056 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001057 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001058 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001059 write!(out, "*");
1060 }
1061 write!(out, "{}", arg.ident);
1062}
1063
David Tolnaya7c2ea12020-10-30 21:32:53 -07001064fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001065 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001066 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001067 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001068 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001069 },
1070 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001071 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001072 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001073 write!(out, ">");
1074 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001075 Type::RustVec(ty) => {
1076 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001077 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001078 write!(out, ">");
1079 }
David Tolnay7db73692019-10-20 14:51:12 -04001080 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001081 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001082 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001083 write!(out, ">");
1084 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001085 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001086 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001087 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001088 write!(out, ">");
1089 }
David Tolnay7db73692019-10-20 14:51:12 -04001090 Type::Ref(r) => {
1091 if r.mutability.is_none() {
1092 write!(out, "const ");
1093 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001094 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001095 write!(out, " &");
1096 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001097 Type::Slice(_) => {
1098 // For now, only U8 slices are supported, which are covered separately below
1099 unreachable!()
1100 }
David Tolnay7db73692019-10-20 14:51:12 -04001101 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001102 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001103 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001104 Type::SliceRefU8(_) => {
1105 write!(out, "::rust::Slice<uint8_t>");
1106 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001107 Type::Fn(f) => {
1108 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1109 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001110 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001111 None => write!(out, "void"),
1112 }
1113 write!(out, "(");
1114 for (i, arg) in f.args.iter().enumerate() {
1115 if i > 0 {
1116 write!(out, ", ");
1117 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001118 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001119 }
1120 write!(out, ")>");
1121 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001122 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001123 }
1124}
1125
David Tolnayf6a89f22020-05-10 23:39:27 -07001126fn write_atom(out: &mut OutFile, atom: Atom) {
1127 match atom {
1128 Bool => write!(out, "bool"),
1129 U8 => write!(out, "uint8_t"),
1130 U16 => write!(out, "uint16_t"),
1131 U32 => write!(out, "uint32_t"),
1132 U64 => write!(out, "uint64_t"),
1133 Usize => write!(out, "size_t"),
1134 I8 => write!(out, "int8_t"),
1135 I16 => write!(out, "int16_t"),
1136 I32 => write!(out, "int32_t"),
1137 I64 => write!(out, "int64_t"),
1138 Isize => write!(out, "::rust::isize"),
1139 F32 => write!(out, "float"),
1140 F64 => write!(out, "double"),
1141 CxxString => write!(out, "::std::string"),
1142 RustString => write!(out, "::rust::String"),
1143 }
1144}
1145
David Tolnaya7c2ea12020-10-30 21:32:53 -07001146fn write_type_space(out: &mut OutFile, ty: &Type) {
1147 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001148 write_space_after_type(out, ty);
1149}
1150
1151fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001152 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001153 Type::Ident(_)
1154 | Type::RustBox(_)
1155 | Type::UniquePtr(_)
1156 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001157 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001158 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001159 | Type::SliceRefU8(_)
1160 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001161 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001162 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001163 }
1164}
1165
David Tolnaycd08c442020-04-25 10:16:33 -07001166// Only called for legal referent types of unique_ptr and element types of
1167// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001168fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001169 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001170 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1171 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001172 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001173 }
1174}
1175
David Tolnayacdf20a2020-04-25 12:40:53 -07001176// Only called for legal referent types of unique_ptr and element types of
1177// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001178fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001179 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001180 Type::Ident(ident) => ident.to_symbol(types),
1181 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001182 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001183 }
1184}
1185
David Tolnaya7c2ea12020-10-30 21:32:53 -07001186fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001187 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001188 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001189 if let Type::RustBox(ty) = ty {
1190 if let Type::Ident(inner) = &ty.inner {
1191 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001192 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001193 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001194 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001195 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001196 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001197 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001198 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001199 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001200 }
David Tolnay7db73692019-10-20 14:51:12 -04001201 } else if let Type::UniquePtr(ptr) = ty {
1202 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001203 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001204 && (!out.types.aliases.contains_key(&inner.rust)
1205 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001206 {
David Tolnay7db73692019-10-20 14:51:12 -04001207 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001208 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001209 }
1210 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001211 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001212 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001213 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001214 && (!out.types.aliases.contains_key(&inner.rust)
1215 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001216 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001217 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001218 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001219 }
1220 }
1221 }
1222 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001223 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001224
David Tolnay750755e2020-03-01 13:04:08 -08001225 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001226 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001227 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001228 if let Type::RustBox(ty) = ty {
1229 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001230 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001231 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001232 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001233 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001234 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001235 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001236 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001237 }
David Tolnay7db73692019-10-20 14:51:12 -04001238 }
1239 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001240 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001241 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001242}
1243
Adrian Taylorc8713432020-10-21 18:20:55 -07001244fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1245 let inner = ident.to_fully_qualified();
1246 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001247
David Tolnay8f16ae72020-10-08 18:21:13 -07001248 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1249 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001250 writeln!(
1251 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001252 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001253 instance, inner,
1254 );
1255 writeln!(
1256 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001257 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001258 instance, inner,
1259 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001260 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001261}
1262
David Tolnaya7c2ea12020-10-30 21:32:53 -07001263fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001264 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001265 let inner = to_typename(&element, out.types);
1266 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001267
David Tolnay8f16ae72020-10-08 18:21:13 -07001268 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1269 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001270 writeln!(
1271 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001272 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001273 instance, inner,
1274 );
1275 writeln!(
1276 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001277 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001278 instance, inner,
1279 );
1280 writeln!(
1281 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001282 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001283 instance, inner,
1284 );
David Tolnay219c0792020-04-24 20:31:37 -07001285 writeln!(
1286 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001287 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001288 inner, instance,
1289 );
David Tolnay503d0192020-04-24 22:18:56 -07001290 writeln!(
1291 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001292 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001293 instance,
1294 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001295 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001296}
1297
Adrian Taylorc8713432020-10-21 18:20:55 -07001298fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1299 let inner = ident.to_fully_qualified();
1300 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001301
1302 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001303 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001304 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001305 writeln!(out, "}}");
1306
1307 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001308 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001309 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001310 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001311}
1312
David Tolnaya7c2ea12020-10-30 21:32:53 -07001313fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001314 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001315 let inner = to_typename(&element, out.types);
1316 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001317
Myron Ahneba35cf2020-02-05 19:41:51 +07001318 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001319 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001320 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001321 writeln!(out, "}}");
1322
1323 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001324 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1325 writeln!(
1326 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001327 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001328 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001329 );
1330 writeln!(out, "}}");
1331
1332 writeln!(out, "template <>");
1333 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001334 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001335 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001336
1337 writeln!(out, "template <>");
1338 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1339 writeln!(
1340 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001341 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001342 instance,
1343 );
1344 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001345
1346 writeln!(out, "template <>");
1347 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001348 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001349 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001350}
1351
David Tolnaya7c2ea12020-10-30 21:32:53 -07001352fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001353 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001354 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001355
David Tolnay8f16ae72020-10-08 18:21:13 -07001356 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1357 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001358
David Tolnaya7c2ea12020-10-30 21:32:53 -07001359 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001360
David Tolnay8f16ae72020-10-08 18:21:13 -07001361 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001362}
1363
1364// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001365fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001366 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001367 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001368 let inner = to_typename(ty, out.types);
1369 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001370
David Tolnay63da4d32020-04-25 09:41:12 -07001371 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001372 // Some aliases are to opaque types; some are to trivial types. We can't
1373 // know at code generation time, so we generate both C++ and Rust side
1374 // bindings for a "new" method anyway. But the Rust code can't be called
1375 // for Opaque types because the 'new' method is not implemented.
1376 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001377 out.types.structs.contains_key(&ident.rust)
1378 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001379 }
David Tolnay63da4d32020-04-25 09:41:12 -07001380 _ => false,
1381 };
1382
David Tolnay7db73692019-10-20 14:51:12 -04001383 writeln!(
1384 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001385 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001386 inner,
1387 );
1388 writeln!(
1389 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001390 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001391 inner,
1392 );
1393 writeln!(
1394 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001395 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001396 instance, inner,
1397 );
David Tolnay7e219b82020-03-01 13:14:51 -08001398 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001399 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001400 if can_construct_from_value {
1401 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001402 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001403 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001404 instance, inner, inner,
1405 );
David Tolnay63da4d32020-04-25 09:41:12 -07001406 writeln!(
1407 out,
1408 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1409 inner, inner,
1410 );
1411 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001412 }
David Tolnay7db73692019-10-20 14:51:12 -04001413 writeln!(
1414 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001415 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001416 instance, inner, inner,
1417 );
David Tolnay7e219b82020-03-01 13:14:51 -08001418 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001419 writeln!(out, "}}");
1420 writeln!(
1421 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001422 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001423 inner, instance, inner,
1424 );
1425 writeln!(out, " return ptr.get();");
1426 writeln!(out, "}}");
1427 writeln!(
1428 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001429 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001430 inner, instance, inner,
1431 );
1432 writeln!(out, " return ptr.release();");
1433 writeln!(out, "}}");
1434 writeln!(
1435 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001436 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001437 instance, inner,
1438 );
1439 writeln!(out, " ptr->~unique_ptr();");
1440 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001441}
Myron Ahneba35cf2020-02-05 19:41:51 +07001442
David Tolnaya7c2ea12020-10-30 21:32:53 -07001443fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001444 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001445 let inner = to_typename(&element, out.types);
1446 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001447
David Tolnay8f16ae72020-10-08 18:21:13 -07001448 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1449 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001450 writeln!(
1451 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001452 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001453 instance, inner,
1454 );
1455 writeln!(out, " return s.size();");
1456 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001457 writeln!(
1458 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001459 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001460 inner, instance, inner,
1461 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001462 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001463 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001464
David Tolnaya7c2ea12020-10-30 21:32:53 -07001465 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001466
David Tolnay8f16ae72020-10-08 18:21:13 -07001467 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001468}