blob: 507649ec05cfc0e4bec386f922d283583e925e67 [file] [log] [blame]
David Tolnayb725d5a2020-12-20 20:27:10 -08001use crate::gen::block::Block;
David Tolnayf7b81fb2020-11-01 22:39:04 -08002use crate::gen::nested::NamespaceEntries;
David Tolnay8810a542020-10-31 21:39:22 -07003use crate::gen::out::OutFile;
David Tolnay942cffd2020-11-03 18:27:10 -08004use crate::gen::{builtin, include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04005use crate::syntax::atom::Atom::{self, *};
David Tolnay891061b2020-04-19 22:42:33 -07006use crate::syntax::symbol::Symbol;
David Tolnay5b1d8632020-12-20 22:05:11 -08007use crate::syntax::trivial::{self, TrivialReason};
Adrian Taylorc8713432020-10-21 18:20:55 -07008use crate::syntax::{
David Tolnay75ea17c2020-12-06 21:08:34 -08009 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, RustName, Signature, Struct, Trait,
David Tolnay5b1d8632020-12-20 22:05:11 -080010 Type, TypeAlias, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070011};
David Tolnay7db73692019-10-20 14:51:12 -040012use proc_macro2::Ident;
David Tolnay5439fa12020-11-03 18:45:01 -080013use std::collections::{HashMap, HashSet};
David Tolnay7db73692019-10-20 14:51:12 -040014
David Tolnayac7188c2020-11-01 16:58:16 -080015pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080016 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040017 let out = &mut out_file;
18
David Tolnaydfb82d72020-11-02 00:10:10 -080019 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070020 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040021
David Tolnay7b0e5102020-11-01 23:22:12 -080022 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080023 write_data_structures(out, apis);
24 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080025 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040026
David Tolnay3374d8d2020-10-31 22:18:45 -070027 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070028 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070029
David Tolnayac7188c2020-11-01 16:58:16 -080030 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070031}
32
David Tolnay7b0e5102020-11-01 23:22:12 -080033fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
34 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080035 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080036 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080037 _ => false,
38 };
Adrian Taylorc8713432020-10-21 18:20:55 -070039
David Tolnay7b0e5102020-11-01 23:22:12 -080040 let apis_by_namespace =
41 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
42
David Tolnayd920be52020-11-01 23:34:30 -080043 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080044
David Tolnayd920be52020-11-01 23:34:30 -080045 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080046 let apis = ns_entries.direct_content();
47
48 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080049 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080050 match api {
David Tolnay17a934c2020-11-02 00:40:04 -080051 Api::Struct(strct) => write_struct_decl(out, &strct.name.cxx),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080052 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080053 Api::CxxType(ety) => write_struct_using(out, &ety.name),
54 Api::RustType(ety) => write_struct_decl(out, &ety.name.cxx),
David Tolnay7b0e5102020-11-01 23:22:12 -080055 _ => unreachable!(),
56 }
David Tolnay7db73692019-10-20 14:51:12 -040057 }
David Tolnay7db73692019-10-20 14:51:12 -040058
David Tolnay7b0e5102020-11-01 23:22:12 -080059 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080060 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
61 write(out, nested_ns_entries, indent + 2);
62 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080063 }
Adrian Taylorf9213622020-10-31 22:25:42 -070064 }
65}
66
David Tolnaye1109d92020-11-01 20:51:56 -080067fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayf94bef12020-04-17 14:46:42 -070068 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070069 for api in apis {
David Tolnay102c7ea2020-11-08 18:58:09 -080070 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayf94bef12020-04-17 14:46:42 -070071 if let Some(receiver) = &efn.sig.receiver {
72 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070073 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070074 .or_insert_with(Vec::new)
75 .push(efn);
76 }
77 }
78 }
Joel Galenson968738f2020-04-15 14:19:33 -070079
David Tolnay5439fa12020-11-03 18:45:01 -080080 let mut structs_written = HashSet::new();
81 let mut toposorted_structs = out.types.toposorted_structs.iter();
David Tolnay9622b462020-11-02 21:34:42 -080082 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070083 match api {
David Tolnay5439fa12020-11-03 18:45:01 -080084 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
85 for next in &mut toposorted_structs {
86 if !out.types.cxx.contains(&strct.name.rust) {
87 out.next_section();
David Tolnay102c7ea2020-11-08 18:58:09 -080088 let methods = methods_for_type
89 .get(&strct.name.rust)
90 .map(Vec::as_slice)
91 .unwrap_or_default();
92 write_struct(out, next, methods);
David Tolnay5439fa12020-11-03 18:45:01 -080093 }
94 structs_written.insert(&next.name.rust);
95 if next.name.rust == strct.name.rust {
96 break;
97 }
98 }
99 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700100 Api::Enum(enm) => {
101 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800102 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700103 check_enum(out, enm);
104 } else {
105 write_enum(out, enm);
106 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700107 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700108 Api::RustType(ety) => {
David Tolnay8d067de2020-12-26 16:18:23 -0800109 out.next_section();
110 let methods = methods_for_type
111 .get(&ety.name.rust)
112 .map(Vec::as_slice)
113 .unwrap_or_default();
114 write_opaque_type(out, ety, methods);
David Tolnayc1fe0052020-04-17 15:15:06 -0700115 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700116 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400117 }
118 }
119
David Tolnayfabca772020-10-03 21:25:41 -0700120 out.next_section();
121 for api in apis {
122 if let Api::TypeAlias(ety) = api {
Adrian Taylorf831a5a2020-12-07 16:49:19 -0800123 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
David Tolnay5b1d8632020-12-20 22:05:11 -0800124 check_trivial_extern_type(out, ety, reasons)
David Tolnayfabca772020-10-03 21:25:41 -0700125 }
126 }
127 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800128}
129
David Tolnaye1109d92020-11-01 20:51:56 -0800130fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700131 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400132 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800133 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800134 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay04770742020-11-01 13:50:50 -0800135 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
136 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
137 _ => {}
138 }
David Tolnay7db73692019-10-20 14:51:12 -0400139 }
David Tolnay7da38202020-11-27 17:36:16 -0800140
141 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400142 }
143
144 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800145 match api {
146 Api::Struct(strct) => write_struct_operators(out, strct),
147 Api::RustFunction(efn) => {
148 out.next_section();
149 write_rust_function_shim(out, efn);
150 }
151 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400152 }
153 }
David Tolnay7db73692019-10-20 14:51:12 -0400154}
155
David Tolnay7da38202020-11-27 17:36:16 -0800156fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
157 out.set_namespace(Default::default());
158 out.begin_block(Block::Namespace("std"));
159
160 for api in apis {
161 if let Api::Struct(strct) = api {
162 if derive::contains(&strct.derives, Trait::Hash) {
163 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800164 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800165 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800166 let qualified = strct.name.to_fully_qualified();
167 writeln!(out, "template <> struct hash<{}> {{", qualified);
168 writeln!(
169 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800170 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800171 qualified,
172 );
173 let link_name = mangle::operator(&strct.name, "hash");
174 write!(out, " return ::");
175 for name in &strct.name.namespace {
176 write!(out, "{}::", name);
177 }
178 writeln!(out, "{}(self);", link_name);
179 writeln!(out, " }}");
180 writeln!(out, "}};");
181 }
182 }
183 }
184
185 out.end_block(Block::Namespace("std"));
186}
187
David Tolnaydfb82d72020-11-02 00:10:10 -0800188fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
189 for api in apis {
190 if let Api::Include(include) = api {
191 out.include.insert(include);
192 }
193 }
194
David Tolnaya7c2ea12020-10-30 21:32:53 -0700195 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400196 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700197 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700198 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
199 | Some(I64) => out.include.cstdint = true,
200 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800201 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700202 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800203 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800204 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700205 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800206 Type::RustBox(_) => out.builtin.rust_box = true,
207 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700208 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb3b24a12020-12-01 15:27:43 -0800209 Type::SharedPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800210 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700211 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800212 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800213 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800214 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800215 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400216 }
217 }
David Tolnayec66d112020-10-31 21:00:26 -0700218}
David Tolnayf51447e2020-03-06 14:14:27 -0800219
David Tolnay102c7ea2020-11-08 18:58:09 -0800220fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800221 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800222 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800223
David Tolnay17a934c2020-11-02 00:40:04 -0800224 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800225 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700226 writeln!(out, "#ifndef {}", guard);
227 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400228 for line in strct.doc.to_string().lines() {
229 writeln!(out, "//{}", line);
230 }
David Tolnay17a934c2020-11-02 00:40:04 -0800231 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800232
David Tolnay7db73692019-10-20 14:51:12 -0400233 for field in &strct.fields {
234 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700235 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400236 writeln!(out, "{};", field.ident);
237 }
David Tolnay84389352020-11-27 17:12:10 -0800238
David Tolnay5554ebd2020-12-10 11:34:41 -0800239 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800240
David Tolnay102c7ea2020-11-08 18:58:09 -0800241 for method in methods {
242 write!(out, " ");
243 let sig = &method.sig;
244 let local_name = method.name.cxx.to_string();
245 write_rust_function_shim_decl(out, &local_name, sig, false);
246 writeln!(out, ";");
247 }
David Tolnay84389352020-11-27 17:12:10 -0800248
David Tolnayb960ed22020-11-27 14:34:30 -0800249 if operator_eq {
250 writeln!(
251 out,
252 " bool operator==(const {} &) const noexcept;",
253 strct.name.cxx,
254 );
255 writeln!(
256 out,
257 " bool operator!=(const {} &) const noexcept;",
258 strct.name.cxx,
259 );
260 }
David Tolnay84389352020-11-27 17:12:10 -0800261
262 if operator_ord {
263 writeln!(
264 out,
265 " bool operator<(const {} &) const noexcept;",
266 strct.name.cxx,
267 );
268 writeln!(
269 out,
270 " bool operator<=(const {} &) const noexcept;",
271 strct.name.cxx,
272 );
273 writeln!(
274 out,
275 " bool operator>(const {} &) const noexcept;",
276 strct.name.cxx,
277 );
278 writeln!(
279 out,
280 " bool operator>=(const {} &) const noexcept;",
281 strct.name.cxx,
282 );
283 }
284
David Tolnay5554ebd2020-12-10 11:34:41 -0800285 out.include.type_traits = true;
286 writeln!(out, " using IsRelocatable = ::std::true_type;");
287
David Tolnay7db73692019-10-20 14:51:12 -0400288 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700289 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400290}
291
292fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
293 writeln!(out, "struct {};", ident);
294}
295
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800296fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800297 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800298 write_atom(out, enm.repr);
299 writeln!(out, ";");
300}
301
David Tolnay8faec772020-11-02 00:18:19 -0800302fn write_struct_using(out: &mut OutFile, ident: &Pair) {
303 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800304}
305
David Tolnay8d067de2020-12-26 16:18:23 -0800306fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800307 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800308 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700309 writeln!(out, "#ifndef {}", guard);
310 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700311 for line in ety.doc.to_string().lines() {
312 writeln!(out, "//{}", line);
313 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800314
David Tolnay365fc7c2020-11-25 16:08:13 -0800315 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800316 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800317 out,
318 "struct {} final : public ::rust::Opaque {{",
319 ety.name.cxx,
320 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800321
Joel Galenson968738f2020-04-15 14:19:33 -0700322 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800323 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700324 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800325 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700326 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800327 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800328 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800329
David Tolnay8d067de2020-12-26 16:18:23 -0800330 if !methods.is_empty() {
331 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700332 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800333
334 out.include.cstddef = true;
335 writeln!(out, "private:");
336 writeln!(out, " struct layout {{");
337 writeln!(out, " static ::std::size_t size() noexcept;");
338 writeln!(out, " static ::std::size_t align() noexcept;");
339 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700340 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700341 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700342}
343
David Tolnay0b9b9f82020-11-01 20:41:00 -0800344fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800345 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800346 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700347 writeln!(out, "#ifndef {}", guard);
348 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700349 for line in enm.doc.to_string().lines() {
350 writeln!(out, "//{}", line);
351 }
David Tolnay17a934c2020-11-02 00:40:04 -0800352 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700353 write_atom(out, enm.repr);
354 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700355 for variant in &enm.variants {
David Tolnaye6f62142020-12-21 16:00:41 -0800356 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700357 }
358 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700359 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700360}
361
David Tolnay0b9b9f82020-11-01 20:41:00 -0800362fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800363 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800364 out.include.type_traits = true;
365 writeln!(
366 out,
367 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
368 enm.name.cxx,
369 );
David Tolnay17a934c2020-11-02 00:40:04 -0800370 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700371 write_atom(out, enm.repr);
372 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700373 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700374 write!(out, "static_assert(static_cast<");
375 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700376 writeln!(
377 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700378 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800379 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700380 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700381 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700382}
383
David Tolnay5b1d8632020-12-20 22:05:11 -0800384fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800385 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700386 // necessary for soundness. That's because triviality is always declared by
387 // the user in the form of an unsafe impl of cxx::ExternType:
388 //
389 // unsafe impl ExternType for MyType {
390 // type Id = cxx::type_id!("...");
391 // type Kind = cxx::kind::Trivial;
392 // }
393 //
394 // Since the user went on the record with their unsafe impl to unsafely
395 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800396 // them if that were wrong. However, in practice correctly reasoning about
397 // the relocatability of C++ types is challenging, particularly if the type
398 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700399 //
David Tolnay174bd952020-11-02 09:23:12 -0800400 // There may be legitimate reasons to opt out of this assertion for support
401 // of types that the programmer knows are soundly Rust-movable despite not
402 // being recognized as such by the C++ type system due to a move constructor
403 // or destructor. To opt out of the relocatability check, they need to do
404 // one of the following things in any header used by `include!` in their
405 // bridge.
406 //
407 // --- if they define the type:
408 // struct MyType {
409 // ...
410 // + using IsRelocatable = std::true_type;
411 // };
412 //
413 // --- otherwise:
414 // + template <>
415 // + struct rust::IsRelocatable<MyType> : std::true_type {};
416 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700417
David Tolnay5b1d8632020-12-20 22:05:11 -0800418 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800419 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800420 writeln!(out, "static_assert(");
421 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
422 writeln!(
423 out,
424 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
425 id.trim_start_matches("::"),
426 trivial::as_what(&alias.name, reasons),
427 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700428}
429
David Tolnayb960ed22020-11-27 14:34:30 -0800430fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
431 out.set_namespace(&strct.name.namespace);
432 out.begin_block(Block::ExternC);
433
434 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800435 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800436 writeln!(
437 out,
438 "bool {}(const {1} &, const {1} &) noexcept;",
439 link_name, strct.name.cxx,
440 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800441
442 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800443 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800444 writeln!(
445 out,
446 "bool {}(const {1} &, const {1} &) noexcept;",
447 link_name, strct.name.cxx,
448 );
449 }
David Tolnayb960ed22020-11-27 14:34:30 -0800450 }
451
David Tolnay84389352020-11-27 17:12:10 -0800452 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800453 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800454 writeln!(
455 out,
456 "bool {}(const {1} &, const {1} &) noexcept;",
457 link_name, strct.name.cxx,
458 );
459
David Tolnaya05f9402020-11-27 17:44:05 -0800460 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800461 writeln!(
462 out,
463 "bool {}(const {1} &, const {1} &) noexcept;",
464 link_name, strct.name.cxx,
465 );
466
467 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800468 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800469 writeln!(
470 out,
471 "bool {}(const {1} &, const {1} &) noexcept;",
472 link_name, strct.name.cxx,
473 );
474
David Tolnaya05f9402020-11-27 17:44:05 -0800475 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800476 writeln!(
477 out,
478 "bool {}(const {1} &, const {1} &) noexcept;",
479 link_name, strct.name.cxx,
480 );
481 }
482 }
483
David Tolnay7da38202020-11-27 17:36:16 -0800484 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800485 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800486 let link_name = mangle::operator(&strct.name, "hash");
487 writeln!(
488 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800489 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800490 link_name, strct.name.cxx,
491 );
492 }
493
David Tolnayb960ed22020-11-27 14:34:30 -0800494 out.end_block(Block::ExternC);
495}
496
497fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
498 if out.header {
499 return;
500 }
501
502 out.set_namespace(&strct.name.namespace);
503
504 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800505 out.next_section();
506 writeln!(
507 out,
508 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
509 strct.name.cxx,
510 );
David Tolnaya05f9402020-11-27 17:44:05 -0800511 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800512 writeln!(out, " return {}(*this, rhs);", link_name);
513 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800514
David Tolnayb960ed22020-11-27 14:34:30 -0800515 out.next_section();
516 writeln!(
517 out,
518 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
519 strct.name.cxx,
520 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800521 if derive::contains(&strct.derives, Trait::Eq) {
522 writeln!(out, " return !(*this == rhs);");
523 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800524 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800525 writeln!(out, " return {}(*this, rhs);", link_name);
526 }
David Tolnayb960ed22020-11-27 14:34:30 -0800527 writeln!(out, "}}");
528 }
David Tolnay84389352020-11-27 17:12:10 -0800529
530 if derive::contains(&strct.derives, Trait::PartialOrd) {
531 out.next_section();
532 writeln!(
533 out,
534 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
535 strct.name.cxx,
536 );
David Tolnaya05f9402020-11-27 17:44:05 -0800537 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800538 writeln!(out, " return {}(*this, rhs);", link_name);
539 writeln!(out, "}}");
540
541 out.next_section();
542 writeln!(
543 out,
544 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
545 strct.name.cxx,
546 );
David Tolnaya05f9402020-11-27 17:44:05 -0800547 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800548 writeln!(out, " return {}(*this, rhs);", link_name);
549 writeln!(out, "}}");
550
551 out.next_section();
552 writeln!(
553 out,
554 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
555 strct.name.cxx,
556 );
557 if derive::contains(&strct.derives, Trait::Ord) {
558 writeln!(out, " return !(*this <= rhs);");
559 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800560 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800561 writeln!(out, " return {}(*this, rhs);", link_name);
562 }
563 writeln!(out, "}}");
564
565 out.next_section();
566 writeln!(
567 out,
568 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
569 strct.name.cxx,
570 );
571 if derive::contains(&strct.derives, Trait::Ord) {
572 writeln!(out, " return !(*this < rhs);");
573 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800574 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800575 writeln!(out, " return {}(*this, rhs);", link_name);
576 }
577 writeln!(out, "}}");
578 }
David Tolnayb960ed22020-11-27 14:34:30 -0800579}
580
David Tolnay0b9b9f82020-11-01 20:41:00 -0800581fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800582 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800583 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800584 out.begin_block(Block::ExternC);
David Tolnaye1476af2020-11-01 13:47:25 -0800585 if let Some(annotation) = &out.opt.cxx_impl_annotations {
David Tolnaycc1ae762020-10-31 15:53:50 -0700586 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700587 }
David Tolnayebef4a22020-03-17 15:33:47 -0700588 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700589 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700590 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700591 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700592 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700593 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700594 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700595 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700596 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800597 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700598 write!(out, "const ");
599 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700600 write!(
601 out,
602 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800603 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700604 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700605 }
David Tolnay7db73692019-10-20 14:51:12 -0400606 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700607 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400608 write!(out, ", ");
609 }
David Tolnaya46a2372020-03-06 10:03:48 -0800610 if arg.ty == RustString {
611 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700612 } else if let Type::RustVec(_) = arg.ty {
613 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800614 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700615 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400616 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700617 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400618 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700619 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400620 write!(out, ", ");
621 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700622 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400623 write!(out, "*return$");
624 }
625 writeln!(out, ") noexcept {{");
626 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700627 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700628 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800629 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700630 Some(receiver) => write!(
631 out,
632 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700633 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800634 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700635 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700636 }
David Tolnay7db73692019-10-20 14:51:12 -0400637 for (i, arg) in efn.args.iter().enumerate() {
638 if i > 0 {
639 write!(out, ", ");
640 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700641 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400642 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700643 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700644 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800645 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700646 write!(out, " const");
647 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700648 }
649 write!(out, " = ");
650 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800651 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700652 Some(receiver) => write!(
653 out,
654 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700655 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800656 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700657 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700658 }
659 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400660 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700661 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700662 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700663 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700664 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700665 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700666 writeln!(out, " [&] {{");
667 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700668 }
David Tolnay7db73692019-10-20 14:51:12 -0400669 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700670 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400671 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700672 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400673 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700674 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400675 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700676 }
677 match &efn.ret {
678 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700679 Some(Type::Str(_)) if !indirect_return => {
680 out.builtin.rust_str_repr = true;
681 write!(out, "::rust::impl<::rust::Str>::repr(");
682 }
David Tolnay73b72642020-11-25 17:44:05 -0800683 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700684 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800685 write!(out, "::rust::impl<");
686 write_type(out, ty);
687 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700688 }
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 Tolnay17a934c2020-11-02 00:40:04 -0800692 None => write!(out, "{}$(", efn.name.rust),
693 Some(_) => write!(out, "(self.*{}$)(", efn.name.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 {
David Tolnay74d6d512020-10-31 22:22:03 -0700706 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700707 write!(
708 out,
709 "::rust::impl<::rust::Str>::new_unchecked({})",
710 arg.ident,
711 );
David Tolnaya46a2372020-03-06 10:03:48 -0800712 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700713 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800714 write!(
715 out,
716 "::rust::String(::rust::unsafe_bitcopy, *{})",
717 arg.ident,
718 );
David Tolnay313b10e2020-04-25 16:30:51 -0700719 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700720 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700721 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700722 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800723 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800724 write_type(out, &arg.ty);
725 write!(out, "(static_cast<");
726 if slice.mutability.is_none() {
727 write!(out, "const ");
728 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800729 write_type_space(out, &slice.inner);
730 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700731 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700732 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800733 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400734 } else {
735 write!(out, "{}", arg.ident);
736 }
737 }
738 write!(out, ")");
739 match &efn.ret {
740 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
741 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800742 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400743 _ => {}
744 }
745 if indirect_return {
746 write!(out, ")");
747 }
748 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700749 if efn.throws {
750 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800751 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700752 writeln!(out, " throw$.ptr = nullptr;");
753 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700754 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700755 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700756 writeln!(
757 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800758 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700759 );
David Tolnay5d121442020-03-17 22:14:40 -0700760 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700761 writeln!(out, " return throw$;");
762 }
David Tolnay7db73692019-10-20 14:51:12 -0400763 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700764 for arg in &efn.args {
765 if let Type::Fn(f) = &arg.ty {
766 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700767 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700768 }
769 }
David Tolnayca563ee2020-11-01 20:12:27 -0800770 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700771}
772
773fn write_function_pointer_trampoline(
774 out: &mut OutFile,
775 efn: &ExternFn,
776 var: &Ident,
777 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700778) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700779 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700780 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700781 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700782
783 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700784 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
785 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400786}
787
David Tolnay0b9b9f82020-11-01 20:41:00 -0800788fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800789 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800790 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700791 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700792 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700793 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800794 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700795}
796
797fn write_rust_function_decl_impl(
798 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700799 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700800 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700801 indirect_call: bool,
802) {
David Tolnay04770742020-11-01 13:50:50 -0800803 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700804 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700805 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700806 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700807 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700808 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700809 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700810 write!(out, "{}(", link_name);
811 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700812 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800813 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700814 write!(out, "const ");
815 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700816 write!(
817 out,
818 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800819 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700820 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700821 needs_comma = true;
822 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700823 for arg in &sig.args {
824 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400825 write!(out, ", ");
826 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700827 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700828 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400829 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700830 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700831 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400832 write!(out, ", ");
833 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700834 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400835 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700836 needs_comma = true;
837 }
838 if indirect_call {
839 if needs_comma {
840 write!(out, ", ");
841 }
842 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400843 }
844 writeln!(out, ") noexcept;");
845}
846
David Tolnay0b9b9f82020-11-01 20:41:00 -0800847fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800848 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400849 for line in efn.doc.to_string().lines() {
850 writeln!(out, "//{}", line);
851 }
David Tolnaya73853b2020-04-20 01:19:56 -0700852 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800853 None => efn.name.cxx.to_string(),
854 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700855 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700856 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700857 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700858 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700859}
860
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700861fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700862 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700863 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700864 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700865 indirect_call: bool,
866) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700867 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700868 write!(out, "{}(", local_name);
869 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400870 if i > 0 {
871 write!(out, ", ");
872 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700873 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400874 write!(out, "{}", arg.ident);
875 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700876 if indirect_call {
877 if !sig.args.is_empty() {
878 write!(out, ", ");
879 }
880 write!(out, "void *extern$");
881 }
David Tolnay1e548172020-03-16 13:37:09 -0700882 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700883 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800884 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700885 write!(out, " const");
886 }
887 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700888 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700889 write!(out, " noexcept");
890 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700891}
892
893fn write_rust_function_shim_impl(
894 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700895 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700896 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700897 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700898 indirect_call: bool,
899) {
900 if out.header && sig.receiver.is_some() {
901 // We've already defined this inside the struct.
902 return;
903 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700904 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400905 if out.header {
906 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700907 return;
David Tolnay7db73692019-10-20 14:51:12 -0400908 }
David Tolnay439cde22020-04-20 00:46:25 -0700909 writeln!(out, " {{");
910 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700911 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700912 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700913 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700914 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700915 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700916 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
917 }
918 }
919 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700920 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700921 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700922 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700923 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700924 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700925 writeln!(out, "> return$;");
926 write!(out, " ");
927 } else if let Some(ret) = &sig.ret {
928 write!(out, "return ");
929 match ret {
930 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700931 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700932 write!(out, "::from_raw(");
933 }
934 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700935 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700936 write!(out, "(");
937 }
938 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700939 Type::Str(_) => {
940 out.builtin.rust_str_new_unchecked = true;
941 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
942 }
David Tolnay73b72642020-11-25 17:44:05 -0800943 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700944 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800945 write!(out, "::rust::impl<");
946 write_type(out, ret);
947 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700948 }
David Tolnay439cde22020-04-20 00:46:25 -0700949 _ => {}
950 }
951 }
952 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700953 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700954 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700955 }
956 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -0800957 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -0700958 if sig.receiver.is_some() {
959 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -0800960 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700961 }
David Tolnay9d840362020-11-10 08:50:40 -0800962 for arg in &sig.args {
963 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700964 write!(out, ", ");
965 }
966 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700967 Type::Str(_) => {
968 out.builtin.rust_str_repr = true;
969 write!(out, "::rust::impl<::rust::Str>::repr(");
970 }
David Tolnay73b72642020-11-25 17:44:05 -0800971 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700972 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800973 write!(out, "::rust::impl<");
974 write_type(out, &arg.ty);
975 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700976 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700977 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700978 _ => {}
979 }
980 write!(out, "{}", arg.ident);
981 match &arg.ty {
982 Type::RustBox(_) => write!(out, ".into_raw()"),
983 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800984 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700985 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700986 _ => {}
987 }
David Tolnay9d840362020-11-10 08:50:40 -0800988 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700989 }
990 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -0800991 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700992 write!(out, ", ");
993 }
994 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -0800995 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -0700996 }
997 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -0800998 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -0700999 write!(out, ", ");
1000 }
1001 write!(out, "extern$");
1002 }
1003 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001004 if !indirect_return {
1005 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -08001006 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001007 write!(out, ")");
1008 }
David Tolnay439cde22020-04-20 00:46:25 -07001009 }
1010 }
1011 writeln!(out, ";");
1012 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001013 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001014 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001015 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001016 writeln!(out, " }}");
1017 }
1018 if indirect_return {
1019 out.include.utility = true;
1020 writeln!(out, " return ::std::move(return$.value);");
1021 }
1022 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001023}
1024
David Tolnaya7c2ea12020-10-30 21:32:53 -07001025fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001026 match ty {
1027 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001028 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001029 }
1030}
1031
David Tolnay75dca2e2020-03-25 20:17:52 -07001032fn indirect_return(sig: &Signature, types: &Types) -> bool {
1033 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001034 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001035 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001036}
1037
David Tolnaya7c2ea12020-10-30 21:32:53 -07001038fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001039 match ty {
1040 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001041 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001042 write!(out, "*");
1043 }
1044 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001045 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001046 write!(out, "const ");
1047 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001048 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001049 write!(out, " *");
1050 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001051 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001052 }
1053}
1054
David Tolnaya7c2ea12020-10-30 21:32:53 -07001055fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1056 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001057 match ty {
1058 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001059 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001060 _ => write_space_after_type(out, ty),
1061 }
1062}
1063
David Tolnaya7c2ea12020-10-30 21:32:53 -07001064fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001065 match ty {
1066 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001067 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001068 write!(out, "*");
1069 }
David Tolnay4a441222020-01-25 16:24:27 -08001070 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001071 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001072 write!(out, "const ");
1073 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001074 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001075 write!(out, " *");
1076 }
David Tolnay73b72642020-11-25 17:44:05 -08001077 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001078 out.builtin.ptr_len = true;
1079 write!(out, "::rust::repr::PtrLen ");
1080 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001081 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1082 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001083 }
1084}
1085
David Tolnaya7c2ea12020-10-30 21:32:53 -07001086fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001087 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001088 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001089 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001090 write!(out, "*");
1091 }
David Tolnay73b72642020-11-25 17:44:05 -08001092 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001093 out.builtin.ptr_len = true;
1094 write!(out, "::rust::repr::PtrLen ");
1095 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001096 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001097 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001098 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001099 write!(out, "*");
1100 }
1101 write!(out, "{}", arg.ident);
1102}
1103
David Tolnaya7c2ea12020-10-30 21:32:53 -07001104fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001105 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001106 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001107 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001108 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001109 },
1110 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001111 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001112 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001113 write!(out, ">");
1114 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001115 Type::RustVec(ty) => {
1116 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001117 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001118 write!(out, ">");
1119 }
David Tolnay7db73692019-10-20 14:51:12 -04001120 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001121 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001122 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001123 write!(out, ">");
1124 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001125 Type::SharedPtr(ptr) => {
1126 write!(out, "::std::shared_ptr<");
1127 write_type(out, &ptr.inner);
1128 write!(out, ">");
1129 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001130 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001131 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001132 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001133 write!(out, ">");
1134 }
David Tolnay7db73692019-10-20 14:51:12 -04001135 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001136 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001137 write!(out, "const ");
1138 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001139 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001140 write!(out, " &");
1141 }
1142 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001143 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001144 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001145 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001146 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001147 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001148 write!(out, "const ");
1149 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001150 write_type(out, &slice.inner);
1151 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001152 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001153 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001154 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001155 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001156 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001157 None => write!(out, "void"),
1158 }
1159 write!(out, "(");
1160 for (i, arg) in f.args.iter().enumerate() {
1161 if i > 0 {
1162 write!(out, ", ");
1163 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001164 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001165 }
1166 write!(out, ")>");
1167 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001168 Type::Array(a) => {
1169 write!(out, "::std::array<");
1170 write_type(out, &a.inner);
1171 write!(out, ", {}>", &a.len);
1172 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001173 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001174 }
1175}
1176
David Tolnayf6a89f22020-05-10 23:39:27 -07001177fn write_atom(out: &mut OutFile, atom: Atom) {
1178 match atom {
1179 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001180 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001181 U8 => write!(out, "::std::uint8_t"),
1182 U16 => write!(out, "::std::uint16_t"),
1183 U32 => write!(out, "::std::uint32_t"),
1184 U64 => write!(out, "::std::uint64_t"),
1185 Usize => write!(out, "::std::size_t"),
1186 I8 => write!(out, "::std::int8_t"),
1187 I16 => write!(out, "::std::int16_t"),
1188 I32 => write!(out, "::std::int32_t"),
1189 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001190 Isize => write!(out, "::rust::isize"),
1191 F32 => write!(out, "float"),
1192 F64 => write!(out, "double"),
1193 CxxString => write!(out, "::std::string"),
1194 RustString => write!(out, "::rust::String"),
1195 }
1196}
1197
David Tolnaya7c2ea12020-10-30 21:32:53 -07001198fn write_type_space(out: &mut OutFile, ty: &Type) {
1199 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001200 write_space_after_type(out, ty);
1201}
1202
1203fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001204 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001205 Type::Ident(_)
1206 | Type::RustBox(_)
1207 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001208 | Type::SharedPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001209 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001210 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001211 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001212 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001213 | Type::Fn(_)
1214 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001215 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001216 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001217 }
1218}
1219
David Tolnay1bdb4712020-11-25 07:27:54 -08001220#[derive(Copy, Clone)]
1221enum UniquePtr<'a> {
David Tolnay75ea17c2020-12-06 21:08:34 -08001222 Ident(&'a RustName),
1223 CxxVector(&'a RustName),
David Tolnay1bdb4712020-11-25 07:27:54 -08001224}
1225
1226trait ToTypename {
1227 fn to_typename(&self, types: &Types) -> String;
1228}
1229
David Tolnay75ea17c2020-12-06 21:08:34 -08001230impl ToTypename for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001231 fn to_typename(&self, types: &Types) -> String {
1232 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001233 }
1234}
1235
David Tolnay1bdb4712020-11-25 07:27:54 -08001236impl<'a> ToTypename for UniquePtr<'a> {
1237 fn to_typename(&self, types: &Types) -> String {
1238 match self {
1239 UniquePtr::Ident(ident) => ident.to_typename(types),
1240 UniquePtr::CxxVector(element) => {
1241 format!("::std::vector<{}>", element.to_typename(types))
1242 }
1243 }
1244 }
1245}
1246
1247trait ToMangled {
1248 fn to_mangled(&self, types: &Types) -> Symbol;
1249}
1250
David Tolnay75ea17c2020-12-06 21:08:34 -08001251impl ToMangled for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001252 fn to_mangled(&self, types: &Types) -> Symbol {
1253 self.to_symbol(types)
1254 }
1255}
1256
1257impl<'a> ToMangled for UniquePtr<'a> {
1258 fn to_mangled(&self, types: &Types) -> Symbol {
1259 match self {
1260 UniquePtr::Ident(ident) => ident.to_mangled(types),
1261 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1262 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001263 }
1264}
1265
David Tolnaya7c2ea12020-10-30 21:32:53 -07001266fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001267 if out.header {
1268 return;
1269 }
1270
1271 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001272 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001273 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001274 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001275 if let Type::RustBox(ptr) = ty {
1276 if let Type::Ident(inner) = &ptr.inner {
1277 if Atom::from(&inner.rust).is_none()
1278 && (!out.types.aliases.contains_key(&inner.rust)
1279 || out.types.explicit_impls.contains(ty))
1280 {
1281 out.next_section();
1282 write_rust_box_extern(out, &out.types.resolve(&inner));
1283 }
David Tolnay7db73692019-10-20 14:51:12 -04001284 }
David Tolnayf33bc242020-12-04 18:27:02 -08001285 } else if let Type::RustVec(vec) = ty {
1286 if let Type::Ident(inner) = &vec.inner {
1287 if Atom::from(&inner.rust).is_none()
1288 && (!out.types.aliases.contains_key(&inner.rust)
1289 || out.types.explicit_impls.contains(ty))
1290 {
David Tolnay6787be62020-04-25 11:01:02 -07001291 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001292 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001293 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001294 }
David Tolnay7db73692019-10-20 14:51:12 -04001295 } else if let Type::UniquePtr(ptr) = ty {
1296 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001297 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001298 && (!out.types.aliases.contains_key(&inner.rust)
1299 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001300 {
David Tolnay7db73692019-10-20 14:51:12 -04001301 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001302 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001303 }
1304 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001305 } else if let Type::SharedPtr(ptr) = ty {
1306 if let Type::Ident(inner) = &ptr.inner {
1307 if Atom::from(&inner.rust).is_none()
David Tolnayb7453812020-12-01 21:46:55 -08001308 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnayb3b24a12020-12-01 15:27:43 -08001309 || out.types.explicit_impls.contains(ty))
1310 {
1311 out.next_section();
1312 write_shared_ptr(out, inner);
1313 }
1314 }
David Tolnayf33bc242020-12-04 18:27:02 -08001315 } else if let Type::CxxVector(vector) = ty {
1316 if let Type::Ident(inner) = &vector.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001317 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001318 && (!out.types.aliases.contains_key(&inner.rust)
1319 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001320 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001321 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001322 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001323 }
1324 }
1325 }
1326 }
David Tolnay0c033e32020-11-01 15:15:48 -08001327 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001328
David Tolnay0c033e32020-11-01 15:15:48 -08001329 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001330 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001331 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001332 if let Type::RustBox(ptr) = ty {
1333 if let Type::Ident(inner) = &ptr.inner {
1334 if Atom::from(&inner.rust).is_none()
1335 && (!out.types.aliases.contains_key(&inner.rust)
1336 || out.types.explicit_impls.contains(ty))
1337 {
1338 write_rust_box_impl(out, &out.types.resolve(&inner));
1339 }
David Tolnay7db73692019-10-20 14:51:12 -04001340 }
David Tolnayf33bc242020-12-04 18:27:02 -08001341 } else if let Type::RustVec(vec) = ty {
1342 if let Type::Ident(inner) = &vec.inner {
1343 if Atom::from(&inner.rust).is_none()
1344 && (!out.types.aliases.contains_key(&inner.rust)
1345 || out.types.explicit_impls.contains(ty))
1346 {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001347 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001348 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001349 }
David Tolnay7db73692019-10-20 14:51:12 -04001350 }
1351 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001352 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001353 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001354}
1355
David Tolnay8faec772020-11-02 00:18:19 -08001356fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001357 let inner = ident.to_fully_qualified();
1358 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001359
David Tolnay0f0162f2020-11-16 23:43:37 -08001360 writeln!(out, "#ifndef CXXBRIDGE1_RUST_BOX_{}", instance);
1361 writeln!(out, "#define CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001362 writeln!(
1363 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001364 "{} *cxxbridge1$box${}$alloc() noexcept;",
1365 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001366 );
1367 writeln!(
1368 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001369 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1370 instance, inner,
1371 );
1372 writeln!(
1373 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001374 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001375 instance, inner,
1376 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001377 writeln!(out, "#endif // CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001378}
1379
David Tolnay75ea17c2020-12-06 21:08:34 -08001380fn write_rust_vec_extern(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001381 let inner = element.to_typename(out.types);
1382 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001383
David Tolnay12320e12020-12-09 23:09:36 -08001384 out.include.cstddef = true;
1385
David Tolnay0f0162f2020-11-16 23:43:37 -08001386 writeln!(out, "#ifndef CXXBRIDGE1_RUST_VEC_{}", instance);
1387 writeln!(out, "#define CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001388 writeln!(
1389 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001390 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001391 instance, inner,
1392 );
1393 writeln!(
1394 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001395 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001396 instance, inner,
1397 );
1398 writeln!(
1399 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001400 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001401 instance, inner,
1402 );
David Tolnay219c0792020-04-24 20:31:37 -07001403 writeln!(
1404 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001405 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001406 instance, inner,
1407 );
1408 writeln!(
1409 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001410 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001411 inner, instance,
1412 );
David Tolnay503d0192020-04-24 22:18:56 -07001413 writeln!(
1414 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001415 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001416 instance, inner,
1417 );
1418 writeln!(
1419 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001420 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001421 instance, inner,
1422 );
1423 writeln!(
1424 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001425 "::std::size_t cxxbridge1$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001426 instance,
1427 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001428 writeln!(out, "#endif // CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001429}
1430
David Tolnay8faec772020-11-02 00:18:19 -08001431fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001432 let inner = ident.to_fully_qualified();
1433 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001434
1435 writeln!(out, "template <>");
David Tolnaye5703162020-12-12 16:26:35 -08001436 writeln!(
1437 out,
1438 "{} *Box<{}>::allocation::alloc() noexcept {{",
1439 inner, inner,
1440 );
David Tolnayc4b34222020-12-12 13:06:26 -08001441 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001442 writeln!(out, "}}");
1443
1444 writeln!(out, "template <>");
David Tolnay25b3c1d2020-12-12 15:50:10 -08001445 writeln!(
1446 out,
David Tolnaye5703162020-12-12 16:26:35 -08001447 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001448 inner, inner,
1449 );
1450 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1451 writeln!(out, "}}");
1452
1453 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001454 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001455 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001456 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001457}
1458
David Tolnay75ea17c2020-12-06 21:08:34 -08001459fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001460 let inner = element.to_typename(out.types);
1461 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001462
David Tolnay12320e12020-12-09 23:09:36 -08001463 out.include.cstddef = true;
1464
Myron Ahneba35cf2020-02-05 19:41:51 +07001465 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001466 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001467 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001468 writeln!(out, "}}");
1469
1470 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001471 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001472 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001473 writeln!(out, "}}");
1474
1475 writeln!(out, "template <>");
David Tolnaybe3cbf72020-12-12 22:12:07 -08001476 writeln!(
1477 out,
1478 "::std::size_t Vec<{}>::size() const noexcept {{",
1479 inner,
1480 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001481 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001482 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001483
1484 writeln!(out, "template <>");
David Tolnaybe3cbf72020-12-12 22:12:07 -08001485 writeln!(
1486 out,
1487 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1488 inner,
1489 );
David Tolnay381d9532020-12-11 13:59:45 -08001490 writeln!(
1491 out,
1492 " return cxxbridge1$rust_vec${}$capacity(this);",
1493 instance,
1494 );
David Tolnaydc62d712020-12-11 13:51:53 -08001495 writeln!(out, "}}");
1496
1497 writeln!(out, "template <>");
David Tolnay219c0792020-04-24 20:31:37 -07001498 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001499 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001500 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001501
1502 writeln!(out, "template <>");
David Tolnayfb6b73c2020-11-10 14:32:16 -08001503 writeln!(
1504 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001505 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001506 inner,
1507 );
1508 writeln!(
1509 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001510 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001511 instance,
1512 );
1513 writeln!(out, "}}");
1514
1515 writeln!(out, "template <>");
David Tolnaybe3cbf72020-12-12 22:12:07 -08001516 writeln!(
1517 out,
1518 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1519 inner,
1520 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001521 writeln!(
1522 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001523 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001524 instance,
1525 );
1526 writeln!(out, "}}");
1527
1528 writeln!(out, "template <>");
David Tolnaybe3cbf72020-12-12 22:12:07 -08001529 writeln!(out, "::std::size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001530 writeln!(out, " return cxxbridge1$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001531 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001532}
1533
David Tolnay75ea17c2020-12-06 21:08:34 -08001534fn write_unique_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001535 let ty = UniquePtr::Ident(ident);
1536 let instance = ty.to_mangled(out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001537
David Tolnay0f0162f2020-11-16 23:43:37 -08001538 writeln!(out, "#ifndef CXXBRIDGE1_UNIQUE_PTR_{}", instance);
1539 writeln!(out, "#define CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001540
David Tolnay1bdb4712020-11-25 07:27:54 -08001541 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001542
David Tolnay0f0162f2020-11-16 23:43:37 -08001543 writeln!(out, "#endif // CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001544}
1545
1546// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001547fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001548 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001549 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001550 let inner = ty.to_typename(out.types);
1551 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001552
David Tolnay63da4d32020-04-25 09:41:12 -07001553 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001554 // Some aliases are to opaque types; some are to trivial types. We can't
1555 // know at code generation time, so we generate both C++ and Rust side
1556 // bindings for a "new" method anyway. But the Rust code can't be called
1557 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001558 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001559 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001560 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001561 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001562 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001563 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001564 };
1565
David Tolnay53462762020-11-25 07:08:10 -08001566 let conditional_delete = match ty {
1567 UniquePtr::Ident(ident) => {
1568 !out.types.structs.contains_key(&ident.rust)
1569 && !out.types.enums.contains_key(&ident.rust)
1570 }
1571 UniquePtr::CxxVector(_) => false,
1572 };
1573
1574 if conditional_delete {
1575 out.builtin.is_complete = true;
1576 let definition = match ty {
1577 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1578 UniquePtr::CxxVector(_) => unreachable!(),
1579 };
1580 writeln!(
1581 out,
1582 "static_assert(::rust::is_complete<{}>::value, \"definition of {} is required\");",
1583 inner, definition,
1584 );
1585 }
David Tolnay7db73692019-10-20 14:51:12 -04001586 writeln!(
1587 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001588 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001589 inner,
1590 );
1591 writeln!(
1592 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001593 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001594 inner,
1595 );
1596 writeln!(
1597 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001598 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001599 instance, inner,
1600 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001601 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001602 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001603 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001604 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001605 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001606 out,
David Tolnay0b881402020-12-01 21:05:08 -08001607 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1608 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001609 );
David Tolnay63da4d32020-04-25 09:41:12 -07001610 writeln!(
1611 out,
David Tolnay0b881402020-12-01 21:05:08 -08001612 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1613 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001614 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001615 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001616 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001617 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001618 }
David Tolnay7db73692019-10-20 14:51:12 -04001619 writeln!(
1620 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001621 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001622 instance, inner, inner,
1623 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001624 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001625 writeln!(out, "}}");
1626 writeln!(
1627 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001628 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001629 inner, instance, inner,
1630 );
1631 writeln!(out, " return ptr.get();");
1632 writeln!(out, "}}");
1633 writeln!(
1634 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001635 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001636 inner, instance, inner,
1637 );
1638 writeln!(out, " return ptr.release();");
1639 writeln!(out, "}}");
1640 writeln!(
1641 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001642 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001643 instance, inner,
1644 );
David Tolnay53462762020-11-25 07:08:10 -08001645 if conditional_delete {
1646 out.builtin.deleter_if = true;
1647 writeln!(
1648 out,
1649 " ::rust::deleter_if<::rust::is_complete<{}>::value>{{}}(ptr);",
1650 inner,
1651 );
1652 } else {
1653 writeln!(out, " ptr->~unique_ptr();");
1654 }
David Tolnay7db73692019-10-20 14:51:12 -04001655 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001656}
Myron Ahneba35cf2020-02-05 19:41:51 +07001657
David Tolnay75ea17c2020-12-06 21:08:34 -08001658fn write_shared_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnayb3b24a12020-12-01 15:27:43 -08001659 let resolved = out.types.resolve(ident);
1660 let inner = resolved.to_fully_qualified();
1661 let instance = ident.to_symbol(out.types);
1662
1663 writeln!(out, "#ifndef CXXBRIDGE1_SHARED_PTR_{}", instance);
1664 writeln!(out, "#define CXXBRIDGE1_SHARED_PTR_{}", instance);
1665 out.include.new = true;
1666 out.include.utility = true;
1667
1668 // Some aliases are to opaque types; some are to trivial types. We can't
1669 // know at code generation time, so we generate both C++ and Rust side
1670 // bindings for a "new" method anyway. But the Rust code can't be called for
1671 // Opaque types because the 'new' method is not implemented.
1672 let can_construct_from_value = out.types.structs.contains_key(&ident.rust)
1673 || out.types.enums.contains_key(&ident.rust)
1674 || out.types.aliases.contains_key(&ident.rust);
1675
David Tolnayb3b24a12020-12-01 15:27:43 -08001676 writeln!(
1677 out,
1678 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1679 inner,
1680 );
1681 writeln!(
1682 out,
1683 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1684 inner,
1685 );
1686 writeln!(
1687 out,
1688 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1689 instance, inner,
1690 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001691 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001692 writeln!(out, "}}");
1693 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001694 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001695 writeln!(
1696 out,
David Tolnay0b881402020-12-01 21:05:08 -08001697 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1698 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001699 );
1700 writeln!(
1701 out,
David Tolnay0b881402020-12-01 21:05:08 -08001702 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1703 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001704 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001705 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001706 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001707 writeln!(out, "}}");
1708 }
1709 writeln!(
1710 out,
1711 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1712 instance, inner, inner,
1713 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001714 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001715 writeln!(out, "}}");
1716 writeln!(
1717 out,
1718 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1719 inner, instance, inner,
1720 );
1721 writeln!(out, " return self.get();");
1722 writeln!(out, "}}");
1723 writeln!(
1724 out,
1725 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1726 instance, inner,
1727 );
1728 writeln!(out, " self->~shared_ptr();");
1729 writeln!(out, "}}");
1730
1731 writeln!(out, "#endif // CXXBRIDGE1_SHARED_PTR_{}", instance);
1732}
1733
David Tolnay75ea17c2020-12-06 21:08:34 -08001734fn write_cxx_vector(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001735 let inner = element.to_typename(out.types);
1736 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001737
David Tolnay12320e12020-12-09 23:09:36 -08001738 out.include.cstddef = true;
1739
David Tolnay0f0162f2020-11-16 23:43:37 -08001740 writeln!(out, "#ifndef CXXBRIDGE1_VECTOR_{}", instance);
1741 writeln!(out, "#define CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001742 writeln!(
1743 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001744 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001745 instance, inner,
1746 );
1747 writeln!(out, " return s.size();");
1748 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001749 writeln!(
1750 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001751 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001752 inner, instance, inner,
1753 );
David Tolnay767e00d2020-12-21 17:12:27 -08001754 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001755 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001756
David Tolnay70820672020-12-07 11:15:14 -08001757 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001758 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
David Tolnay63da4d32020-04-25 09:41:12 -07001759
David Tolnay0f0162f2020-11-16 23:43:37 -08001760 writeln!(out, "#endif // CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001761}