blob: 095b2c2fd4db0b16b7a88b18f666ec155feddcd9 [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
Adrian Taylor565ddf02020-10-29 21:12:36 -070024 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070025
David Tolnay04b81652020-10-31 22:43:25 -070026 gen_namespace_forward_declarations(out, &apis_by_namespace);
27 gen_namespace_contents(out, &apis_by_namespace, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -070028
29 if !header {
30 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070031 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040032 }
33
David Tolnaye629c642020-10-31 22:02:09 -070034 write_builtins(out);
David Tolnay8810a542020-10-31 21:39:22 -070035 write_includes(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070036
37 out_file
38}
39
David Tolnay04b81652020-10-31 22:43:25 -070040fn gen_namespace_forward_declarations(out: &mut OutFile, ns_entries: &NamespaceEntries) {
Adrian Taylor565ddf02020-10-29 21:12:36 -070041 let apis = ns_entries.entries();
Adrian Taylorc8713432020-10-21 18:20:55 -070042
David Tolnay7db73692019-10-20 14:51:12 -040043 out.next_section();
David Tolnay630af882020-10-31 22:03:47 -070044 for api in apis {
David Tolnay7db73692019-10-20 14:51:12 -040045 match api {
David Tolnay880d1f82020-10-31 22:05:24 -070046 Api::Include(include) => out.include.insert(include),
Adrian Taylorc8713432020-10-21 18:20:55 -070047 Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident),
48 Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx),
49 Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident),
David Tolnay7c295462020-04-25 12:45:07 -070050 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040051 }
52 }
53
Adrian Taylorf9213622020-10-31 22:25:42 -070054 out.next_section();
55
56 for (child_ns, child_ns_entries) in ns_entries.children() {
57 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -070058 gen_namespace_forward_declarations(out, child_ns_entries);
Adrian Taylorf9213622020-10-31 22:25:42 -070059 writeln!(out, "}} // namespace {}", child_ns);
60 }
61}
62
David Tolnay04b81652020-10-31 22:43:25 -070063fn gen_namespace_contents(out: &mut OutFile, ns_entries: &NamespaceEntries, opt: &Opt) {
Adrian Taylorf9213622020-10-31 22:25:42 -070064 let apis = ns_entries.entries();
65
David Tolnayf94bef12020-04-17 14:46:42 -070066 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070067 for api in apis {
David Tolnayf94bef12020-04-17 14:46:42 -070068 if let Api::RustFunction(efn) = api {
69 if let Some(receiver) = &efn.sig.receiver {
70 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070071 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070072 .or_insert_with(Vec::new)
73 .push(efn);
74 }
75 }
76 }
Joel Galenson968738f2020-04-15 14:19:33 -070077
David Tolnay7db73692019-10-20 14:51:12 -040078 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070079 match api {
80 Api::Struct(strct) => {
81 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070082 if !out.types.cxx.contains(&strct.ident.rust) {
David Tolnaya7c2ea12020-10-30 21:32:53 -070083 write_struct(out, strct);
David Tolnaya593d6e2020-08-29 19:48:08 -070084 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070085 }
Joel Galensonc03402a2020-04-23 17:31:09 -070086 Api::Enum(enm) => {
87 out.next_section();
David Tolnay4d148422020-10-31 22:41:37 -070088 if out.types.cxx.contains(&enm.ident.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070089 check_enum(out, enm);
90 } else {
91 write_enum(out, enm);
92 }
Joel Galensonc03402a2020-04-23 17:31:09 -070093 }
David Tolnayc1fe0052020-04-17 15:15:06 -070094 Api::RustType(ety) => {
Adrian Taylorc8713432020-10-21 18:20:55 -070095 if let Some(methods) = methods_for_type.get(&ety.ident.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -070096 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070097 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070098 }
David Tolnayc1fe0052020-04-17 15:15:06 -070099 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700100 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400101 }
102 }
103
David Tolnayfabca772020-10-03 21:25:41 -0700104 out.next_section();
105 for api in apis {
106 if let Api::TypeAlias(ety) = api {
David Tolnay4d148422020-10-31 22:41:37 -0700107 if out.types.required_trivial.contains_key(&ety.ident.rust) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700108 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700109 }
110 }
111 }
112
David Tolnayce5a91f2020-10-31 22:42:08 -0700113 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400114 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700115 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400116 for api in apis {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700117 let (efn, write): (_, fn(_, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400118 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
119 Api::RustFunction(efn) => (efn, write_rust_function_decl),
120 _ => continue,
121 };
122 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700123 write(out, efn, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400124 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800125 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400126 }
127
128 for api in apis {
129 if let Api::RustFunction(efn) = api {
130 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700131 write_rust_function_shim(out, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400132 }
133 }
134
135 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700136
Adrian Taylor565ddf02020-10-29 21:12:36 -0700137 for (child_ns, child_ns_entries) in ns_entries.children() {
Adrian Taylorc8713432020-10-21 18:20:55 -0700138 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -0700139 gen_namespace_contents(out, child_ns_entries, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -0700140 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400141 }
David Tolnay7db73692019-10-20 14:51:12 -0400142}
143
David Tolnay528200f2020-10-31 21:02:22 -0700144fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700145 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400146 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700147 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700148 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
149 | Some(I64) => out.include.cstdint = true,
150 Some(Usize) => out.include.cstddef = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700151 Some(Isize) => {
152 out.include.basetsd = true;
153 out.builtin.rust_isize = true;
154 }
David Tolnay89e386d2020-10-03 19:02:19 -0700155 Some(CxxString) => out.include.string = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700156 Some(RustString) => {
157 out.include.array = true;
158 out.include.cstdint = true;
159 out.include.string = true;
160 out.builtin.rust_string = true;
161 }
162 Some(Bool) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700163 },
David Tolnayb7a7cb62020-03-17 21:18:40 -0700164 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700165 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700167 out.builtin.rust_box = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700168 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700169 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700170 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700171 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700172 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700173 out.builtin.panic = true;
174 out.builtin.rust_vec = true;
175 out.builtin.unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700176 }
David Tolnayb9da1462020-10-31 21:03:41 -0700177 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 Type::Str(_) => {
179 out.include.cstdint = true;
180 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700181 out.builtin.rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700182 }
David Tolnayb9da1462020-10-31 21:03:41 -0700183 Type::CxxVector(_) => out.include.vector = true,
David Tolnay75dca2e2020-03-25 20:17:52 -0700184 Type::Fn(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700185 out.builtin.rust_fn = true;
David Tolnay75dca2e2020-03-25 20:17:52 -0700186 }
David Tolnayb9da1462020-10-31 21:03:41 -0700187 Type::Slice(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700188 out.builtin.rust_slice = true;
David Tolnay4770b472020-04-14 16:32:59 -0700189 }
David Tolnayb9da1462020-10-31 21:03:41 -0700190 Type::SliceRefU8(_) => {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 out.include.cstdint = true;
David Tolnayb9da1462020-10-31 21:03:41 -0700192 out.builtin.rust_slice = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 }
194 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400195 }
196 }
197
David Tolnay09011c32020-03-06 14:40:28 -0800198 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700199 match api {
200 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700201 if efn.throws {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700202 out.builtin.trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700203 } else if let Some(Type::Str(_)) = efn.ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700204 out.builtin.rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700205 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700206 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700207 match arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700208 Type::Str(_) => out.builtin.rust_str_new_unchecked = true,
209 Type::RustVec(_) => out.builtin.unsafe_bitcopy = true,
210 _ => out.builtin.unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700211 }
David Tolnay09011c32020-03-06 14:40:28 -0800212 }
213 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 Api::RustFunction(efn) if !out.header => {
215 if efn.throws {
216 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700217 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700218 out.builtin.rust_str = true;
219 out.builtin.rust_error = true;
220 out.builtin.maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 }
222 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700223 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700224 out.builtin.manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700225 }
226 if let Type::Str(_) = arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700227 out.builtin.rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 }
229 }
230 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700231 if out.types.needs_indirect_abi(ret) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700232 out.builtin.maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700233 } else if let Type::Str(_) = ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700234 out.builtin.rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 }
David Tolnayf51447e2020-03-06 14:14:27 -0800236 }
237 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700238 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800239 }
240 }
David Tolnayec66d112020-10-31 21:00:26 -0700241}
David Tolnayf51447e2020-03-06 14:14:27 -0800242
David Tolnay8810a542020-10-31 21:39:22 -0700243fn write_includes(out: &mut OutFile) {
244 let include = &mut out.include;
245 let out = &mut include.content;
246
David Tolnay12af7e42020-10-31 21:09:23 -0700247 for include in &include.custom {
248 match include.kind {
249 IncludeKind::Quoted => {
250 writeln!(out, "#include \"{}\"", include.path.escape_default());
251 }
252 IncludeKind::Bracketed => {
253 writeln!(out, "#include <{}>", include.path);
254 }
255 }
256 }
257
258 if include.array {
259 writeln!(out, "#include <array>");
260 }
261 if include.cstddef {
262 writeln!(out, "#include <cstddef>");
263 }
264 if include.cstdint {
265 writeln!(out, "#include <cstdint>");
266 }
267 if include.cstring {
268 writeln!(out, "#include <cstring>");
269 }
270 if include.exception {
271 writeln!(out, "#include <exception>");
272 }
273 if include.memory {
274 writeln!(out, "#include <memory>");
275 }
276 if include.new {
277 writeln!(out, "#include <new>");
278 }
279 if include.string {
280 writeln!(out, "#include <string>");
281 }
282 if include.type_traits {
283 writeln!(out, "#include <type_traits>");
284 }
285 if include.utility {
286 writeln!(out, "#include <utility>");
287 }
288 if include.vector {
289 writeln!(out, "#include <vector>");
290 }
291 if include.basetsd {
292 writeln!(out, "#if defined(_WIN32)");
293 writeln!(out, "#include <basetsd.h>");
294 writeln!(out, "#endif");
295 }
296}
297
David Tolnayec66d112020-10-31 21:00:26 -0700298fn write_builtins(out: &mut OutFile) {
David Tolnayfd68e562020-10-31 21:59:56 -0700299 if out.builtin == Default::default() {
300 return;
David Tolnayf51447e2020-03-06 14:14:27 -0800301 }
302
David Tolnayfd68e562020-10-31 21:59:56 -0700303 let include = &mut out.include;
304 let builtin = &mut out.builtin;
305 let out = &mut builtin.content;
David Tolnay16ab1462020-09-02 15:10:09 -0700306
David Tolnayfd68e562020-10-31 21:59:56 -0700307 out.begin_block("namespace rust");
308 out.begin_block("inline namespace cxxbridge05");
309 writeln!(out, "// #include \"rust/cxx.h\"");
310
311 include::write(out, builtin.panic, "CXXBRIDGE05_PANIC");
312
313 if builtin.rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700314 out.next_section();
315 writeln!(out, "struct unsafe_bitcopy_t;");
316 }
317
David Tolnayfd68e562020-10-31 21:59:56 -0700318 if builtin.rust_error {
David Tolnay84ddf9e2020-10-31 15:36:48 -0700319 out.begin_block("namespace");
320 writeln!(out, "template <typename T>");
321 writeln!(out, "class impl;");
322 out.end_block("namespace");
323 }
324
David Tolnayfd68e562020-10-31 21:59:56 -0700325 include::write(out, builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
326 include::write(out, builtin.rust_str, "CXXBRIDGE05_RUST_STR");
327 include::write(out, builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
328 include::write(out, builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
329 include::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
330 include::write(out, builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
331 include::write(out, builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
332 include::write(out, builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
333 include::write(out, builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800334
David Tolnayfd68e562020-10-31 21:59:56 -0700335 if builtin.manually_drop {
David Tolnayf51447e2020-03-06 14:14:27 -0800336 out.next_section();
David Tolnayfd68e562020-10-31 21:59:56 -0700337 include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800338 writeln!(out, "template <typename T>");
339 writeln!(out, "union ManuallyDrop {{");
340 writeln!(out, " T value;");
341 writeln!(
342 out,
343 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
344 );
345 writeln!(out, " ~ManuallyDrop() {{}}");
346 writeln!(out, "}};");
347 }
348
David Tolnayfd68e562020-10-31 21:59:56 -0700349 if builtin.maybe_uninit {
David Tolnay09011c32020-03-06 14:40:28 -0800350 out.next_section();
351 writeln!(out, "template <typename T>");
352 writeln!(out, "union MaybeUninit {{");
353 writeln!(out, " T value;");
354 writeln!(out, " MaybeUninit() {{}}");
355 writeln!(out, " ~MaybeUninit() {{}}");
356 writeln!(out, "}};");
357 }
358
David Tolnayd68dfa82020-10-31 16:01:24 -0700359 out.begin_block("namespace");
360
David Tolnayfd68e562020-10-31 21:59:56 -0700361 if builtin.trycatch
362 || builtin.rust_error
363 || builtin.rust_str_new_unchecked
364 || builtin.rust_str_repr
David Tolnay3be0e1f2020-10-31 20:53:00 -0700365 {
David Tolnayd68dfa82020-10-31 16:01:24 -0700366 out.begin_block("namespace repr");
367 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700368 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700369 writeln!(out, " size_t len;");
370 writeln!(out, "}};");
371 out.end_block("namespace repr");
372 }
373
David Tolnayfd68e562020-10-31 21:59:56 -0700374 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700375 out.next_section();
376 writeln!(out, "template <>");
377 writeln!(out, "class impl<Str> final {{");
378 writeln!(out, "public:");
David Tolnayfd68e562020-10-31 21:59:56 -0700379 if builtin.rust_str_new_unchecked {
David Tolnaya4eb9432020-10-31 20:32:19 -0700380 writeln!(
381 out,
382 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
383 );
384 writeln!(out, " Str str;");
385 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
386 writeln!(out, " str.len = repr.len;");
387 writeln!(out, " return str;");
388 writeln!(out, " }}");
389 }
David Tolnayfd68e562020-10-31 21:59:56 -0700390 if builtin.rust_str_repr {
David Tolnaya4eb9432020-10-31 20:32:19 -0700391 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
392 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
393 writeln!(out, " }}");
394 }
David Tolnay0356d332020-10-31 19:46:41 -0700395 writeln!(out, "}};");
396 }
397
David Tolnayfd68e562020-10-31 21:59:56 -0700398 if builtin.rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700399 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700400 writeln!(out, "template <>");
401 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700402 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700403 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700404 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700405 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700406 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700407 writeln!(out, " return error;");
408 writeln!(out, " }}");
409 writeln!(out, "}};");
410 }
411
David Tolnayd68dfa82020-10-31 16:01:24 -0700412 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700413 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700414
David Tolnayfd68e562020-10-31 21:59:56 -0700415 if builtin.trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700416 out.begin_block("namespace behavior");
David Tolnayfd68e562020-10-31 21:59:56 -0700417 include.exception = true;
418 include.type_traits = true;
419 include.utility = true;
David Tolnay04722332020-03-18 11:31:54 -0700420 writeln!(out, "class missing {{}};");
421 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700422 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700423 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700424 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700425 writeln!(
426 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700427 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700428 );
David Tolnay04722332020-03-18 11:31:54 -0700429 writeln!(out, " missing>::value>::type");
430 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700431 writeln!(out, " func();");
432 writeln!(out, "}} catch (const ::std::exception &e) {{");
433 writeln!(out, " fail(e.what());");
434 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700435 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700436 }
437
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800438 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400439}
440
David Tolnaya7c2ea12020-10-30 21:32:53 -0700441fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700442 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700443 writeln!(out, "#ifndef {}", guard);
444 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400445 for line in strct.doc.to_string().lines() {
446 writeln!(out, "//{}", line);
447 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700448 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400449 for field in &strct.fields {
450 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700451 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400452 writeln!(out, "{};", field.ident);
453 }
454 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700455 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400456}
457
458fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
459 writeln!(out, "struct {};", ident);
460}
461
Adrian Taylorc8713432020-10-21 18:20:55 -0700462fn write_struct_using(out: &mut OutFile, ident: &CppName) {
463 writeln!(
464 out,
465 "using {} = {};",
466 ident.ident,
467 ident.to_fully_qualified()
468 );
David Tolnay8861bee2020-01-20 18:39:24 -0800469}
470
David Tolnaya7c2ea12020-10-30 21:32:53 -0700471fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700472 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700473 writeln!(out, "#ifndef {}", guard);
474 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700475 for line in ety.doc.to_string().lines() {
476 writeln!(out, "//{}", line);
477 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700478 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
479 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
480 writeln!(
481 out,
482 " {}(const {} &) = delete;",
483 ety.ident.cxx.ident, ety.ident.cxx.ident
484 );
Joel Galenson968738f2020-04-15 14:19:33 -0700485 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700486 write!(out, " ");
487 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700488 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700489 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700490 writeln!(out, ";");
491 }
492 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700493 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700494}
495
Joel Galensonc03402a2020-04-23 17:31:09 -0700496fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700497 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700498 writeln!(out, "#ifndef {}", guard);
499 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700500 for line in enm.doc.to_string().lines() {
501 writeln!(out, "//{}", line);
502 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700503 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700504 write_atom(out, enm.repr);
505 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700506 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700507 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700508 }
509 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700510 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700511}
512
Joel Galenson905eb2e2020-05-04 14:58:14 -0700513fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700514 write!(
515 out,
516 "static_assert(sizeof({}) == sizeof(",
517 enm.ident.cxx.ident
518 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700519 write_atom(out, enm.repr);
520 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700521 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700522 write!(out, "static_assert(static_cast<");
523 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700524 writeln!(
525 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700526 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700527 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700528 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700529 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700530}
531
Adrian Taylorc8713432020-10-21 18:20:55 -0700532fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700533 // NOTE: The following two static assertions are just nice-to-have and not
534 // necessary for soundness. That's because triviality is always declared by
535 // the user in the form of an unsafe impl of cxx::ExternType:
536 //
537 // unsafe impl ExternType for MyType {
538 // type Id = cxx::type_id!("...");
539 // type Kind = cxx::kind::Trivial;
540 // }
541 //
542 // Since the user went on the record with their unsafe impl to unsafely
543 // claim they KNOW that the type is trivial, it's fine for that to be on
544 // them if that were wrong.
545 //
546 // There may be a legitimate reason we'll want to remove these assertions
547 // for support of types that the programmer knows are Rust-movable despite
548 // not being recognized as such by the C++ type system due to a move
549 // constructor or destructor.
550
Adrian Taylorc8713432020-10-21 18:20:55 -0700551 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700552 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700553 writeln!(out, "static_assert(");
554 writeln!(
555 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700556 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700557 id,
558 );
559 writeln!(
560 out,
561 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
562 id,
563 );
564 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700565 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700566 writeln!(
567 out,
568 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
569 id,
570 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700571}
572
Adrian Taylorc8713432020-10-21 18:20:55 -0700573fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700574 let mut has_cxx_throws = false;
575 for api in apis {
576 if let Api::CxxFunction(efn) = api {
577 if efn.throws {
578 has_cxx_throws = true;
579 break;
580 }
581 }
582 }
583
584 if has_cxx_throws {
585 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700586 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700587 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700588 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700589 );
590 }
591}
592
David Tolnaya7c2ea12020-10-30 21:32:53 -0700593fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700594 if let Some(annotation) = impl_annotations {
595 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700596 }
David Tolnayebef4a22020-03-17 15:33:47 -0700597 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700598 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700599 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700600 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700601 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700602 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700603 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700604 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700605 if receiver.mutability.is_none() {
606 write!(out, "const ");
607 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700608 write!(
609 out,
610 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700611 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700612 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700613 }
David Tolnay7db73692019-10-20 14:51:12 -0400614 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700615 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400616 write!(out, ", ");
617 }
David Tolnaya46a2372020-03-06 10:03:48 -0800618 if arg.ty == RustString {
619 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700620 } else if let Type::RustVec(_) = arg.ty {
621 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800622 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700623 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400624 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700625 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400626 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700627 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400628 write!(out, ", ");
629 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700630 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400631 write!(out, "*return$");
632 }
633 writeln!(out, ") noexcept {{");
634 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700635 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700636 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700637 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700638 Some(receiver) => write!(
639 out,
640 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700641 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700642 efn.ident.rust
643 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700644 }
David Tolnay7db73692019-10-20 14:51:12 -0400645 for (i, arg) in efn.args.iter().enumerate() {
646 if i > 0 {
647 write!(out, ", ");
648 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700649 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400650 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700651 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700652 if let Some(receiver) = &efn.receiver {
653 if receiver.mutability.is_none() {
654 write!(out, " const");
655 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700656 }
657 write!(out, " = ");
658 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700659 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
660 Some(receiver) => write!(
661 out,
662 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700663 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700664 efn.ident.cxx.ident
665 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700666 }
667 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400668 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700669 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700670 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700671 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700672 writeln!(out, " [&] {{");
673 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700674 }
David Tolnay7db73692019-10-20 14:51:12 -0400675 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700676 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400677 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700678 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400679 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700680 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400681 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700682 }
683 match &efn.ret {
684 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700685 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700686 Some(Type::SliceRefU8(_)) if !indirect_return => {
687 write!(out, "::rust::Slice<uint8_t>::Repr(")
688 }
David Tolnay99642622020-03-25 13:07:35 -0700689 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400690 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700691 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700692 None => write!(out, "{}$(", efn.ident.rust),
693 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700694 }
David Tolnay7db73692019-10-20 14:51:12 -0400695 for (i, arg) in efn.args.iter().enumerate() {
696 if i > 0 {
697 write!(out, ", ");
698 }
699 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700700 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400701 write!(out, "::from_raw({})", arg.ident);
702 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700703 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400704 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700705 } else if let Type::Str(_) = arg.ty {
706 write!(
707 out,
708 "::rust::impl<::rust::Str>::new_unchecked({})",
709 arg.ident,
710 );
David Tolnaya46a2372020-03-06 10:03:48 -0800711 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800712 write!(
713 out,
714 "::rust::String(::rust::unsafe_bitcopy, *{})",
715 arg.ident,
716 );
David Tolnay313b10e2020-04-25 16:30:51 -0700717 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700718 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700719 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700720 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700721 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800722 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400723 } else {
724 write!(out, "{}", arg.ident);
725 }
726 }
727 write!(out, ")");
728 match &efn.ret {
729 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
730 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700731 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400732 _ => {}
733 }
734 if indirect_return {
735 write!(out, ")");
736 }
737 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700738 if efn.throws {
739 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700740 writeln!(out, " throw$.ptr = nullptr;");
741 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700742 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700743 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700744 writeln!(
745 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700746 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700747 );
David Tolnay5d121442020-03-17 22:14:40 -0700748 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700749 writeln!(out, " return throw$;");
750 }
David Tolnay7db73692019-10-20 14:51:12 -0400751 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700752 for arg in &efn.args {
753 if let Type::Fn(f) = &arg.ty {
754 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700755 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700756 }
757 }
758}
759
760fn write_function_pointer_trampoline(
761 out: &mut OutFile,
762 efn: &ExternFn,
763 var: &Ident,
764 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700765) {
766 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700767 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700768 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700769 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700770
771 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700772 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
773 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400774}
775
David Tolnaya7c2ea12020-10-30 21:32:53 -0700776fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
777 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700778 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700779 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700780}
781
782fn write_rust_function_decl_impl(
783 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700784 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700785 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700786 indirect_call: bool,
787) {
788 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700789 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700790 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700791 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700792 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700793 write!(out, "{}(", link_name);
794 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700795 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700796 if receiver.mutability.is_none() {
797 write!(out, "const ");
798 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700799 write!(
800 out,
801 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700802 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700803 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700804 needs_comma = true;
805 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700806 for arg in &sig.args {
807 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400808 write!(out, ", ");
809 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700810 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700811 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400812 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700813 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700814 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400815 write!(out, ", ");
816 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700817 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400818 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700819 needs_comma = true;
820 }
821 if indirect_call {
822 if needs_comma {
823 write!(out, ", ");
824 }
825 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400826 }
827 writeln!(out, ") noexcept;");
828}
829
David Tolnaya7c2ea12020-10-30 21:32:53 -0700830fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400831 for line in efn.doc.to_string().lines() {
832 writeln!(out, "//{}", line);
833 }
David Tolnaya73853b2020-04-20 01:19:56 -0700834 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700835 None => efn.ident.cxx.ident.to_string(),
836 Some(receiver) => format!(
837 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700838 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700839 efn.ident.cxx.ident
840 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700841 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700842 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700843 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700844 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700845}
846
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700847fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700848 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700849 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700850 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700851 indirect_call: bool,
852) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700853 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700854 write!(out, "{}(", local_name);
855 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400856 if i > 0 {
857 write!(out, ", ");
858 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700859 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400860 write!(out, "{}", arg.ident);
861 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700862 if indirect_call {
863 if !sig.args.is_empty() {
864 write!(out, ", ");
865 }
866 write!(out, "void *extern$");
867 }
David Tolnay1e548172020-03-16 13:37:09 -0700868 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700869 if let Some(receiver) = &sig.receiver {
870 if receiver.mutability.is_none() {
871 write!(out, " const");
872 }
873 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700874 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700875 write!(out, " noexcept");
876 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700877}
878
879fn write_rust_function_shim_impl(
880 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700881 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700882 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700883 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700884 indirect_call: bool,
885) {
886 if out.header && sig.receiver.is_some() {
887 // We've already defined this inside the struct.
888 return;
889 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700890 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400891 if out.header {
892 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700893 return;
David Tolnay7db73692019-10-20 14:51:12 -0400894 }
David Tolnay439cde22020-04-20 00:46:25 -0700895 writeln!(out, " {{");
896 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700897 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700898 out.include.utility = true;
899 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700900 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700901 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
902 }
903 }
904 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700905 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700906 if indirect_return {
907 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700908 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700909 writeln!(out, "> return$;");
910 write!(out, " ");
911 } else if let Some(ret) = &sig.ret {
912 write!(out, "return ");
913 match ret {
914 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700915 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700916 write!(out, "::from_raw(");
917 }
918 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700919 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700920 write!(out, "(");
921 }
922 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700923 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700924 _ => {}
925 }
926 }
927 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700928 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700929 }
930 write!(out, "{}(", invoke);
931 if sig.receiver.is_some() {
932 write!(out, "*this");
933 }
934 for (i, arg) in sig.args.iter().enumerate() {
935 if i > 0 || sig.receiver.is_some() {
936 write!(out, ", ");
937 }
938 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700939 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700940 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700941 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700942 _ => {}
943 }
944 write!(out, "{}", arg.ident);
945 match &arg.ty {
946 Type::RustBox(_) => write!(out, ".into_raw()"),
947 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700948 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700949 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700950 _ => {}
951 }
952 }
953 if indirect_return {
954 if !sig.args.is_empty() {
955 write!(out, ", ");
956 }
957 write!(out, "&return$.value");
958 }
959 if indirect_call {
960 if !sig.args.is_empty() || indirect_return {
961 write!(out, ", ");
962 }
963 write!(out, "extern$");
964 }
965 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400966 if !indirect_return {
967 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700968 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400969 write!(out, ")");
970 }
David Tolnay439cde22020-04-20 00:46:25 -0700971 }
972 }
973 writeln!(out, ";");
974 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700975 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700976 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700977 writeln!(out, " }}");
978 }
979 if indirect_return {
980 out.include.utility = true;
981 writeln!(out, " return ::std::move(return$.value);");
982 }
983 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400984}
985
David Tolnaya7c2ea12020-10-30 21:32:53 -0700986fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400987 match ty {
988 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700989 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400990 }
991}
992
David Tolnay75dca2e2020-03-25 20:17:52 -0700993fn indirect_return(sig: &Signature, types: &Types) -> bool {
994 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700995 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700996 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700997}
998
David Tolnaya7c2ea12020-10-30 21:32:53 -0700999fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001000 match ty {
1001 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001002 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001003 write!(out, "*");
1004 }
1005 Type::Ref(ty) => {
1006 if ty.mutability.is_none() {
1007 write!(out, "const ");
1008 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001009 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001010 write!(out, " *");
1011 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001012 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001013 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001014 }
1015}
1016
David Tolnaya7c2ea12020-10-30 21:32:53 -07001017fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1018 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001019 match ty {
1020 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001021 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001022 _ => write_space_after_type(out, ty),
1023 }
1024}
1025
David Tolnaya7c2ea12020-10-30 21:32:53 -07001026fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001027 match ty {
1028 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001029 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001030 write!(out, "*");
1031 }
David Tolnay4a441222020-01-25 16:24:27 -08001032 Some(Type::Ref(ty)) => {
1033 if ty.mutability.is_none() {
1034 write!(out, "const ");
1035 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001036 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001037 write!(out, " *");
1038 }
David Tolnay0356d332020-10-31 19:46:41 -07001039 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001040 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001041 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1042 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001043 }
1044}
1045
David Tolnaya7c2ea12020-10-30 21:32:53 -07001046fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001047 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001048 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001049 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001050 write!(out, "*");
1051 }
David Tolnay0356d332020-10-31 19:46:41 -07001052 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001053 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001054 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001055 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001056 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001057 write!(out, "*");
1058 }
1059 write!(out, "{}", arg.ident);
1060}
1061
David Tolnaya7c2ea12020-10-30 21:32:53 -07001062fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001063 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001064 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001065 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001066 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001067 },
1068 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001069 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001070 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001071 write!(out, ">");
1072 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001073 Type::RustVec(ty) => {
1074 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001075 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001076 write!(out, ">");
1077 }
David Tolnay7db73692019-10-20 14:51:12 -04001078 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001079 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001080 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001081 write!(out, ">");
1082 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001083 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001084 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001085 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001086 write!(out, ">");
1087 }
David Tolnay7db73692019-10-20 14:51:12 -04001088 Type::Ref(r) => {
1089 if r.mutability.is_none() {
1090 write!(out, "const ");
1091 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001092 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001093 write!(out, " &");
1094 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001095 Type::Slice(_) => {
1096 // For now, only U8 slices are supported, which are covered separately below
1097 unreachable!()
1098 }
David Tolnay7db73692019-10-20 14:51:12 -04001099 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001100 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001101 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001102 Type::SliceRefU8(_) => {
1103 write!(out, "::rust::Slice<uint8_t>");
1104 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001105 Type::Fn(f) => {
1106 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1107 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001108 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001109 None => write!(out, "void"),
1110 }
1111 write!(out, "(");
1112 for (i, arg) in f.args.iter().enumerate() {
1113 if i > 0 {
1114 write!(out, ", ");
1115 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001116 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001117 }
1118 write!(out, ")>");
1119 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001120 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001121 }
1122}
1123
David Tolnayf6a89f22020-05-10 23:39:27 -07001124fn write_atom(out: &mut OutFile, atom: Atom) {
1125 match atom {
1126 Bool => write!(out, "bool"),
1127 U8 => write!(out, "uint8_t"),
1128 U16 => write!(out, "uint16_t"),
1129 U32 => write!(out, "uint32_t"),
1130 U64 => write!(out, "uint64_t"),
1131 Usize => write!(out, "size_t"),
1132 I8 => write!(out, "int8_t"),
1133 I16 => write!(out, "int16_t"),
1134 I32 => write!(out, "int32_t"),
1135 I64 => write!(out, "int64_t"),
1136 Isize => write!(out, "::rust::isize"),
1137 F32 => write!(out, "float"),
1138 F64 => write!(out, "double"),
1139 CxxString => write!(out, "::std::string"),
1140 RustString => write!(out, "::rust::String"),
1141 }
1142}
1143
David Tolnaya7c2ea12020-10-30 21:32:53 -07001144fn write_type_space(out: &mut OutFile, ty: &Type) {
1145 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001146 write_space_after_type(out, ty);
1147}
1148
1149fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001150 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001151 Type::Ident(_)
1152 | Type::RustBox(_)
1153 | Type::UniquePtr(_)
1154 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001155 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001156 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001157 | Type::SliceRefU8(_)
1158 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001159 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001160 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001161 }
1162}
1163
David Tolnaycd08c442020-04-25 10:16:33 -07001164// Only called for legal referent types of unique_ptr and element types of
1165// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001166fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001167 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001168 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1169 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001170 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001171 }
1172}
1173
David Tolnayacdf20a2020-04-25 12:40:53 -07001174// Only called for legal referent types of unique_ptr and element types of
1175// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001176fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001177 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001178 Type::Ident(ident) => ident.to_symbol(types),
1179 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001180 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001181 }
1182}
1183
David Tolnaya7c2ea12020-10-30 21:32:53 -07001184fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001185 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001186 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001187 if let Type::RustBox(ty) = ty {
1188 if let Type::Ident(inner) = &ty.inner {
1189 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001190 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001191 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001192 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001193 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001194 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001195 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001196 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001197 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001198 }
David Tolnay7db73692019-10-20 14:51:12 -04001199 } else if let Type::UniquePtr(ptr) = ty {
1200 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001201 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001202 && (!out.types.aliases.contains_key(&inner.rust)
1203 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001204 {
David Tolnay7db73692019-10-20 14:51:12 -04001205 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001206 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001207 }
1208 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001209 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001210 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001211 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001212 && (!out.types.aliases.contains_key(&inner.rust)
1213 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001214 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001215 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001216 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001217 }
1218 }
1219 }
1220 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001221 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001222
David Tolnay750755e2020-03-01 13:04:08 -08001223 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001224 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001225 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001226 if let Type::RustBox(ty) = ty {
1227 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001228 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001229 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001230 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001231 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001232 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001233 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001234 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001235 }
David Tolnay7db73692019-10-20 14:51:12 -04001236 }
1237 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001238 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001239 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001240}
1241
Adrian Taylorc8713432020-10-21 18:20:55 -07001242fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1243 let inner = ident.to_fully_qualified();
1244 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001245
David Tolnay8f16ae72020-10-08 18:21:13 -07001246 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1247 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001248 writeln!(
1249 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001250 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001251 instance, inner,
1252 );
1253 writeln!(
1254 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001255 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001256 instance, inner,
1257 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001258 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001259}
1260
David Tolnaya7c2ea12020-10-30 21:32:53 -07001261fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001262 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001263 let inner = to_typename(&element, out.types);
1264 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001265
David Tolnay8f16ae72020-10-08 18:21:13 -07001266 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1267 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001268 writeln!(
1269 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001270 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001271 instance, inner,
1272 );
1273 writeln!(
1274 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001275 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001276 instance, inner,
1277 );
1278 writeln!(
1279 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001280 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001281 instance, inner,
1282 );
David Tolnay219c0792020-04-24 20:31:37 -07001283 writeln!(
1284 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001285 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001286 inner, instance,
1287 );
David Tolnay503d0192020-04-24 22:18:56 -07001288 writeln!(
1289 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001290 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001291 instance,
1292 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001293 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001294}
1295
Adrian Taylorc8713432020-10-21 18:20:55 -07001296fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1297 let inner = ident.to_fully_qualified();
1298 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001299
1300 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001301 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001302 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001303 writeln!(out, "}}");
1304
1305 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001306 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001307 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001308 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001309}
1310
David Tolnaya7c2ea12020-10-30 21:32:53 -07001311fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001312 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001313 let inner = to_typename(&element, out.types);
1314 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001315
Myron Ahneba35cf2020-02-05 19:41:51 +07001316 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001317 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001318 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001319 writeln!(out, "}}");
1320
1321 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001322 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1323 writeln!(
1324 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001325 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001326 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001327 );
1328 writeln!(out, "}}");
1329
1330 writeln!(out, "template <>");
1331 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001332 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001333 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001334
1335 writeln!(out, "template <>");
1336 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1337 writeln!(
1338 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001339 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001340 instance,
1341 );
1342 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001343
1344 writeln!(out, "template <>");
1345 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001346 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001347 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001348}
1349
David Tolnaya7c2ea12020-10-30 21:32:53 -07001350fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001351 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001352 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001353
David Tolnay8f16ae72020-10-08 18:21:13 -07001354 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1355 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001356
David Tolnaya7c2ea12020-10-30 21:32:53 -07001357 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001358
David Tolnay8f16ae72020-10-08 18:21:13 -07001359 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001360}
1361
1362// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001363fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001364 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001365 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001366 let inner = to_typename(ty, out.types);
1367 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001368
David Tolnay63da4d32020-04-25 09:41:12 -07001369 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001370 // Some aliases are to opaque types; some are to trivial types. We can't
1371 // know at code generation time, so we generate both C++ and Rust side
1372 // bindings for a "new" method anyway. But the Rust code can't be called
1373 // for Opaque types because the 'new' method is not implemented.
1374 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001375 out.types.structs.contains_key(&ident.rust)
1376 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001377 }
David Tolnay63da4d32020-04-25 09:41:12 -07001378 _ => false,
1379 };
1380
David Tolnay7db73692019-10-20 14:51:12 -04001381 writeln!(
1382 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001383 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001384 inner,
1385 );
1386 writeln!(
1387 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001388 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001389 inner,
1390 );
1391 writeln!(
1392 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001393 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001394 instance, inner,
1395 );
David Tolnay7e219b82020-03-01 13:14:51 -08001396 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001397 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001398 if can_construct_from_value {
1399 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001400 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001401 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001402 instance, inner, inner,
1403 );
David Tolnay63da4d32020-04-25 09:41:12 -07001404 writeln!(
1405 out,
1406 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1407 inner, inner,
1408 );
1409 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001410 }
David Tolnay7db73692019-10-20 14:51:12 -04001411 writeln!(
1412 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001413 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001414 instance, inner, inner,
1415 );
David Tolnay7e219b82020-03-01 13:14:51 -08001416 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001417 writeln!(out, "}}");
1418 writeln!(
1419 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001420 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001421 inner, instance, inner,
1422 );
1423 writeln!(out, " return ptr.get();");
1424 writeln!(out, "}}");
1425 writeln!(
1426 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001427 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001428 inner, instance, inner,
1429 );
1430 writeln!(out, " return ptr.release();");
1431 writeln!(out, "}}");
1432 writeln!(
1433 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001434 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001435 instance, inner,
1436 );
1437 writeln!(out, " ptr->~unique_ptr();");
1438 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001439}
Myron Ahneba35cf2020-02-05 19:41:51 +07001440
David Tolnaya7c2ea12020-10-30 21:32:53 -07001441fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001442 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001443 let inner = to_typename(&element, out.types);
1444 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001445
David Tolnay8f16ae72020-10-08 18:21:13 -07001446 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1447 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001448 writeln!(
1449 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001450 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001451 instance, inner,
1452 );
1453 writeln!(out, " return s.size();");
1454 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001455 writeln!(
1456 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001457 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001458 inner, instance, inner,
1459 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001460 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001461 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001462
David Tolnaya7c2ea12020-10-30 21:32:53 -07001463 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001464
David Tolnay8f16ae72020-10-08 18:21:13 -07001465 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001466}