blob: cadeb4e78ff7bb81b85c306bead4f734ac6304f8 [file] [log] [blame]
Adrian Taylor565ddf02020-10-29 21:12:36 -07001use crate::gen::namespace_organizer::NamespaceEntries;
David Tolnay7db73692019-10-20 14:51:12 -04002use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07003use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04004use crate::syntax::atom::Atom::{self, *};
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Adrian Taylorc8713432020-10-21 18:20:55 -07006use crate::syntax::{
7 mangle, Api, CppName, Enum, ExternFn, ExternType, ResolvableName, Signature, Struct, Type,
8 Types, Var,
9};
David Tolnay7db73692019-10-20 14:51:12 -040010use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -070011use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040012
David Tolnayb560a0f2020-10-30 21:28:45 -070013pub(super) fn gen<'a>(apis: &[Api], types: &'a Types, opt: &Opt, header: bool) -> OutFile<'a> {
14 let mut out_file = OutFile::new(header, types);
David Tolnay7db73692019-10-20 14:51:12 -040015 let out = &mut out_file;
16
David Tolnay54702b92020-07-31 11:50:09 -070017 if header {
18 writeln!(out.front, "#pragma once");
19 }
20
David Tolnay4aae7c02020-10-28 12:35:42 -070021 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040022 for api in apis {
23 if let Api::Include(include) = api {
David Tolnay353d98c2020-10-28 15:43:35 -070024 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040025 }
26 }
27
David Tolnaya7c2ea12020-10-30 21:32:53 -070028 write_includes(out);
29 write_include_cxxbridge(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040030
David Tolnay7db73692019-10-20 14:51:12 -040031 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070032
Adrian Taylor565ddf02020-10-29 21:12:36 -070033 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070034
35 gen_namespace_contents(&apis_by_namespace, types, opt, header, out);
36
37 if !header {
38 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070039 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040040 }
41
Adrian Taylorc8713432020-10-21 18:20:55 -070042 write!(out.front, "{}", out.include);
43
44 out_file
45}
46
47fn gen_namespace_contents(
48 ns_entries: &NamespaceEntries,
49 types: &Types,
50 opt: &Opt,
51 header: bool,
52 out: &mut OutFile,
53) {
Adrian Taylor565ddf02020-10-29 21:12:36 -070054 let apis = ns_entries.entries();
Adrian Taylorc8713432020-10-21 18:20:55 -070055
David Tolnay7db73692019-10-20 14:51:12 -040056 out.next_section();
Adrian Taylor451ec9f2020-10-29 22:51:30 -070057 for api in apis.iter() {
David Tolnay7db73692019-10-20 14:51:12 -040058 match api {
Adrian Taylorc8713432020-10-21 18:20:55 -070059 Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident),
60 Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx),
61 Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident),
David Tolnay7c295462020-04-25 12:45:07 -070062 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040063 }
64 }
65
David Tolnayf94bef12020-04-17 14:46:42 -070066 let mut methods_for_type = HashMap::new();
Adrian Taylor451ec9f2020-10-29 22:51:30 -070067 for api in apis.iter() {
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();
Adrian Taylorc8713432020-10-21 18:20:55 -070082 if !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();
Adrian Taylorc8713432020-10-21 18:20:55 -070088 if 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 {
Adrian Taylorc8713432020-10-21 18:20:55 -0700107 if types.required_trivial.contains_key(&ety.ident.rust) {
108 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700109 }
110 }
111 }
112
David Tolnay7db73692019-10-20 14:51:12 -0400113 if !header {
114 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);
139 gen_namespace_contents(&child_ns_entries, types, opt, header, out);
140 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400141 }
David Tolnay7db73692019-10-20 14:51:12 -0400142}
143
David Tolnaya7c2ea12020-10-30 21:32:53 -0700144fn write_includes(out: &mut OutFile) {
145 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,
151 Some(CxxString) => out.include.string = true,
David Tolnayf57f7562020-10-04 19:56:26 -0700152 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700153 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800154 Type::RustBox(_) => out.include.type_traits = true,
155 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700156 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700157 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700158 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400159 }
160 }
David Tolnay7db73692019-10-20 14:51:12 -0400161}
162
David Tolnaya7c2ea12020-10-30 21:32:53 -0700163fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api]) {
David Tolnay16ab1462020-09-02 15:10:09 -0700164 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700165 let mut needs_rust_string = false;
166 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700167 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400168 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700169 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700170 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700171 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700172 let mut needs_unsafe_bitcopy = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700173 for ty in out.types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700174 match ty {
175 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700176 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700177 out.include.type_traits = true;
178 needs_rust_box = true;
179 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700180 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700181 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700182 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700183 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700184 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700185 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700186 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700187 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188 Type::Str(_) => {
189 out.include.cstdint = true;
190 out.include.string = true;
191 needs_rust_str = true;
192 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700193 Type::Fn(_) => {
194 needs_rust_fn = true;
195 }
David Tolnay4770b472020-04-14 16:32:59 -0700196 Type::Slice(_) | Type::SliceRefU8(_) => {
197 needs_rust_slice = true;
198 }
David Tolnay7c295462020-04-25 12:45:07 -0700199 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400200 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700201 needs_rust_isize = true;
202 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 ty if ty == RustString => {
204 out.include.array = true;
205 out.include.cstdint = true;
206 out.include.string = true;
207 needs_rust_string = true;
208 }
209 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400210 }
211 }
212
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800214 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800215 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700216 let mut needs_trycatch = false;
David Tolnaya4eb9432020-10-31 20:32:19 -0700217 let mut needs_rust_str_new_unchecked = false;
218 let mut needs_rust_str_repr = false;
David Tolnay09011c32020-03-06 14:40:28 -0800219 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700220 match api {
221 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700222 if efn.throws {
223 needs_trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700224 } else if let Some(Type::Str(_)) = efn.ret {
225 needs_rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700226 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700227 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700228 match arg.ty {
229 Type::Str(_) => needs_rust_str_new_unchecked = true,
230 Type::RustVec(_) => needs_unsafe_bitcopy = true,
231 _ => needs_unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700232 }
David Tolnay09011c32020-03-06 14:40:28 -0800233 }
234 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 Api::RustFunction(efn) if !out.header => {
236 if efn.throws {
237 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700238 out.include.string = true;
239 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700240 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200241 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 }
243 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700244 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 needs_manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700246 }
247 if let Type::Str(_) = arg.ty {
248 needs_rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 }
250 }
251 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700252 if out.types.needs_indirect_abi(ret) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700253 needs_maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700254 } else if let Type::Str(_) = ret {
255 needs_rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700256 }
David Tolnayf51447e2020-03-06 14:14:27 -0800257 }
258 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700259 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800260 }
261 }
262
David Tolnay750755e2020-03-01 13:04:08 -0800263 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700264 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800265
David Tolnay16ab1462020-09-02 15:10:09 -0700266 if needs_panic
267 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700268 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700269 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700270 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700271 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700272 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700273 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700274 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700275 || needs_unsafe_bitcopy
276 || needs_manually_drop
277 || needs_maybe_uninit
278 {
David Tolnay736cbca2020-03-11 16:49:18 -0700279 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800280 }
281
David Tolnay8f16ae72020-10-08 18:21:13 -0700282 include::write(out, needs_panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700283
David Tolnay99a95e62020-09-02 15:05:53 -0700284 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700285 out.next_section();
286 writeln!(out, "struct unsafe_bitcopy_t;");
287 }
288
David Tolnay84ddf9e2020-10-31 15:36:48 -0700289 if needs_rust_error {
290 out.begin_block("namespace");
291 writeln!(out, "template <typename T>");
292 writeln!(out, "class impl;");
293 out.end_block("namespace");
294 }
295
David Tolnay8f16ae72020-10-08 18:21:13 -0700296 include::write(out, needs_rust_string, "CXXBRIDGE05_RUST_STRING");
297 include::write(out, needs_rust_str, "CXXBRIDGE05_RUST_STR");
298 include::write(out, needs_rust_slice, "CXXBRIDGE05_RUST_SLICE");
299 include::write(out, needs_rust_box, "CXXBRIDGE05_RUST_BOX");
300 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
301 include::write(out, needs_rust_vec, "CXXBRIDGE05_RUST_VEC");
302 include::write(out, needs_rust_fn, "CXXBRIDGE05_RUST_FN");
303 include::write(out, needs_rust_error, "CXXBRIDGE05_RUST_ERROR");
304 include::write(out, needs_rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800305
306 if needs_manually_drop {
307 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700308 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800309 writeln!(out, "template <typename T>");
310 writeln!(out, "union ManuallyDrop {{");
311 writeln!(out, " T value;");
312 writeln!(
313 out,
314 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
315 );
316 writeln!(out, " ~ManuallyDrop() {{}}");
317 writeln!(out, "}};");
318 }
319
David Tolnay09011c32020-03-06 14:40:28 -0800320 if needs_maybe_uninit {
321 out.next_section();
322 writeln!(out, "template <typename T>");
323 writeln!(out, "union MaybeUninit {{");
324 writeln!(out, " T value;");
325 writeln!(out, " MaybeUninit() {{}}");
326 writeln!(out, " ~MaybeUninit() {{}}");
327 writeln!(out, "}};");
328 }
329
David Tolnayd68dfa82020-10-31 16:01:24 -0700330 out.begin_block("namespace");
331
David Tolnaya4eb9432020-10-31 20:32:19 -0700332 if needs_trycatch || needs_rust_error || needs_rust_str_new_unchecked || needs_rust_str_repr {
David Tolnayd68dfa82020-10-31 16:01:24 -0700333 out.begin_block("namespace repr");
334 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700335 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700336 writeln!(out, " size_t len;");
337 writeln!(out, "}};");
338 out.end_block("namespace repr");
339 }
340
David Tolnaya4eb9432020-10-31 20:32:19 -0700341 if needs_rust_str_new_unchecked || needs_rust_str_repr {
David Tolnay0356d332020-10-31 19:46:41 -0700342 out.next_section();
343 writeln!(out, "template <>");
344 writeln!(out, "class impl<Str> final {{");
345 writeln!(out, "public:");
David Tolnaya4eb9432020-10-31 20:32:19 -0700346 if needs_rust_str_new_unchecked {
347 writeln!(
348 out,
349 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
350 );
351 writeln!(out, " Str str;");
352 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
353 writeln!(out, " str.len = repr.len;");
354 writeln!(out, " return str;");
355 writeln!(out, " }}");
356 }
357 if needs_rust_str_repr {
358 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
359 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
360 writeln!(out, " }}");
361 }
David Tolnay0356d332020-10-31 19:46:41 -0700362 writeln!(out, "}};");
363 }
364
David Tolnaya0c9bc72020-10-31 14:37:14 -0700365 if needs_rust_error {
David Tolnay0356d332020-10-31 19:46:41 -0700366 out.next_section();
David Tolnay84ddf9e2020-10-31 15:36:48 -0700367 writeln!(out, "template <>");
368 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700369 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700370 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700371 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700372 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700373 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700374 writeln!(out, " return error;");
375 writeln!(out, " }}");
376 writeln!(out, "}};");
377 }
378
David Tolnayd68dfa82020-10-31 16:01:24 -0700379 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700380 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700381
David Tolnay5d121442020-03-17 22:14:40 -0700382 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700383 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700384 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700385 out.include.type_traits = true;
386 out.include.utility = true;
387 writeln!(out, "class missing {{}};");
388 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700389 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700390 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700391 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700392 writeln!(
393 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700394 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700395 );
David Tolnay04722332020-03-18 11:31:54 -0700396 writeln!(out, " missing>::value>::type");
397 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700398 writeln!(out, " func();");
399 writeln!(out, "}} catch (const ::std::exception &e) {{");
400 writeln!(out, " fail(e.what());");
401 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700402 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700403 }
404
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800405 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400406}
407
David Tolnaya7c2ea12020-10-30 21:32:53 -0700408fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700409 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700410 writeln!(out, "#ifndef {}", guard);
411 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400412 for line in strct.doc.to_string().lines() {
413 writeln!(out, "//{}", line);
414 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700415 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400416 for field in &strct.fields {
417 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700418 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400419 writeln!(out, "{};", field.ident);
420 }
421 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700422 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400423}
424
425fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
426 writeln!(out, "struct {};", ident);
427}
428
Adrian Taylorc8713432020-10-21 18:20:55 -0700429fn write_struct_using(out: &mut OutFile, ident: &CppName) {
430 writeln!(
431 out,
432 "using {} = {};",
433 ident.ident,
434 ident.to_fully_qualified()
435 );
David Tolnay8861bee2020-01-20 18:39:24 -0800436}
437
David Tolnaya7c2ea12020-10-30 21:32:53 -0700438fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700439 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700440 writeln!(out, "#ifndef {}", guard);
441 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700442 for line in ety.doc.to_string().lines() {
443 writeln!(out, "//{}", line);
444 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700445 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
446 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
447 writeln!(
448 out,
449 " {}(const {} &) = delete;",
450 ety.ident.cxx.ident, ety.ident.cxx.ident
451 );
Joel Galenson968738f2020-04-15 14:19:33 -0700452 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700453 write!(out, " ");
454 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700455 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700456 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700457 writeln!(out, ";");
458 }
459 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700460 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700461}
462
Joel Galensonc03402a2020-04-23 17:31:09 -0700463fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700464 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700465 writeln!(out, "#ifndef {}", guard);
466 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700467 for line in enm.doc.to_string().lines() {
468 writeln!(out, "//{}", line);
469 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700470 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700471 write_atom(out, enm.repr);
472 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700473 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700474 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700475 }
476 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700477 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700478}
479
Joel Galenson905eb2e2020-05-04 14:58:14 -0700480fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700481 write!(
482 out,
483 "static_assert(sizeof({}) == sizeof(",
484 enm.ident.cxx.ident
485 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700486 write_atom(out, enm.repr);
487 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700488 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700489 write!(out, "static_assert(static_cast<");
490 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700491 writeln!(
492 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700493 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700494 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700495 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700496 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700497}
498
Adrian Taylorc8713432020-10-21 18:20:55 -0700499fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700500 // NOTE: The following two static assertions are just nice-to-have and not
501 // necessary for soundness. That's because triviality is always declared by
502 // the user in the form of an unsafe impl of cxx::ExternType:
503 //
504 // unsafe impl ExternType for MyType {
505 // type Id = cxx::type_id!("...");
506 // type Kind = cxx::kind::Trivial;
507 // }
508 //
509 // Since the user went on the record with their unsafe impl to unsafely
510 // claim they KNOW that the type is trivial, it's fine for that to be on
511 // them if that were wrong.
512 //
513 // There may be a legitimate reason we'll want to remove these assertions
514 // for support of types that the programmer knows are Rust-movable despite
515 // not being recognized as such by the C++ type system due to a move
516 // constructor or destructor.
517
Adrian Taylorc8713432020-10-21 18:20:55 -0700518 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700519 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700520 writeln!(out, "static_assert(");
521 writeln!(
522 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700523 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700524 id,
525 );
526 writeln!(
527 out,
528 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
529 id,
530 );
531 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700532 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700533 writeln!(
534 out,
535 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
536 id,
537 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700538}
539
Adrian Taylorc8713432020-10-21 18:20:55 -0700540fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700541 let mut has_cxx_throws = false;
542 for api in apis {
543 if let Api::CxxFunction(efn) = api {
544 if efn.throws {
545 has_cxx_throws = true;
546 break;
547 }
548 }
549 }
550
551 if has_cxx_throws {
552 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700553 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700554 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700555 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700556 );
557 }
558}
559
David Tolnaya7c2ea12020-10-30 21:32:53 -0700560fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700561 if let Some(annotation) = impl_annotations {
562 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700563 }
David Tolnayebef4a22020-03-17 15:33:47 -0700564 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700565 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700566 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700567 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700568 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700569 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700570 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700571 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700572 if receiver.mutability.is_none() {
573 write!(out, "const ");
574 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700575 write!(
576 out,
577 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700578 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700579 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700580 }
David Tolnay7db73692019-10-20 14:51:12 -0400581 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700582 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400583 write!(out, ", ");
584 }
David Tolnaya46a2372020-03-06 10:03:48 -0800585 if arg.ty == RustString {
586 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700587 } else if let Type::RustVec(_) = arg.ty {
588 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800589 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700590 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400591 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700592 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400593 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700594 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400595 write!(out, ", ");
596 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700597 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400598 write!(out, "*return$");
599 }
600 writeln!(out, ") noexcept {{");
601 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700602 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700603 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700604 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700605 Some(receiver) => write!(
606 out,
607 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700608 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700609 efn.ident.rust
610 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700611 }
David Tolnay7db73692019-10-20 14:51:12 -0400612 for (i, arg) in efn.args.iter().enumerate() {
613 if i > 0 {
614 write!(out, ", ");
615 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700616 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400617 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700618 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700619 if let Some(receiver) = &efn.receiver {
620 if receiver.mutability.is_none() {
621 write!(out, " const");
622 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700623 }
624 write!(out, " = ");
625 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700626 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
627 Some(receiver) => write!(
628 out,
629 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700630 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700631 efn.ident.cxx.ident
632 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700633 }
634 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400635 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700636 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700637 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700638 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700639 writeln!(out, " [&] {{");
640 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700641 }
David Tolnay7db73692019-10-20 14:51:12 -0400642 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700643 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400644 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700645 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400646 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700647 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400648 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700649 }
650 match &efn.ret {
651 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700652 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700653 Some(Type::SliceRefU8(_)) if !indirect_return => {
654 write!(out, "::rust::Slice<uint8_t>::Repr(")
655 }
David Tolnay99642622020-03-25 13:07:35 -0700656 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400657 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700658 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700659 None => write!(out, "{}$(", efn.ident.rust),
660 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700661 }
David Tolnay7db73692019-10-20 14:51:12 -0400662 for (i, arg) in efn.args.iter().enumerate() {
663 if i > 0 {
664 write!(out, ", ");
665 }
666 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700667 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400668 write!(out, "::from_raw({})", arg.ident);
669 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700670 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400671 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700672 } else if let Type::Str(_) = arg.ty {
673 write!(
674 out,
675 "::rust::impl<::rust::Str>::new_unchecked({})",
676 arg.ident,
677 );
David Tolnaya46a2372020-03-06 10:03:48 -0800678 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800679 write!(
680 out,
681 "::rust::String(::rust::unsafe_bitcopy, *{})",
682 arg.ident,
683 );
David Tolnay313b10e2020-04-25 16:30:51 -0700684 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700685 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700686 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700687 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700688 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800689 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400690 } else {
691 write!(out, "{}", arg.ident);
692 }
693 }
694 write!(out, ")");
695 match &efn.ret {
696 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
697 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700698 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400699 _ => {}
700 }
701 if indirect_return {
702 write!(out, ")");
703 }
704 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700705 if efn.throws {
706 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700707 writeln!(out, " throw$.ptr = nullptr;");
708 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700709 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700710 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700711 writeln!(
712 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700713 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700714 );
David Tolnay5d121442020-03-17 22:14:40 -0700715 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700716 writeln!(out, " return throw$;");
717 }
David Tolnay7db73692019-10-20 14:51:12 -0400718 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700719 for arg in &efn.args {
720 if let Type::Fn(f) = &arg.ty {
721 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700722 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700723 }
724 }
725}
726
727fn write_function_pointer_trampoline(
728 out: &mut OutFile,
729 efn: &ExternFn,
730 var: &Ident,
731 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700732) {
733 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700734 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700735 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700736 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700737
738 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700739 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
740 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400741}
742
David Tolnaya7c2ea12020-10-30 21:32:53 -0700743fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
744 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700745 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700746 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700747}
748
749fn write_rust_function_decl_impl(
750 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700751 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700752 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700753 indirect_call: bool,
754) {
755 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700756 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700757 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700758 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700759 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700760 write!(out, "{}(", link_name);
761 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700762 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700763 if receiver.mutability.is_none() {
764 write!(out, "const ");
765 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700766 write!(
767 out,
768 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700769 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700770 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700771 needs_comma = true;
772 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700773 for arg in &sig.args {
774 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400775 write!(out, ", ");
776 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700777 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700778 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400779 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700780 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700781 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400782 write!(out, ", ");
783 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700784 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400785 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700786 needs_comma = true;
787 }
788 if indirect_call {
789 if needs_comma {
790 write!(out, ", ");
791 }
792 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400793 }
794 writeln!(out, ") noexcept;");
795}
796
David Tolnaya7c2ea12020-10-30 21:32:53 -0700797fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400798 for line in efn.doc.to_string().lines() {
799 writeln!(out, "//{}", line);
800 }
David Tolnaya73853b2020-04-20 01:19:56 -0700801 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700802 None => efn.ident.cxx.ident.to_string(),
803 Some(receiver) => format!(
804 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700805 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700806 efn.ident.cxx.ident
807 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700808 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700809 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700810 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700811 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700812}
813
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700814fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700815 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700816 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700817 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700818 indirect_call: bool,
819) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700820 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700821 write!(out, "{}(", local_name);
822 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400823 if i > 0 {
824 write!(out, ", ");
825 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700826 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400827 write!(out, "{}", arg.ident);
828 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700829 if indirect_call {
830 if !sig.args.is_empty() {
831 write!(out, ", ");
832 }
833 write!(out, "void *extern$");
834 }
David Tolnay1e548172020-03-16 13:37:09 -0700835 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700836 if let Some(receiver) = &sig.receiver {
837 if receiver.mutability.is_none() {
838 write!(out, " const");
839 }
840 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700841 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700842 write!(out, " noexcept");
843 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700844}
845
846fn write_rust_function_shim_impl(
847 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700848 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700849 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700850 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700851 indirect_call: bool,
852) {
853 if out.header && sig.receiver.is_some() {
854 // We've already defined this inside the struct.
855 return;
856 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700857 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400858 if out.header {
859 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700860 return;
David Tolnay7db73692019-10-20 14:51:12 -0400861 }
David Tolnay439cde22020-04-20 00:46:25 -0700862 writeln!(out, " {{");
863 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700864 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700865 out.include.utility = true;
866 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700867 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700868 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
869 }
870 }
871 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700872 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700873 if indirect_return {
874 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700875 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700876 writeln!(out, "> return$;");
877 write!(out, " ");
878 } else if let Some(ret) = &sig.ret {
879 write!(out, "return ");
880 match ret {
881 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700882 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700883 write!(out, "::from_raw(");
884 }
885 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700886 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700887 write!(out, "(");
888 }
889 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700890 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700891 _ => {}
892 }
893 }
894 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700895 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700896 }
897 write!(out, "{}(", invoke);
898 if sig.receiver.is_some() {
899 write!(out, "*this");
900 }
901 for (i, arg) in sig.args.iter().enumerate() {
902 if i > 0 || sig.receiver.is_some() {
903 write!(out, ", ");
904 }
905 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700906 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700907 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700908 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700909 _ => {}
910 }
911 write!(out, "{}", arg.ident);
912 match &arg.ty {
913 Type::RustBox(_) => write!(out, ".into_raw()"),
914 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700915 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700916 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700917 _ => {}
918 }
919 }
920 if indirect_return {
921 if !sig.args.is_empty() {
922 write!(out, ", ");
923 }
924 write!(out, "&return$.value");
925 }
926 if indirect_call {
927 if !sig.args.is_empty() || indirect_return {
928 write!(out, ", ");
929 }
930 write!(out, "extern$");
931 }
932 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400933 if !indirect_return {
934 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700935 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400936 write!(out, ")");
937 }
David Tolnay439cde22020-04-20 00:46:25 -0700938 }
939 }
940 writeln!(out, ";");
941 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700942 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700943 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700944 writeln!(out, " }}");
945 }
946 if indirect_return {
947 out.include.utility = true;
948 writeln!(out, " return ::std::move(return$.value);");
949 }
950 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400951}
952
David Tolnaya7c2ea12020-10-30 21:32:53 -0700953fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400954 match ty {
955 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700956 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400957 }
958}
959
David Tolnay75dca2e2020-03-25 20:17:52 -0700960fn indirect_return(sig: &Signature, types: &Types) -> bool {
961 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700962 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700963 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700964}
965
David Tolnaya7c2ea12020-10-30 21:32:53 -0700966fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700967 match ty {
968 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700969 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700970 write!(out, "*");
971 }
972 Type::Ref(ty) => {
973 if ty.mutability.is_none() {
974 write!(out, "const ");
975 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700976 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700977 write!(out, " *");
978 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700979 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700980 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700981 }
982}
983
David Tolnaya7c2ea12020-10-30 21:32:53 -0700984fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
985 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700986 match ty {
987 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700988 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700989 _ => write_space_after_type(out, ty),
990 }
991}
992
David Tolnaya7c2ea12020-10-30 21:32:53 -0700993fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400994 match ty {
995 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700996 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400997 write!(out, "*");
998 }
David Tolnay4a441222020-01-25 16:24:27 -0800999 Some(Type::Ref(ty)) => {
1000 if ty.mutability.is_none() {
1001 write!(out, "const ");
1002 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001003 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001004 write!(out, " *");
1005 }
David Tolnay0356d332020-10-31 19:46:41 -07001006 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001007 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001008 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1009 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001010 }
1011}
1012
David Tolnaya7c2ea12020-10-30 21:32:53 -07001013fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001014 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001015 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001016 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001017 write!(out, "*");
1018 }
David Tolnay0356d332020-10-31 19:46:41 -07001019 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001020 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001021 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001022 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001023 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001024 write!(out, "*");
1025 }
1026 write!(out, "{}", arg.ident);
1027}
1028
David Tolnaya7c2ea12020-10-30 21:32:53 -07001029fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001030 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001031 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001032 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001033 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001034 },
1035 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001036 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001037 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001038 write!(out, ">");
1039 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001040 Type::RustVec(ty) => {
1041 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001042 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001043 write!(out, ">");
1044 }
David Tolnay7db73692019-10-20 14:51:12 -04001045 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001046 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001047 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001048 write!(out, ">");
1049 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001050 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001051 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001052 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001053 write!(out, ">");
1054 }
David Tolnay7db73692019-10-20 14:51:12 -04001055 Type::Ref(r) => {
1056 if r.mutability.is_none() {
1057 write!(out, "const ");
1058 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001059 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001060 write!(out, " &");
1061 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001062 Type::Slice(_) => {
1063 // For now, only U8 slices are supported, which are covered separately below
1064 unreachable!()
1065 }
David Tolnay7db73692019-10-20 14:51:12 -04001066 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001067 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001068 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001069 Type::SliceRefU8(_) => {
1070 write!(out, "::rust::Slice<uint8_t>");
1071 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001072 Type::Fn(f) => {
1073 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1074 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001075 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001076 None => write!(out, "void"),
1077 }
1078 write!(out, "(");
1079 for (i, arg) in f.args.iter().enumerate() {
1080 if i > 0 {
1081 write!(out, ", ");
1082 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001083 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001084 }
1085 write!(out, ")>");
1086 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001087 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001088 }
1089}
1090
David Tolnayf6a89f22020-05-10 23:39:27 -07001091fn write_atom(out: &mut OutFile, atom: Atom) {
1092 match atom {
1093 Bool => write!(out, "bool"),
1094 U8 => write!(out, "uint8_t"),
1095 U16 => write!(out, "uint16_t"),
1096 U32 => write!(out, "uint32_t"),
1097 U64 => write!(out, "uint64_t"),
1098 Usize => write!(out, "size_t"),
1099 I8 => write!(out, "int8_t"),
1100 I16 => write!(out, "int16_t"),
1101 I32 => write!(out, "int32_t"),
1102 I64 => write!(out, "int64_t"),
1103 Isize => write!(out, "::rust::isize"),
1104 F32 => write!(out, "float"),
1105 F64 => write!(out, "double"),
1106 CxxString => write!(out, "::std::string"),
1107 RustString => write!(out, "::rust::String"),
1108 }
1109}
1110
David Tolnaya7c2ea12020-10-30 21:32:53 -07001111fn write_type_space(out: &mut OutFile, ty: &Type) {
1112 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001113 write_space_after_type(out, ty);
1114}
1115
1116fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001117 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001118 Type::Ident(_)
1119 | Type::RustBox(_)
1120 | Type::UniquePtr(_)
1121 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001122 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001123 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001124 | Type::SliceRefU8(_)
1125 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001126 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001127 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001128 }
1129}
1130
David Tolnaycd08c442020-04-25 10:16:33 -07001131// Only called for legal referent types of unique_ptr and element types of
1132// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001133fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001134 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001135 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1136 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001137 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001138 }
1139}
1140
David Tolnayacdf20a2020-04-25 12:40:53 -07001141// Only called for legal referent types of unique_ptr and element types of
1142// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001143fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001144 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001145 Type::Ident(ident) => ident.to_symbol(types),
1146 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001147 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001148 }
1149}
1150
David Tolnaya7c2ea12020-10-30 21:32:53 -07001151fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001152 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001153 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001154 if let Type::RustBox(ty) = ty {
1155 if let Type::Ident(inner) = &ty.inner {
1156 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001157 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001158 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001159 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001160 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001161 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001162 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001163 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001164 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001165 }
David Tolnay7db73692019-10-20 14:51:12 -04001166 } else if let Type::UniquePtr(ptr) = ty {
1167 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001168 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001169 && (!out.types.aliases.contains_key(&inner.rust)
1170 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001171 {
David Tolnay7db73692019-10-20 14:51:12 -04001172 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001173 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001174 }
1175 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001176 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001177 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001178 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001179 && (!out.types.aliases.contains_key(&inner.rust)
1180 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001181 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001182 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001183 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001184 }
1185 }
1186 }
1187 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001188 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001189
David Tolnay750755e2020-03-01 13:04:08 -08001190 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001191 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001192 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001193 if let Type::RustBox(ty) = ty {
1194 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001195 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001196 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001197 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001198 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001199 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001200 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001201 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001202 }
David Tolnay7db73692019-10-20 14:51:12 -04001203 }
1204 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001205 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001206 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001207}
1208
Adrian Taylorc8713432020-10-21 18:20:55 -07001209fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1210 let inner = ident.to_fully_qualified();
1211 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001212
David Tolnay8f16ae72020-10-08 18:21:13 -07001213 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1214 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001215 writeln!(
1216 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001217 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001218 instance, inner,
1219 );
1220 writeln!(
1221 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001222 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001223 instance, inner,
1224 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001225 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001226}
1227
David Tolnaya7c2ea12020-10-30 21:32:53 -07001228fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001229 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001230 let inner = to_typename(&element, out.types);
1231 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001232
David Tolnay8f16ae72020-10-08 18:21:13 -07001233 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1234 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001235 writeln!(
1236 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001237 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001238 instance, inner,
1239 );
1240 writeln!(
1241 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001242 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001243 instance, inner,
1244 );
1245 writeln!(
1246 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001247 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001248 instance, inner,
1249 );
David Tolnay219c0792020-04-24 20:31:37 -07001250 writeln!(
1251 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001252 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001253 inner, instance,
1254 );
David Tolnay503d0192020-04-24 22:18:56 -07001255 writeln!(
1256 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001257 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001258 instance,
1259 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001260 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001261}
1262
Adrian Taylorc8713432020-10-21 18:20:55 -07001263fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1264 let inner = ident.to_fully_qualified();
1265 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001266
1267 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001268 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001269 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001270 writeln!(out, "}}");
1271
1272 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001273 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001274 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001275 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001276}
1277
David Tolnaya7c2ea12020-10-30 21:32:53 -07001278fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001279 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001280 let inner = to_typename(&element, out.types);
1281 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001282
Myron Ahneba35cf2020-02-05 19:41:51 +07001283 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001284 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001285 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001286 writeln!(out, "}}");
1287
1288 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001289 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1290 writeln!(
1291 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001292 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001293 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001294 );
1295 writeln!(out, "}}");
1296
1297 writeln!(out, "template <>");
1298 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001299 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001300 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001301
1302 writeln!(out, "template <>");
1303 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1304 writeln!(
1305 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001306 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001307 instance,
1308 );
1309 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001310
1311 writeln!(out, "template <>");
1312 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001313 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001314 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001315}
1316
David Tolnaya7c2ea12020-10-30 21:32:53 -07001317fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001318 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001319 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001320
David Tolnay8f16ae72020-10-08 18:21:13 -07001321 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1322 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001323
David Tolnaya7c2ea12020-10-30 21:32:53 -07001324 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001325
David Tolnay8f16ae72020-10-08 18:21:13 -07001326 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001327}
1328
1329// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001330fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001331 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001332 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001333 let inner = to_typename(ty, out.types);
1334 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001335
David Tolnay63da4d32020-04-25 09:41:12 -07001336 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001337 // Some aliases are to opaque types; some are to trivial types. We can't
1338 // know at code generation time, so we generate both C++ and Rust side
1339 // bindings for a "new" method anyway. But the Rust code can't be called
1340 // for Opaque types because the 'new' method is not implemented.
1341 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001342 out.types.structs.contains_key(&ident.rust)
1343 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001344 }
David Tolnay63da4d32020-04-25 09:41:12 -07001345 _ => false,
1346 };
1347
David Tolnay7db73692019-10-20 14:51:12 -04001348 writeln!(
1349 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001350 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001351 inner,
1352 );
1353 writeln!(
1354 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001355 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001356 inner,
1357 );
1358 writeln!(
1359 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001360 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001361 instance, inner,
1362 );
David Tolnay7e219b82020-03-01 13:14:51 -08001363 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001364 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001365 if can_construct_from_value {
1366 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001367 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001368 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001369 instance, inner, inner,
1370 );
David Tolnay63da4d32020-04-25 09:41:12 -07001371 writeln!(
1372 out,
1373 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1374 inner, inner,
1375 );
1376 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001377 }
David Tolnay7db73692019-10-20 14:51:12 -04001378 writeln!(
1379 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001380 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001381 instance, inner, inner,
1382 );
David Tolnay7e219b82020-03-01 13:14:51 -08001383 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001384 writeln!(out, "}}");
1385 writeln!(
1386 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001387 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001388 inner, instance, inner,
1389 );
1390 writeln!(out, " return ptr.get();");
1391 writeln!(out, "}}");
1392 writeln!(
1393 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001394 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001395 inner, instance, inner,
1396 );
1397 writeln!(out, " return ptr.release();");
1398 writeln!(out, "}}");
1399 writeln!(
1400 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001401 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001402 instance, inner,
1403 );
1404 writeln!(out, " ptr->~unique_ptr();");
1405 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001406}
Myron Ahneba35cf2020-02-05 19:41:51 +07001407
David Tolnaya7c2ea12020-10-30 21:32:53 -07001408fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001409 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001410 let inner = to_typename(&element, out.types);
1411 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001412
David Tolnay8f16ae72020-10-08 18:21:13 -07001413 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1414 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001415 writeln!(
1416 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001417 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001418 instance, inner,
1419 );
1420 writeln!(out, " return s.size();");
1421 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001422 writeln!(
1423 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001424 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001425 inner, instance, inner,
1426 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001427 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001428 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001429
David Tolnaya7c2ea12020-10-30 21:32:53 -07001430 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001431
David Tolnay8f16ae72020-10-08 18:21:13 -07001432 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001433}