blob: fc6801c29d6b158d05cff7d1237d289e8e27e8c8 [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 Tolnay358bc4b2020-12-26 22:37:57 -0800135 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800136 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
137 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
138 _ => {}
139 }
David Tolnay7db73692019-10-20 14:51:12 -0400140 }
David Tolnay7da38202020-11-27 17:36:16 -0800141
142 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400143 }
144
145 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800146 match api {
147 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800148 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800149 Api::RustFunction(efn) => {
150 out.next_section();
151 write_rust_function_shim(out, efn);
152 }
153 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400154 }
155 }
David Tolnay7db73692019-10-20 14:51:12 -0400156}
157
David Tolnay7da38202020-11-27 17:36:16 -0800158fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
159 out.set_namespace(Default::default());
160 out.begin_block(Block::Namespace("std"));
161
162 for api in apis {
163 if let Api::Struct(strct) = api {
164 if derive::contains(&strct.derives, Trait::Hash) {
165 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800166 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800167 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800168 let qualified = strct.name.to_fully_qualified();
169 writeln!(out, "template <> struct hash<{}> {{", qualified);
170 writeln!(
171 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800172 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800173 qualified,
174 );
175 let link_name = mangle::operator(&strct.name, "hash");
176 write!(out, " return ::");
177 for name in &strct.name.namespace {
178 write!(out, "{}::", name);
179 }
180 writeln!(out, "{}(self);", link_name);
181 writeln!(out, " }}");
182 writeln!(out, "}};");
183 }
184 }
185 }
186
187 out.end_block(Block::Namespace("std"));
188}
189
David Tolnaydfb82d72020-11-02 00:10:10 -0800190fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
191 for api in apis {
192 if let Api::Include(include) = api {
193 out.include.insert(include);
194 }
195 }
196
David Tolnaya7c2ea12020-10-30 21:32:53 -0700197 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400198 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700199 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700200 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
201 | Some(I64) => out.include.cstdint = true,
202 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800203 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700204 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800205 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800206 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700207 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800208 Type::RustBox(_) => out.builtin.rust_box = true,
209 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700210 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay215e77f2020-12-28 17:09:48 -0800211 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800212 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700213 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800214 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800215 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800216 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800217 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400218 }
219 }
David Tolnayec66d112020-10-31 21:00:26 -0700220}
David Tolnayf51447e2020-03-06 14:14:27 -0800221
David Tolnay102c7ea2020-11-08 18:58:09 -0800222fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800223 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800224 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800225
David Tolnay17a934c2020-11-02 00:40:04 -0800226 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800227 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700228 writeln!(out, "#ifndef {}", guard);
229 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400230 for line in strct.doc.to_string().lines() {
231 writeln!(out, "//{}", line);
232 }
David Tolnay17a934c2020-11-02 00:40:04 -0800233 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800234
David Tolnay7db73692019-10-20 14:51:12 -0400235 for field in &strct.fields {
236 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700237 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400238 writeln!(out, "{};", field.ident);
239 }
David Tolnay84389352020-11-27 17:12:10 -0800240
David Tolnay5554ebd2020-12-10 11:34:41 -0800241 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800242
David Tolnay102c7ea2020-11-08 18:58:09 -0800243 for method in methods {
244 write!(out, " ");
245 let sig = &method.sig;
246 let local_name = method.name.cxx.to_string();
247 write_rust_function_shim_decl(out, &local_name, sig, false);
248 writeln!(out, ";");
249 }
David Tolnay84389352020-11-27 17:12:10 -0800250
David Tolnayb960ed22020-11-27 14:34:30 -0800251 if operator_eq {
252 writeln!(
253 out,
254 " bool operator==(const {} &) const noexcept;",
255 strct.name.cxx,
256 );
257 writeln!(
258 out,
259 " bool operator!=(const {} &) const noexcept;",
260 strct.name.cxx,
261 );
262 }
David Tolnay84389352020-11-27 17:12:10 -0800263
264 if operator_ord {
265 writeln!(
266 out,
267 " bool operator<(const {} &) const noexcept;",
268 strct.name.cxx,
269 );
270 writeln!(
271 out,
272 " bool operator<=(const {} &) const noexcept;",
273 strct.name.cxx,
274 );
275 writeln!(
276 out,
277 " bool operator>(const {} &) const noexcept;",
278 strct.name.cxx,
279 );
280 writeln!(
281 out,
282 " bool operator>=(const {} &) const noexcept;",
283 strct.name.cxx,
284 );
285 }
286
David Tolnay5554ebd2020-12-10 11:34:41 -0800287 out.include.type_traits = true;
288 writeln!(out, " using IsRelocatable = ::std::true_type;");
289
David Tolnay7db73692019-10-20 14:51:12 -0400290 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700291 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400292}
293
294fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
295 writeln!(out, "struct {};", ident);
296}
297
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800298fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800299 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800300 write_atom(out, enm.repr);
301 writeln!(out, ";");
302}
303
David Tolnay8faec772020-11-02 00:18:19 -0800304fn write_struct_using(out: &mut OutFile, ident: &Pair) {
305 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800306}
307
David Tolnay8d067de2020-12-26 16:18:23 -0800308fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800309 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800310 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700311 writeln!(out, "#ifndef {}", guard);
312 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700313 for line in ety.doc.to_string().lines() {
314 writeln!(out, "//{}", line);
315 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800316
David Tolnay365fc7c2020-11-25 16:08:13 -0800317 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800318 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800319 out,
320 "struct {} final : public ::rust::Opaque {{",
321 ety.name.cxx,
322 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800323
Joel Galenson968738f2020-04-15 14:19:33 -0700324 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800325 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700326 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800327 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700328 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800329 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800330 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800331
David Tolnay8d067de2020-12-26 16:18:23 -0800332 if !methods.is_empty() {
333 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700334 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800335
David Tolnayee6ecfc2020-12-26 21:54:37 -0800336 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800337 out.include.cstddef = true;
338 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800339 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800340 writeln!(out, " struct layout {{");
341 writeln!(out, " static ::std::size_t size() noexcept;");
342 writeln!(out, " static ::std::size_t align() noexcept;");
343 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700344 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700345 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700346}
347
David Tolnay0b9b9f82020-11-01 20:41:00 -0800348fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800349 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800350 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700351 writeln!(out, "#ifndef {}", guard);
352 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700353 for line in enm.doc.to_string().lines() {
354 writeln!(out, "//{}", line);
355 }
David Tolnay17a934c2020-11-02 00:40:04 -0800356 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700357 write_atom(out, enm.repr);
358 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700359 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800360 for line in variant.doc.to_string().lines() {
361 writeln!(out, " //{}", line);
362 }
David Tolnaye6f62142020-12-21 16:00:41 -0800363 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700364 }
365 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700366 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700367}
368
David Tolnay0b9b9f82020-11-01 20:41:00 -0800369fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800370 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800371 out.include.type_traits = true;
372 writeln!(
373 out,
374 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
375 enm.name.cxx,
376 );
David Tolnay17a934c2020-11-02 00:40:04 -0800377 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700378 write_atom(out, enm.repr);
379 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700380 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700381 write!(out, "static_assert(static_cast<");
382 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700383 writeln!(
384 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700385 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800386 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700387 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700388 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700389}
390
David Tolnay5b1d8632020-12-20 22:05:11 -0800391fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800392 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700393 // necessary for soundness. That's because triviality is always declared by
394 // the user in the form of an unsafe impl of cxx::ExternType:
395 //
396 // unsafe impl ExternType for MyType {
397 // type Id = cxx::type_id!("...");
398 // type Kind = cxx::kind::Trivial;
399 // }
400 //
401 // Since the user went on the record with their unsafe impl to unsafely
402 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800403 // them if that were wrong. However, in practice correctly reasoning about
404 // the relocatability of C++ types is challenging, particularly if the type
405 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700406 //
David Tolnay174bd952020-11-02 09:23:12 -0800407 // There may be legitimate reasons to opt out of this assertion for support
408 // of types that the programmer knows are soundly Rust-movable despite not
409 // being recognized as such by the C++ type system due to a move constructor
410 // or destructor. To opt out of the relocatability check, they need to do
411 // one of the following things in any header used by `include!` in their
412 // bridge.
413 //
414 // --- if they define the type:
415 // struct MyType {
416 // ...
417 // + using IsRelocatable = std::true_type;
418 // };
419 //
420 // --- otherwise:
421 // + template <>
422 // + struct rust::IsRelocatable<MyType> : std::true_type {};
423 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700424
David Tolnay5b1d8632020-12-20 22:05:11 -0800425 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800426 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800427 writeln!(out, "static_assert(");
428 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
429 writeln!(
430 out,
431 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
432 id.trim_start_matches("::"),
433 trivial::as_what(&alias.name, reasons),
434 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700435}
436
David Tolnayb960ed22020-11-27 14:34:30 -0800437fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
438 out.set_namespace(&strct.name.namespace);
439 out.begin_block(Block::ExternC);
440
441 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800442 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800443 writeln!(
444 out,
445 "bool {}(const {1} &, const {1} &) noexcept;",
446 link_name, strct.name.cxx,
447 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800448
449 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800450 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800451 writeln!(
452 out,
453 "bool {}(const {1} &, const {1} &) noexcept;",
454 link_name, strct.name.cxx,
455 );
456 }
David Tolnayb960ed22020-11-27 14:34:30 -0800457 }
458
David Tolnay84389352020-11-27 17:12:10 -0800459 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800460 let link_name = mangle::operator(&strct.name, "lt");
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
David Tolnaya05f9402020-11-27 17:44:05 -0800467 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800468 writeln!(
469 out,
470 "bool {}(const {1} &, const {1} &) noexcept;",
471 link_name, strct.name.cxx,
472 );
473
474 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800475 let link_name = mangle::operator(&strct.name, "gt");
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
David Tolnaya05f9402020-11-27 17:44:05 -0800482 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800483 writeln!(
484 out,
485 "bool {}(const {1} &, const {1} &) noexcept;",
486 link_name, strct.name.cxx,
487 );
488 }
489 }
490
David Tolnay7da38202020-11-27 17:36:16 -0800491 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800492 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800493 let link_name = mangle::operator(&strct.name, "hash");
494 writeln!(
495 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800496 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800497 link_name, strct.name.cxx,
498 );
499 }
500
David Tolnayb960ed22020-11-27 14:34:30 -0800501 out.end_block(Block::ExternC);
502}
503
504fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
505 if out.header {
506 return;
507 }
508
509 out.set_namespace(&strct.name.namespace);
510
511 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800512 out.next_section();
513 writeln!(
514 out,
515 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
516 strct.name.cxx,
517 );
David Tolnaya05f9402020-11-27 17:44:05 -0800518 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800519 writeln!(out, " return {}(*this, rhs);", link_name);
520 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800521
David Tolnayb960ed22020-11-27 14:34:30 -0800522 out.next_section();
523 writeln!(
524 out,
525 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
526 strct.name.cxx,
527 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800528 if derive::contains(&strct.derives, Trait::Eq) {
529 writeln!(out, " return !(*this == rhs);");
530 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800531 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800532 writeln!(out, " return {}(*this, rhs);", link_name);
533 }
David Tolnayb960ed22020-11-27 14:34:30 -0800534 writeln!(out, "}}");
535 }
David Tolnay84389352020-11-27 17:12:10 -0800536
537 if derive::contains(&strct.derives, Trait::PartialOrd) {
538 out.next_section();
539 writeln!(
540 out,
541 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
542 strct.name.cxx,
543 );
David Tolnaya05f9402020-11-27 17:44:05 -0800544 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800545 writeln!(out, " return {}(*this, rhs);", link_name);
546 writeln!(out, "}}");
547
548 out.next_section();
549 writeln!(
550 out,
551 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
552 strct.name.cxx,
553 );
David Tolnaya05f9402020-11-27 17:44:05 -0800554 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800555 writeln!(out, " return {}(*this, rhs);", link_name);
556 writeln!(out, "}}");
557
558 out.next_section();
559 writeln!(
560 out,
561 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
562 strct.name.cxx,
563 );
564 if derive::contains(&strct.derives, Trait::Ord) {
565 writeln!(out, " return !(*this <= rhs);");
566 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800567 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800568 writeln!(out, " return {}(*this, rhs);", link_name);
569 }
570 writeln!(out, "}}");
571
572 out.next_section();
573 writeln!(
574 out,
575 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
576 strct.name.cxx,
577 );
578 if derive::contains(&strct.derives, Trait::Ord) {
579 writeln!(out, " return !(*this < rhs);");
580 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800581 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800582 writeln!(out, " return {}(*this, rhs);", link_name);
583 }
584 writeln!(out, "}}");
585 }
David Tolnayb960ed22020-11-27 14:34:30 -0800586}
587
David Tolnay358bc4b2020-12-26 22:37:57 -0800588fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
589 out.set_namespace(&ety.name.namespace);
590 out.begin_block(Block::ExternC);
591
592 let link_name = mangle::operator(&ety.name, "sizeof");
593 writeln!(out, "::std::size_t {}() noexcept;", link_name);
594
595 let link_name = mangle::operator(&ety.name, "alignof");
596 writeln!(out, "::std::size_t {}() noexcept;", link_name);
597
598 out.end_block(Block::ExternC);
599}
600
601fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
602 if out.header {
603 return;
604 }
605
606 out.set_namespace(&ety.name.namespace);
607
608 out.next_section();
609 let link_name = mangle::operator(&ety.name, "sizeof");
610 writeln!(
611 out,
612 "::std::size_t {}::layout::size() noexcept {{",
613 ety.name.cxx,
614 );
615 writeln!(out, " return {}();", link_name);
616 writeln!(out, "}}");
617
618 out.next_section();
619 let link_name = mangle::operator(&ety.name, "alignof");
620 writeln!(
621 out,
622 "::std::size_t {}::layout::align() noexcept {{",
623 ety.name.cxx,
624 );
625 writeln!(out, " return {}();", link_name);
626 writeln!(out, "}}");
627}
628
David Tolnay1eadd262020-12-29 16:42:08 -0800629fn begin_function_definition(out: &mut OutFile) {
630 if let Some(annotation) = &out.opt.cxx_impl_annotations {
631 write!(out, "{} ", annotation);
632 }
633}
634
David Tolnay0b9b9f82020-11-01 20:41:00 -0800635fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800636 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800637 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800638 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800639 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700640 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700641 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700642 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700643 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700644 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700645 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700646 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700647 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700648 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800649 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700650 write!(out, "const ");
651 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700652 write!(
653 out,
654 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800655 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700656 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700657 }
David Tolnay7db73692019-10-20 14:51:12 -0400658 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700659 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400660 write!(out, ", ");
661 }
David Tolnaya46a2372020-03-06 10:03:48 -0800662 if arg.ty == RustString {
663 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700664 } else if let Type::RustVec(_) = arg.ty {
665 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800666 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700667 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400668 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700669 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400670 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700671 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400672 write!(out, ", ");
673 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700674 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400675 write!(out, "*return$");
676 }
677 writeln!(out, ") noexcept {{");
678 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700679 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700680 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800681 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700682 Some(receiver) => write!(
683 out,
684 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700685 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800686 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700687 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700688 }
David Tolnay7db73692019-10-20 14:51:12 -0400689 for (i, arg) in efn.args.iter().enumerate() {
690 if i > 0 {
691 write!(out, ", ");
692 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700693 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400694 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700695 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700696 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800697 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700698 write!(out, " const");
699 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700700 }
701 write!(out, " = ");
702 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800703 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700704 Some(receiver) => write!(
705 out,
706 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700707 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800708 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700709 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700710 }
711 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400712 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700713 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700714 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700715 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700716 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700717 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700718 writeln!(out, " [&] {{");
719 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700720 }
David Tolnay7db73692019-10-20 14:51:12 -0400721 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700722 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400723 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700724 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400725 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700726 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400727 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700728 }
729 match &efn.ret {
730 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700731 Some(Type::Str(_)) if !indirect_return => {
732 out.builtin.rust_str_repr = true;
733 write!(out, "::rust::impl<::rust::Str>::repr(");
734 }
David Tolnay73b72642020-11-25 17:44:05 -0800735 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700736 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800737 write!(out, "::rust::impl<");
738 write_type(out, ty);
739 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700740 }
David Tolnay99642622020-03-25 13:07:35 -0700741 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400742 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700743 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800744 None => write!(out, "{}$(", efn.name.rust),
745 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700746 }
David Tolnay7db73692019-10-20 14:51:12 -0400747 for (i, arg) in efn.args.iter().enumerate() {
748 if i > 0 {
749 write!(out, ", ");
750 }
751 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700752 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400753 write!(out, "::from_raw({})", arg.ident);
754 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700755 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400756 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700757 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700758 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700759 write!(
760 out,
761 "::rust::impl<::rust::Str>::new_unchecked({})",
762 arg.ident,
763 );
David Tolnaya46a2372020-03-06 10:03:48 -0800764 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700765 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800766 write!(
767 out,
768 "::rust::String(::rust::unsafe_bitcopy, *{})",
769 arg.ident,
770 );
David Tolnay313b10e2020-04-25 16:30:51 -0700771 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700772 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700773 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700774 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800775 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800776 write_type(out, &arg.ty);
777 write!(out, "(static_cast<");
778 if slice.mutability.is_none() {
779 write!(out, "const ");
780 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800781 write_type_space(out, &slice.inner);
782 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700783 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700784 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800785 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400786 } else {
787 write!(out, "{}", arg.ident);
788 }
789 }
790 write!(out, ")");
791 match &efn.ret {
792 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
793 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800794 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400795 _ => {}
796 }
797 if indirect_return {
798 write!(out, ")");
799 }
800 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700801 if efn.throws {
802 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800803 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700804 writeln!(out, " throw$.ptr = nullptr;");
805 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700806 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700807 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700808 writeln!(
809 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800810 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700811 );
David Tolnay5d121442020-03-17 22:14:40 -0700812 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700813 writeln!(out, " return throw$;");
814 }
David Tolnay7db73692019-10-20 14:51:12 -0400815 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700816 for arg in &efn.args {
817 if let Type::Fn(f) = &arg.ty {
818 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700819 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700820 }
821 }
David Tolnayca563ee2020-11-01 20:12:27 -0800822 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700823}
824
825fn write_function_pointer_trampoline(
826 out: &mut OutFile,
827 efn: &ExternFn,
828 var: &Ident,
829 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700830) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700831 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700832 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700833 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700834
835 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700836 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
837 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400838}
839
David Tolnay0b9b9f82020-11-01 20:41:00 -0800840fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800841 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800842 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700843 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700844 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700845 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800846 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700847}
848
849fn write_rust_function_decl_impl(
850 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700851 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700852 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 indirect_call: bool,
854) {
David Tolnay04770742020-11-01 13:50:50 -0800855 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700856 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700857 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700858 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700859 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700860 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700861 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700862 write!(out, "{}(", link_name);
863 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700864 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800865 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700866 write!(out, "const ");
867 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700868 write!(
869 out,
870 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800871 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700872 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700873 needs_comma = true;
874 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700875 for arg in &sig.args {
876 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400877 write!(out, ", ");
878 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700879 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700880 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400881 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700882 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700883 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400884 write!(out, ", ");
885 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700886 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400887 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700888 needs_comma = true;
889 }
890 if indirect_call {
891 if needs_comma {
892 write!(out, ", ");
893 }
894 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400895 }
896 writeln!(out, ") noexcept;");
897}
898
David Tolnay0b9b9f82020-11-01 20:41:00 -0800899fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800900 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400901 for line in efn.doc.to_string().lines() {
902 writeln!(out, "//{}", line);
903 }
David Tolnaya73853b2020-04-20 01:19:56 -0700904 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800905 None => efn.name.cxx.to_string(),
906 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700907 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700908 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700909 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700910 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700911}
912
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700913fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700914 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700915 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700916 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700917 indirect_call: bool,
918) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700919 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700920 write!(out, "{}(", local_name);
921 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400922 if i > 0 {
923 write!(out, ", ");
924 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700925 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400926 write!(out, "{}", arg.ident);
927 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700928 if indirect_call {
929 if !sig.args.is_empty() {
930 write!(out, ", ");
931 }
932 write!(out, "void *extern$");
933 }
David Tolnay1e548172020-03-16 13:37:09 -0700934 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700935 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800936 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700937 write!(out, " const");
938 }
939 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700940 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700941 write!(out, " noexcept");
942 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700943}
944
945fn write_rust_function_shim_impl(
946 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700947 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700948 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700949 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700950 indirect_call: bool,
951) {
952 if out.header && sig.receiver.is_some() {
953 // We've already defined this inside the struct.
954 return;
955 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800956 if !out.header {
957 begin_function_definition(out);
958 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700959 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400960 if out.header {
961 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700962 return;
David Tolnay7db73692019-10-20 14:51:12 -0400963 }
David Tolnay439cde22020-04-20 00:46:25 -0700964 writeln!(out, " {{");
965 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700966 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700967 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700968 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700969 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700970 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700971 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
972 }
973 }
974 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700975 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700976 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700977 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700978 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700979 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700980 writeln!(out, "> return$;");
981 write!(out, " ");
982 } else if let Some(ret) = &sig.ret {
983 write!(out, "return ");
984 match ret {
985 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700986 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700987 write!(out, "::from_raw(");
988 }
989 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700990 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700991 write!(out, "(");
992 }
993 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700994 Type::Str(_) => {
995 out.builtin.rust_str_new_unchecked = true;
996 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
997 }
David Tolnay73b72642020-11-25 17:44:05 -0800998 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700999 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001000 write!(out, "::rust::impl<");
1001 write_type(out, ret);
1002 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001003 }
David Tolnay439cde22020-04-20 00:46:25 -07001004 _ => {}
1005 }
1006 }
1007 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -07001008 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001009 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001010 }
1011 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001012 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001013 if sig.receiver.is_some() {
1014 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001015 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001016 }
David Tolnay9d840362020-11-10 08:50:40 -08001017 for arg in &sig.args {
1018 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001019 write!(out, ", ");
1020 }
1021 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -07001022 Type::Str(_) => {
1023 out.builtin.rust_str_repr = true;
1024 write!(out, "::rust::impl<::rust::Str>::repr(");
1025 }
David Tolnay73b72642020-11-25 17:44:05 -08001026 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001027 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001028 write!(out, "::rust::impl<");
1029 write_type(out, &arg.ty);
1030 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001031 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001032 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -07001033 _ => {}
1034 }
1035 write!(out, "{}", arg.ident);
1036 match &arg.ty {
1037 Type::RustBox(_) => write!(out, ".into_raw()"),
1038 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -08001039 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001040 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001041 _ => {}
1042 }
David Tolnay9d840362020-11-10 08:50:40 -08001043 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001044 }
1045 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001046 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001047 write!(out, ", ");
1048 }
1049 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001050 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001051 }
1052 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001053 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001054 write!(out, ", ");
1055 }
1056 write!(out, "extern$");
1057 }
1058 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001059 if !indirect_return {
1060 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -08001061 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001062 write!(out, ")");
1063 }
David Tolnay439cde22020-04-20 00:46:25 -07001064 }
1065 }
1066 writeln!(out, ";");
1067 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001068 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001069 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001070 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001071 writeln!(out, " }}");
1072 }
1073 if indirect_return {
1074 out.include.utility = true;
1075 writeln!(out, " return ::std::move(return$.value);");
1076 }
1077 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001078}
1079
David Tolnaya7c2ea12020-10-30 21:32:53 -07001080fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001081 match ty {
1082 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001083 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001084 }
1085}
1086
David Tolnay75dca2e2020-03-25 20:17:52 -07001087fn indirect_return(sig: &Signature, types: &Types) -> bool {
1088 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001089 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001090 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001091}
1092
David Tolnaya7c2ea12020-10-30 21:32:53 -07001093fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001094 match ty {
1095 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001096 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001097 write!(out, "*");
1098 }
1099 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001100 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001101 write!(out, "const ");
1102 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001103 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001104 write!(out, " *");
1105 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001106 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001107 }
1108}
1109
David Tolnaya7c2ea12020-10-30 21:32:53 -07001110fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1111 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001112 match ty {
1113 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001114 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001115 _ => write_space_after_type(out, ty),
1116 }
1117}
1118
David Tolnaya7c2ea12020-10-30 21:32:53 -07001119fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001120 match ty {
1121 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001122 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001123 write!(out, "*");
1124 }
David Tolnay4a441222020-01-25 16:24:27 -08001125 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001126 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001127 write!(out, "const ");
1128 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001129 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001130 write!(out, " *");
1131 }
David Tolnay73b72642020-11-25 17:44:05 -08001132 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001133 out.builtin.ptr_len = true;
1134 write!(out, "::rust::repr::PtrLen ");
1135 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001136 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1137 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001138 }
1139}
1140
David Tolnaya7c2ea12020-10-30 21:32:53 -07001141fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001142 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001143 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001144 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001145 write!(out, "*");
1146 }
David Tolnay73b72642020-11-25 17:44:05 -08001147 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001148 out.builtin.ptr_len = true;
1149 write!(out, "::rust::repr::PtrLen ");
1150 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001151 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001152 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001153 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001154 write!(out, "*");
1155 }
1156 write!(out, "{}", arg.ident);
1157}
1158
David Tolnaya7c2ea12020-10-30 21:32:53 -07001159fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001160 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001161 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001162 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001163 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001164 },
1165 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001166 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001167 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001168 write!(out, ">");
1169 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001170 Type::RustVec(ty) => {
1171 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001172 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001173 write!(out, ">");
1174 }
David Tolnay7db73692019-10-20 14:51:12 -04001175 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001176 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001177 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001178 write!(out, ">");
1179 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001180 Type::SharedPtr(ptr) => {
1181 write!(out, "::std::shared_ptr<");
1182 write_type(out, &ptr.inner);
1183 write!(out, ">");
1184 }
David Tolnay215e77f2020-12-28 17:09:48 -08001185 Type::WeakPtr(ptr) => {
1186 write!(out, "::std::weak_ptr<");
1187 write_type(out, &ptr.inner);
1188 write!(out, ">");
1189 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001190 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001191 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001192 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001193 write!(out, ">");
1194 }
David Tolnay7db73692019-10-20 14:51:12 -04001195 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001196 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001197 write!(out, "const ");
1198 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001199 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001200 write!(out, " &");
1201 }
1202 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001203 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001204 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001205 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001206 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001207 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001208 write!(out, "const ");
1209 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001210 write_type(out, &slice.inner);
1211 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001212 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001213 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001214 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001215 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001216 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001217 None => write!(out, "void"),
1218 }
1219 write!(out, "(");
1220 for (i, arg) in f.args.iter().enumerate() {
1221 if i > 0 {
1222 write!(out, ", ");
1223 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001224 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001225 }
1226 write!(out, ")>");
1227 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001228 Type::Array(a) => {
1229 write!(out, "::std::array<");
1230 write_type(out, &a.inner);
1231 write!(out, ", {}>", &a.len);
1232 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001233 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001234 }
1235}
1236
David Tolnayf6a89f22020-05-10 23:39:27 -07001237fn write_atom(out: &mut OutFile, atom: Atom) {
1238 match atom {
1239 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001240 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001241 U8 => write!(out, "::std::uint8_t"),
1242 U16 => write!(out, "::std::uint16_t"),
1243 U32 => write!(out, "::std::uint32_t"),
1244 U64 => write!(out, "::std::uint64_t"),
1245 Usize => write!(out, "::std::size_t"),
1246 I8 => write!(out, "::std::int8_t"),
1247 I16 => write!(out, "::std::int16_t"),
1248 I32 => write!(out, "::std::int32_t"),
1249 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001250 Isize => write!(out, "::rust::isize"),
1251 F32 => write!(out, "float"),
1252 F64 => write!(out, "double"),
1253 CxxString => write!(out, "::std::string"),
1254 RustString => write!(out, "::rust::String"),
1255 }
1256}
1257
David Tolnaya7c2ea12020-10-30 21:32:53 -07001258fn write_type_space(out: &mut OutFile, ty: &Type) {
1259 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001260 write_space_after_type(out, ty);
1261}
1262
1263fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001264 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001265 Type::Ident(_)
1266 | Type::RustBox(_)
1267 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001268 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001269 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001270 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001271 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001272 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001273 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001274 | Type::Fn(_)
1275 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001276 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001277 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001278 }
1279}
1280
David Tolnay1bdb4712020-11-25 07:27:54 -08001281#[derive(Copy, Clone)]
1282enum UniquePtr<'a> {
David Tolnay75ea17c2020-12-06 21:08:34 -08001283 Ident(&'a RustName),
1284 CxxVector(&'a RustName),
David Tolnay1bdb4712020-11-25 07:27:54 -08001285}
1286
1287trait ToTypename {
1288 fn to_typename(&self, types: &Types) -> String;
1289}
1290
David Tolnay75ea17c2020-12-06 21:08:34 -08001291impl ToTypename for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001292 fn to_typename(&self, types: &Types) -> String {
1293 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001294 }
1295}
1296
David Tolnay1bdb4712020-11-25 07:27:54 -08001297impl<'a> ToTypename for UniquePtr<'a> {
1298 fn to_typename(&self, types: &Types) -> String {
1299 match self {
1300 UniquePtr::Ident(ident) => ident.to_typename(types),
1301 UniquePtr::CxxVector(element) => {
1302 format!("::std::vector<{}>", element.to_typename(types))
1303 }
1304 }
1305 }
1306}
1307
1308trait ToMangled {
1309 fn to_mangled(&self, types: &Types) -> Symbol;
1310}
1311
David Tolnay75ea17c2020-12-06 21:08:34 -08001312impl ToMangled for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001313 fn to_mangled(&self, types: &Types) -> Symbol {
1314 self.to_symbol(types)
1315 }
1316}
1317
1318impl<'a> ToMangled for UniquePtr<'a> {
1319 fn to_mangled(&self, types: &Types) -> Symbol {
1320 match self {
1321 UniquePtr::Ident(ident) => ident.to_mangled(types),
1322 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1323 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001324 }
1325}
1326
David Tolnaya7c2ea12020-10-30 21:32:53 -07001327fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001328 if out.header {
1329 return;
1330 }
1331
1332 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001333 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001334 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001335 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001336 if let Type::RustBox(ptr) = ty {
1337 if let Type::Ident(inner) = &ptr.inner {
1338 if Atom::from(&inner.rust).is_none()
1339 && (!out.types.aliases.contains_key(&inner.rust)
1340 || out.types.explicit_impls.contains(ty))
1341 {
1342 out.next_section();
1343 write_rust_box_extern(out, &out.types.resolve(&inner));
1344 }
David Tolnay7db73692019-10-20 14:51:12 -04001345 }
David Tolnayf33bc242020-12-04 18:27:02 -08001346 } else if let Type::RustVec(vec) = ty {
1347 if let Type::Ident(inner) = &vec.inner {
1348 if Atom::from(&inner.rust).is_none()
1349 && (!out.types.aliases.contains_key(&inner.rust)
1350 || out.types.explicit_impls.contains(ty))
1351 {
David Tolnay6787be62020-04-25 11:01:02 -07001352 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001353 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001354 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001355 }
David Tolnay7db73692019-10-20 14:51:12 -04001356 } else if let Type::UniquePtr(ptr) = ty {
1357 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001358 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001359 && (!out.types.aliases.contains_key(&inner.rust)
1360 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001361 {
David Tolnay7db73692019-10-20 14:51:12 -04001362 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001363 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001364 }
1365 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001366 } else if let Type::SharedPtr(ptr) = ty {
1367 if let Type::Ident(inner) = &ptr.inner {
1368 if Atom::from(&inner.rust).is_none()
David Tolnayb7453812020-12-01 21:46:55 -08001369 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnayb3b24a12020-12-01 15:27:43 -08001370 || out.types.explicit_impls.contains(ty))
1371 {
1372 out.next_section();
1373 write_shared_ptr(out, inner);
1374 }
1375 }
David Tolnay215e77f2020-12-28 17:09:48 -08001376 } else if let Type::WeakPtr(ptr) = ty {
1377 if let Type::Ident(inner) = &ptr.inner {
1378 if Atom::from(&inner.rust).is_none()
1379 && (!out.types.aliases.contains_key(&inner.rust)
1380 || out.types.explicit_impls.contains(ty))
1381 {
1382 out.next_section();
1383 write_weak_ptr(out, inner);
1384 }
1385 }
David Tolnayf33bc242020-12-04 18:27:02 -08001386 } else if let Type::CxxVector(vector) = ty {
1387 if let Type::Ident(inner) = &vector.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001388 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001389 && (!out.types.aliases.contains_key(&inner.rust)
1390 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001391 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001392 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001393 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001394 }
1395 }
1396 }
1397 }
David Tolnay0c033e32020-11-01 15:15:48 -08001398 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001399
David Tolnay0c033e32020-11-01 15:15:48 -08001400 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001401 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001402 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001403 if let Type::RustBox(ptr) = ty {
1404 if let Type::Ident(inner) = &ptr.inner {
1405 if Atom::from(&inner.rust).is_none()
1406 && (!out.types.aliases.contains_key(&inner.rust)
1407 || out.types.explicit_impls.contains(ty))
1408 {
1409 write_rust_box_impl(out, &out.types.resolve(&inner));
1410 }
David Tolnay7db73692019-10-20 14:51:12 -04001411 }
David Tolnayf33bc242020-12-04 18:27:02 -08001412 } else if let Type::RustVec(vec) = ty {
1413 if let Type::Ident(inner) = &vec.inner {
1414 if Atom::from(&inner.rust).is_none()
1415 && (!out.types.aliases.contains_key(&inner.rust)
1416 || out.types.explicit_impls.contains(ty))
1417 {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001418 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001419 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001420 }
David Tolnay7db73692019-10-20 14:51:12 -04001421 }
1422 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001423 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001424 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001425}
1426
David Tolnay8faec772020-11-02 00:18:19 -08001427fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001428 let inner = ident.to_fully_qualified();
1429 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001430
David Tolnay7db73692019-10-20 14:51:12 -04001431 writeln!(
1432 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001433 "{} *cxxbridge1$box${}$alloc() noexcept;",
1434 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001435 );
1436 writeln!(
1437 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001438 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1439 instance, inner,
1440 );
1441 writeln!(
1442 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001443 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001444 instance, inner,
1445 );
David Tolnay7db73692019-10-20 14:51:12 -04001446}
1447
David Tolnay75ea17c2020-12-06 21:08:34 -08001448fn write_rust_vec_extern(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001449 let inner = element.to_typename(out.types);
1450 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001451
David Tolnay12320e12020-12-09 23:09:36 -08001452 out.include.cstddef = true;
1453
Myron Ahneba35cf2020-02-05 19:41:51 +07001454 writeln!(
1455 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001456 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001457 instance, inner,
1458 );
1459 writeln!(
1460 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001461 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001462 instance, inner,
1463 );
1464 writeln!(
1465 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001466 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001467 instance, inner,
1468 );
David Tolnay219c0792020-04-24 20:31:37 -07001469 writeln!(
1470 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001471 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001472 instance, inner,
1473 );
1474 writeln!(
1475 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001476 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001477 inner, instance,
1478 );
David Tolnay503d0192020-04-24 22:18:56 -07001479 writeln!(
1480 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001481 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001482 instance, inner,
1483 );
1484 writeln!(
1485 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001486 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001487 instance, inner,
1488 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001489}
1490
David Tolnay8faec772020-11-02 00:18:19 -08001491fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001492 let inner = ident.to_fully_qualified();
1493 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001494
1495 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001496 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001497 writeln!(
1498 out,
1499 "{} *Box<{}>::allocation::alloc() noexcept {{",
1500 inner, inner,
1501 );
David Tolnayc4b34222020-12-12 13:06:26 -08001502 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001503 writeln!(out, "}}");
1504
1505 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001506 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001507 writeln!(
1508 out,
David Tolnaye5703162020-12-12 16:26:35 -08001509 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001510 inner, inner,
1511 );
1512 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1513 writeln!(out, "}}");
1514
1515 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001516 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001517 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001518 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001519 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001520}
1521
David Tolnay75ea17c2020-12-06 21:08:34 -08001522fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001523 let inner = element.to_typename(out.types);
1524 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001525
David Tolnay12320e12020-12-09 23:09:36 -08001526 out.include.cstddef = true;
1527
Myron Ahneba35cf2020-02-05 19:41:51 +07001528 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001529 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001530 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001531 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001532 writeln!(out, "}}");
1533
1534 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001535 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001536 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001537 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001538 writeln!(out, "}}");
1539
1540 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001541 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001542 writeln!(
1543 out,
1544 "::std::size_t Vec<{}>::size() const noexcept {{",
1545 inner,
1546 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001547 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001548 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001549
1550 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001551 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001552 writeln!(
1553 out,
1554 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1555 inner,
1556 );
David Tolnay381d9532020-12-11 13:59:45 -08001557 writeln!(
1558 out,
1559 " return cxxbridge1$rust_vec${}$capacity(this);",
1560 instance,
1561 );
David Tolnaydc62d712020-12-11 13:51:53 -08001562 writeln!(out, "}}");
1563
1564 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001565 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001566 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001567 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001568 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001569
1570 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001571 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001572 writeln!(
1573 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001574 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001575 inner,
1576 );
1577 writeln!(
1578 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001579 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001580 instance,
1581 );
1582 writeln!(out, "}}");
1583
1584 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001585 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001586 writeln!(
1587 out,
1588 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1589 inner,
1590 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001591 writeln!(
1592 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001593 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001594 instance,
1595 );
1596 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001597}
1598
David Tolnay75ea17c2020-12-06 21:08:34 -08001599fn write_unique_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001600 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001601 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001602}
1603
1604// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001605fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001606 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001607 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001608 let inner = ty.to_typename(out.types);
1609 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001610
David Tolnay63da4d32020-04-25 09:41:12 -07001611 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001612 // Some aliases are to opaque types; some are to trivial types. We can't
1613 // know at code generation time, so we generate both C++ and Rust side
1614 // bindings for a "new" method anyway. But the Rust code can't be called
1615 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001616 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001617 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001618 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001619 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001620 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001621 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001622 };
1623
David Tolnay53462762020-11-25 07:08:10 -08001624 let conditional_delete = match ty {
1625 UniquePtr::Ident(ident) => {
1626 !out.types.structs.contains_key(&ident.rust)
1627 && !out.types.enums.contains_key(&ident.rust)
1628 }
1629 UniquePtr::CxxVector(_) => false,
1630 };
1631
1632 if conditional_delete {
1633 out.builtin.is_complete = true;
1634 let definition = match ty {
1635 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1636 UniquePtr::CxxVector(_) => unreachable!(),
1637 };
1638 writeln!(
1639 out,
David Tolnay75068632020-12-26 22:15:17 -08001640 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001641 inner, definition,
1642 );
1643 }
David Tolnay7db73692019-10-20 14:51:12 -04001644 writeln!(
1645 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001646 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001647 inner,
1648 );
1649 writeln!(
1650 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001651 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001652 inner,
1653 );
1654 writeln!(
1655 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001656 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001657 instance, inner,
1658 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001659 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001660 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001661 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001662 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001663 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001664 out,
David Tolnay0b881402020-12-01 21:05:08 -08001665 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1666 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001667 );
David Tolnay63da4d32020-04-25 09:41:12 -07001668 writeln!(
1669 out,
David Tolnay0b881402020-12-01 21:05:08 -08001670 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1671 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001672 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001673 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001674 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001675 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001676 }
David Tolnay7db73692019-10-20 14:51:12 -04001677 writeln!(
1678 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001679 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001680 instance, inner, inner,
1681 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001682 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001683 writeln!(out, "}}");
1684 writeln!(
1685 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001686 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001687 inner, instance, inner,
1688 );
1689 writeln!(out, " return ptr.get();");
1690 writeln!(out, "}}");
1691 writeln!(
1692 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001693 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001694 inner, instance, inner,
1695 );
1696 writeln!(out, " return ptr.release();");
1697 writeln!(out, "}}");
1698 writeln!(
1699 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001700 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001701 instance, inner,
1702 );
David Tolnay53462762020-11-25 07:08:10 -08001703 if conditional_delete {
1704 out.builtin.deleter_if = true;
1705 writeln!(
1706 out,
David Tolnay75068632020-12-26 22:15:17 -08001707 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001708 inner,
1709 );
1710 } else {
1711 writeln!(out, " ptr->~unique_ptr();");
1712 }
David Tolnay7db73692019-10-20 14:51:12 -04001713 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001714}
Myron Ahneba35cf2020-02-05 19:41:51 +07001715
David Tolnay75ea17c2020-12-06 21:08:34 -08001716fn write_shared_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnayb3b24a12020-12-01 15:27:43 -08001717 let resolved = out.types.resolve(ident);
1718 let inner = resolved.to_fully_qualified();
1719 let instance = ident.to_symbol(out.types);
1720
David Tolnayb3b24a12020-12-01 15:27:43 -08001721 out.include.new = true;
1722 out.include.utility = true;
1723
1724 // Some aliases are to opaque types; some are to trivial types. We can't
1725 // know at code generation time, so we generate both C++ and Rust side
1726 // bindings for a "new" method anyway. But the Rust code can't be called for
1727 // Opaque types because the 'new' method is not implemented.
1728 let can_construct_from_value = out.types.structs.contains_key(&ident.rust)
1729 || out.types.enums.contains_key(&ident.rust)
1730 || out.types.aliases.contains_key(&ident.rust);
1731
David Tolnayb3b24a12020-12-01 15:27:43 -08001732 writeln!(
1733 out,
1734 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1735 inner,
1736 );
1737 writeln!(
1738 out,
1739 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1740 inner,
1741 );
1742 writeln!(
1743 out,
1744 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1745 instance, inner,
1746 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001747 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001748 writeln!(out, "}}");
1749 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001750 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001751 writeln!(
1752 out,
David Tolnay0b881402020-12-01 21:05:08 -08001753 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1754 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001755 );
1756 writeln!(
1757 out,
David Tolnay0b881402020-12-01 21:05:08 -08001758 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1759 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001760 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001761 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001762 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001763 writeln!(out, "}}");
1764 }
1765 writeln!(
1766 out,
1767 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1768 instance, inner, inner,
1769 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001770 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001771 writeln!(out, "}}");
1772 writeln!(
1773 out,
1774 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1775 inner, instance, inner,
1776 );
1777 writeln!(out, " return self.get();");
1778 writeln!(out, "}}");
1779 writeln!(
1780 out,
1781 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1782 instance, inner,
1783 );
1784 writeln!(out, " self->~shared_ptr();");
1785 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001786}
1787
David Tolnay215e77f2020-12-28 17:09:48 -08001788fn write_weak_ptr(out: &mut OutFile, ident: &RustName) {
1789 let resolved = out.types.resolve(ident);
1790 let inner = resolved.to_fully_qualified();
1791 let instance = ident.to_symbol(out.types);
1792
1793 out.include.new = true;
1794 out.include.utility = true;
1795
1796 writeln!(
1797 out,
1798 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1799 inner,
1800 );
1801 writeln!(
1802 out,
1803 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1804 inner,
1805 );
1806 writeln!(
1807 out,
1808 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1809 instance, inner,
1810 );
1811 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1812 writeln!(out, "}}");
1813 writeln!(
1814 out,
1815 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1816 instance, inner, inner,
1817 );
1818 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1819 writeln!(out, "}}");
1820 writeln!(
1821 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001822 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1823 instance, inner, inner,
1824 );
1825 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1826 writeln!(out, "}}");
1827 writeln!(
1828 out,
David Tolnay7a487852020-12-28 18:02:25 -08001829 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1830 instance, inner, inner,
1831 );
1832 writeln!(
1833 out,
1834 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1835 inner,
1836 );
1837 writeln!(out, "}}");
1838 writeln!(
1839 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001840 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1841 instance, inner,
1842 );
1843 writeln!(out, " self->~weak_ptr();");
1844 writeln!(out, "}}");
1845}
1846
David Tolnay75ea17c2020-12-06 21:08:34 -08001847fn write_cxx_vector(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001848 let inner = element.to_typename(out.types);
1849 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001850
David Tolnay12320e12020-12-09 23:09:36 -08001851 out.include.cstddef = true;
1852
Myron Ahneba35cf2020-02-05 19:41:51 +07001853 writeln!(
1854 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001855 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001856 instance, inner,
1857 );
1858 writeln!(out, " return s.size();");
1859 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001860 writeln!(
1861 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001862 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001863 inner, instance, inner,
1864 );
David Tolnay767e00d2020-12-21 17:12:27 -08001865 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001866 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001867
David Tolnay70820672020-12-07 11:15:14 -08001868 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001869 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001870}