blob: 7a910f103f4d9d530d7a0c78909a96d91a62d272 [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 Tolnaye6f62142020-12-21 16:00:41 -0800360 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700361 }
362 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700363 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700364}
365
David Tolnay0b9b9f82020-11-01 20:41:00 -0800366fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800367 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800368 out.include.type_traits = true;
369 writeln!(
370 out,
371 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
372 enm.name.cxx,
373 );
David Tolnay17a934c2020-11-02 00:40:04 -0800374 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700375 write_atom(out, enm.repr);
376 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700377 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700378 write!(out, "static_assert(static_cast<");
379 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700380 writeln!(
381 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700382 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800383 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700384 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700385 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700386}
387
David Tolnay5b1d8632020-12-20 22:05:11 -0800388fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800389 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700390 // necessary for soundness. That's because triviality is always declared by
391 // the user in the form of an unsafe impl of cxx::ExternType:
392 //
393 // unsafe impl ExternType for MyType {
394 // type Id = cxx::type_id!("...");
395 // type Kind = cxx::kind::Trivial;
396 // }
397 //
398 // Since the user went on the record with their unsafe impl to unsafely
399 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800400 // them if that were wrong. However, in practice correctly reasoning about
401 // the relocatability of C++ types is challenging, particularly if the type
402 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700403 //
David Tolnay174bd952020-11-02 09:23:12 -0800404 // There may be legitimate reasons to opt out of this assertion for support
405 // of types that the programmer knows are soundly Rust-movable despite not
406 // being recognized as such by the C++ type system due to a move constructor
407 // or destructor. To opt out of the relocatability check, they need to do
408 // one of the following things in any header used by `include!` in their
409 // bridge.
410 //
411 // --- if they define the type:
412 // struct MyType {
413 // ...
414 // + using IsRelocatable = std::true_type;
415 // };
416 //
417 // --- otherwise:
418 // + template <>
419 // + struct rust::IsRelocatable<MyType> : std::true_type {};
420 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700421
David Tolnay5b1d8632020-12-20 22:05:11 -0800422 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800423 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800424 writeln!(out, "static_assert(");
425 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
426 writeln!(
427 out,
428 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
429 id.trim_start_matches("::"),
430 trivial::as_what(&alias.name, reasons),
431 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700432}
433
David Tolnayb960ed22020-11-27 14:34:30 -0800434fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
435 out.set_namespace(&strct.name.namespace);
436 out.begin_block(Block::ExternC);
437
438 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800439 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800440 writeln!(
441 out,
442 "bool {}(const {1} &, const {1} &) noexcept;",
443 link_name, strct.name.cxx,
444 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800445
446 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800447 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800448 writeln!(
449 out,
450 "bool {}(const {1} &, const {1} &) noexcept;",
451 link_name, strct.name.cxx,
452 );
453 }
David Tolnayb960ed22020-11-27 14:34:30 -0800454 }
455
David Tolnay84389352020-11-27 17:12:10 -0800456 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800457 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800458 writeln!(
459 out,
460 "bool {}(const {1} &, const {1} &) noexcept;",
461 link_name, strct.name.cxx,
462 );
463
David Tolnaya05f9402020-11-27 17:44:05 -0800464 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800465 writeln!(
466 out,
467 "bool {}(const {1} &, const {1} &) noexcept;",
468 link_name, strct.name.cxx,
469 );
470
471 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800472 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800473 writeln!(
474 out,
475 "bool {}(const {1} &, const {1} &) noexcept;",
476 link_name, strct.name.cxx,
477 );
478
David Tolnaya05f9402020-11-27 17:44:05 -0800479 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800480 writeln!(
481 out,
482 "bool {}(const {1} &, const {1} &) noexcept;",
483 link_name, strct.name.cxx,
484 );
485 }
486 }
487
David Tolnay7da38202020-11-27 17:36:16 -0800488 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800489 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800490 let link_name = mangle::operator(&strct.name, "hash");
491 writeln!(
492 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800493 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800494 link_name, strct.name.cxx,
495 );
496 }
497
David Tolnayb960ed22020-11-27 14:34:30 -0800498 out.end_block(Block::ExternC);
499}
500
501fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
502 if out.header {
503 return;
504 }
505
506 out.set_namespace(&strct.name.namespace);
507
508 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800509 out.next_section();
510 writeln!(
511 out,
512 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
513 strct.name.cxx,
514 );
David Tolnaya05f9402020-11-27 17:44:05 -0800515 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800516 writeln!(out, " return {}(*this, rhs);", link_name);
517 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800518
David Tolnayb960ed22020-11-27 14:34:30 -0800519 out.next_section();
520 writeln!(
521 out,
522 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
523 strct.name.cxx,
524 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800525 if derive::contains(&strct.derives, Trait::Eq) {
526 writeln!(out, " return !(*this == rhs);");
527 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800528 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800529 writeln!(out, " return {}(*this, rhs);", link_name);
530 }
David Tolnayb960ed22020-11-27 14:34:30 -0800531 writeln!(out, "}}");
532 }
David Tolnay84389352020-11-27 17:12:10 -0800533
534 if derive::contains(&strct.derives, Trait::PartialOrd) {
535 out.next_section();
536 writeln!(
537 out,
538 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
539 strct.name.cxx,
540 );
David Tolnaya05f9402020-11-27 17:44:05 -0800541 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800542 writeln!(out, " return {}(*this, rhs);", link_name);
543 writeln!(out, "}}");
544
545 out.next_section();
546 writeln!(
547 out,
548 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
549 strct.name.cxx,
550 );
David Tolnaya05f9402020-11-27 17:44:05 -0800551 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800552 writeln!(out, " return {}(*this, rhs);", link_name);
553 writeln!(out, "}}");
554
555 out.next_section();
556 writeln!(
557 out,
558 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
559 strct.name.cxx,
560 );
561 if derive::contains(&strct.derives, Trait::Ord) {
562 writeln!(out, " return !(*this <= rhs);");
563 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800564 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800565 writeln!(out, " return {}(*this, rhs);", link_name);
566 }
567 writeln!(out, "}}");
568
569 out.next_section();
570 writeln!(
571 out,
572 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
573 strct.name.cxx,
574 );
575 if derive::contains(&strct.derives, Trait::Ord) {
576 writeln!(out, " return !(*this < rhs);");
577 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800578 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800579 writeln!(out, " return {}(*this, rhs);", link_name);
580 }
581 writeln!(out, "}}");
582 }
David Tolnayb960ed22020-11-27 14:34:30 -0800583}
584
David Tolnay358bc4b2020-12-26 22:37:57 -0800585fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
586 out.set_namespace(&ety.name.namespace);
587 out.begin_block(Block::ExternC);
588
589 let link_name = mangle::operator(&ety.name, "sizeof");
590 writeln!(out, "::std::size_t {}() noexcept;", link_name);
591
592 let link_name = mangle::operator(&ety.name, "alignof");
593 writeln!(out, "::std::size_t {}() noexcept;", link_name);
594
595 out.end_block(Block::ExternC);
596}
597
598fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
599 if out.header {
600 return;
601 }
602
603 out.set_namespace(&ety.name.namespace);
604
605 out.next_section();
606 let link_name = mangle::operator(&ety.name, "sizeof");
607 writeln!(
608 out,
609 "::std::size_t {}::layout::size() noexcept {{",
610 ety.name.cxx,
611 );
612 writeln!(out, " return {}();", link_name);
613 writeln!(out, "}}");
614
615 out.next_section();
616 let link_name = mangle::operator(&ety.name, "alignof");
617 writeln!(
618 out,
619 "::std::size_t {}::layout::align() noexcept {{",
620 ety.name.cxx,
621 );
622 writeln!(out, " return {}();", link_name);
623 writeln!(out, "}}");
624}
625
David Tolnay1eadd262020-12-29 16:42:08 -0800626fn begin_function_definition(out: &mut OutFile) {
627 if let Some(annotation) = &out.opt.cxx_impl_annotations {
628 write!(out, "{} ", annotation);
629 }
630}
631
David Tolnay0b9b9f82020-11-01 20:41:00 -0800632fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800633 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800634 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800635 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800636 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700637 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700638 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700639 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700640 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700641 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700642 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700643 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700644 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700645 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800646 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700647 write!(out, "const ");
648 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700649 write!(
650 out,
651 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800652 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700653 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700654 }
David Tolnay7db73692019-10-20 14:51:12 -0400655 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700656 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400657 write!(out, ", ");
658 }
David Tolnaya46a2372020-03-06 10:03:48 -0800659 if arg.ty == RustString {
660 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700661 } else if let Type::RustVec(_) = arg.ty {
662 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800663 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700664 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400665 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700666 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400667 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700668 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400669 write!(out, ", ");
670 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700671 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400672 write!(out, "*return$");
673 }
674 writeln!(out, ") noexcept {{");
675 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700676 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700677 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800678 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700679 Some(receiver) => write!(
680 out,
681 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700682 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800683 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700684 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700685 }
David Tolnay7db73692019-10-20 14:51:12 -0400686 for (i, arg) in efn.args.iter().enumerate() {
687 if i > 0 {
688 write!(out, ", ");
689 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700690 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400691 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700692 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700693 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800694 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700695 write!(out, " const");
696 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700697 }
698 write!(out, " = ");
699 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800700 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700701 Some(receiver) => write!(
702 out,
703 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700704 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800705 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700706 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700707 }
708 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400709 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700710 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700711 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700712 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700713 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700714 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700715 writeln!(out, " [&] {{");
716 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700717 }
David Tolnay7db73692019-10-20 14:51:12 -0400718 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700719 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400720 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700721 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400722 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700723 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400724 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700725 }
726 match &efn.ret {
727 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700728 Some(Type::Str(_)) if !indirect_return => {
729 out.builtin.rust_str_repr = true;
730 write!(out, "::rust::impl<::rust::Str>::repr(");
731 }
David Tolnay73b72642020-11-25 17:44:05 -0800732 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700733 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800734 write!(out, "::rust::impl<");
735 write_type(out, ty);
736 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700737 }
David Tolnay99642622020-03-25 13:07:35 -0700738 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400739 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700740 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800741 None => write!(out, "{}$(", efn.name.rust),
742 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700743 }
David Tolnay7db73692019-10-20 14:51:12 -0400744 for (i, arg) in efn.args.iter().enumerate() {
745 if i > 0 {
746 write!(out, ", ");
747 }
748 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700749 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400750 write!(out, "::from_raw({})", arg.ident);
751 } else if let Type::UniquePtr(_) = &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, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700754 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700755 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700756 write!(
757 out,
758 "::rust::impl<::rust::Str>::new_unchecked({})",
759 arg.ident,
760 );
David Tolnaya46a2372020-03-06 10:03:48 -0800761 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700762 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800763 write!(
764 out,
765 "::rust::String(::rust::unsafe_bitcopy, *{})",
766 arg.ident,
767 );
David Tolnay313b10e2020-04-25 16:30:51 -0700768 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700769 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700770 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700771 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800772 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800773 write_type(out, &arg.ty);
774 write!(out, "(static_cast<");
775 if slice.mutability.is_none() {
776 write!(out, "const ");
777 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800778 write_type_space(out, &slice.inner);
779 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700780 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700781 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800782 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400783 } else {
784 write!(out, "{}", arg.ident);
785 }
786 }
787 write!(out, ")");
788 match &efn.ret {
789 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
790 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800791 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400792 _ => {}
793 }
794 if indirect_return {
795 write!(out, ")");
796 }
797 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700798 if efn.throws {
799 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800800 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700801 writeln!(out, " throw$.ptr = nullptr;");
802 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700803 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700804 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700805 writeln!(
806 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800807 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700808 );
David Tolnay5d121442020-03-17 22:14:40 -0700809 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700810 writeln!(out, " return throw$;");
811 }
David Tolnay7db73692019-10-20 14:51:12 -0400812 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700813 for arg in &efn.args {
814 if let Type::Fn(f) = &arg.ty {
815 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700816 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700817 }
818 }
David Tolnayca563ee2020-11-01 20:12:27 -0800819 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700820}
821
822fn write_function_pointer_trampoline(
823 out: &mut OutFile,
824 efn: &ExternFn,
825 var: &Ident,
826 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700827) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700828 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700829 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700830 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700831
832 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700833 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
834 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400835}
836
David Tolnay0b9b9f82020-11-01 20:41:00 -0800837fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800838 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800839 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700840 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700841 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700842 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800843 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700844}
845
846fn write_rust_function_decl_impl(
847 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700848 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700849 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700850 indirect_call: bool,
851) {
David Tolnay04770742020-11-01 13:50:50 -0800852 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700854 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700855 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700856 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700857 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700858 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700859 write!(out, "{}(", link_name);
860 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700861 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800862 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700863 write!(out, "const ");
864 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700865 write!(
866 out,
867 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800868 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700869 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700870 needs_comma = true;
871 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700872 for arg in &sig.args {
873 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400874 write!(out, ", ");
875 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700876 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700877 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400878 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700879 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700880 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400881 write!(out, ", ");
882 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700883 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400884 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700885 needs_comma = true;
886 }
887 if indirect_call {
888 if needs_comma {
889 write!(out, ", ");
890 }
891 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400892 }
893 writeln!(out, ") noexcept;");
894}
895
David Tolnay0b9b9f82020-11-01 20:41:00 -0800896fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800897 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400898 for line in efn.doc.to_string().lines() {
899 writeln!(out, "//{}", line);
900 }
David Tolnaya73853b2020-04-20 01:19:56 -0700901 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800902 None => efn.name.cxx.to_string(),
903 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700904 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700905 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700906 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700907 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700908}
909
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700910fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700911 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700912 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700913 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700914 indirect_call: bool,
915) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700916 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700917 write!(out, "{}(", local_name);
918 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400919 if i > 0 {
920 write!(out, ", ");
921 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700922 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400923 write!(out, "{}", arg.ident);
924 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700925 if indirect_call {
926 if !sig.args.is_empty() {
927 write!(out, ", ");
928 }
929 write!(out, "void *extern$");
930 }
David Tolnay1e548172020-03-16 13:37:09 -0700931 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700932 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800933 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700934 write!(out, " const");
935 }
936 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700937 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700938 write!(out, " noexcept");
939 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700940}
941
942fn write_rust_function_shim_impl(
943 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700944 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700945 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700946 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700947 indirect_call: bool,
948) {
949 if out.header && sig.receiver.is_some() {
950 // We've already defined this inside the struct.
951 return;
952 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800953 if !out.header {
954 begin_function_definition(out);
955 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700956 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400957 if out.header {
958 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700959 return;
David Tolnay7db73692019-10-20 14:51:12 -0400960 }
David Tolnay439cde22020-04-20 00:46:25 -0700961 writeln!(out, " {{");
962 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700963 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700964 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700965 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700966 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700967 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700968 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
969 }
970 }
971 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700972 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700973 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700974 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700975 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700976 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700977 writeln!(out, "> return$;");
978 write!(out, " ");
979 } else if let Some(ret) = &sig.ret {
980 write!(out, "return ");
981 match ret {
982 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700983 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700984 write!(out, "::from_raw(");
985 }
986 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700987 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700988 write!(out, "(");
989 }
990 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700991 Type::Str(_) => {
992 out.builtin.rust_str_new_unchecked = true;
993 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
994 }
David Tolnay73b72642020-11-25 17:44:05 -0800995 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700996 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800997 write!(out, "::rust::impl<");
998 write_type(out, ret);
999 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001000 }
David Tolnay439cde22020-04-20 00:46:25 -07001001 _ => {}
1002 }
1003 }
1004 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -07001005 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001006 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001007 }
1008 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001009 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001010 if sig.receiver.is_some() {
1011 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001012 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001013 }
David Tolnay9d840362020-11-10 08:50:40 -08001014 for arg in &sig.args {
1015 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001016 write!(out, ", ");
1017 }
1018 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -07001019 Type::Str(_) => {
1020 out.builtin.rust_str_repr = true;
1021 write!(out, "::rust::impl<::rust::Str>::repr(");
1022 }
David Tolnay73b72642020-11-25 17:44:05 -08001023 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001024 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001025 write!(out, "::rust::impl<");
1026 write_type(out, &arg.ty);
1027 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001028 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001029 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -07001030 _ => {}
1031 }
1032 write!(out, "{}", arg.ident);
1033 match &arg.ty {
1034 Type::RustBox(_) => write!(out, ".into_raw()"),
1035 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -08001036 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001037 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001038 _ => {}
1039 }
David Tolnay9d840362020-11-10 08:50:40 -08001040 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001041 }
1042 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001043 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001044 write!(out, ", ");
1045 }
1046 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001047 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001048 }
1049 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001050 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001051 write!(out, ", ");
1052 }
1053 write!(out, "extern$");
1054 }
1055 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001056 if !indirect_return {
1057 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -08001058 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001059 write!(out, ")");
1060 }
David Tolnay439cde22020-04-20 00:46:25 -07001061 }
1062 }
1063 writeln!(out, ";");
1064 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001065 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001066 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001067 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001068 writeln!(out, " }}");
1069 }
1070 if indirect_return {
1071 out.include.utility = true;
1072 writeln!(out, " return ::std::move(return$.value);");
1073 }
1074 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001075}
1076
David Tolnaya7c2ea12020-10-30 21:32:53 -07001077fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001078 match ty {
1079 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001080 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001081 }
1082}
1083
David Tolnay75dca2e2020-03-25 20:17:52 -07001084fn indirect_return(sig: &Signature, types: &Types) -> bool {
1085 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001086 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001087 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001088}
1089
David Tolnaya7c2ea12020-10-30 21:32:53 -07001090fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001091 match ty {
1092 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001093 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001094 write!(out, "*");
1095 }
1096 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001097 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001098 write!(out, "const ");
1099 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001100 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001101 write!(out, " *");
1102 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001103 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001104 }
1105}
1106
David Tolnaya7c2ea12020-10-30 21:32:53 -07001107fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1108 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001109 match ty {
1110 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001111 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001112 _ => write_space_after_type(out, ty),
1113 }
1114}
1115
David Tolnaya7c2ea12020-10-30 21:32:53 -07001116fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001117 match ty {
1118 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001119 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001120 write!(out, "*");
1121 }
David Tolnay4a441222020-01-25 16:24:27 -08001122 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001123 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001124 write!(out, "const ");
1125 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001126 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001127 write!(out, " *");
1128 }
David Tolnay73b72642020-11-25 17:44:05 -08001129 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001130 out.builtin.ptr_len = true;
1131 write!(out, "::rust::repr::PtrLen ");
1132 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001133 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1134 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001135 }
1136}
1137
David Tolnaya7c2ea12020-10-30 21:32:53 -07001138fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001139 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001140 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001141 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001142 write!(out, "*");
1143 }
David Tolnay73b72642020-11-25 17:44:05 -08001144 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001145 out.builtin.ptr_len = true;
1146 write!(out, "::rust::repr::PtrLen ");
1147 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001148 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001149 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001150 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001151 write!(out, "*");
1152 }
1153 write!(out, "{}", arg.ident);
1154}
1155
David Tolnaya7c2ea12020-10-30 21:32:53 -07001156fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001157 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001158 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001159 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001160 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001161 },
1162 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001163 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001164 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001165 write!(out, ">");
1166 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001167 Type::RustVec(ty) => {
1168 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001169 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001170 write!(out, ">");
1171 }
David Tolnay7db73692019-10-20 14:51:12 -04001172 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001173 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001174 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001175 write!(out, ">");
1176 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001177 Type::SharedPtr(ptr) => {
1178 write!(out, "::std::shared_ptr<");
1179 write_type(out, &ptr.inner);
1180 write!(out, ">");
1181 }
David Tolnay215e77f2020-12-28 17:09:48 -08001182 Type::WeakPtr(ptr) => {
1183 write!(out, "::std::weak_ptr<");
1184 write_type(out, &ptr.inner);
1185 write!(out, ">");
1186 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001187 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001188 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001189 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001190 write!(out, ">");
1191 }
David Tolnay7db73692019-10-20 14:51:12 -04001192 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001193 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001194 write!(out, "const ");
1195 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001196 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001197 write!(out, " &");
1198 }
1199 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001200 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001201 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001202 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001203 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001204 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001205 write!(out, "const ");
1206 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001207 write_type(out, &slice.inner);
1208 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001209 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001210 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001211 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001212 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001213 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001214 None => write!(out, "void"),
1215 }
1216 write!(out, "(");
1217 for (i, arg) in f.args.iter().enumerate() {
1218 if i > 0 {
1219 write!(out, ", ");
1220 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001221 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001222 }
1223 write!(out, ")>");
1224 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001225 Type::Array(a) => {
1226 write!(out, "::std::array<");
1227 write_type(out, &a.inner);
1228 write!(out, ", {}>", &a.len);
1229 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001230 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001231 }
1232}
1233
David Tolnayf6a89f22020-05-10 23:39:27 -07001234fn write_atom(out: &mut OutFile, atom: Atom) {
1235 match atom {
1236 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001237 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001238 U8 => write!(out, "::std::uint8_t"),
1239 U16 => write!(out, "::std::uint16_t"),
1240 U32 => write!(out, "::std::uint32_t"),
1241 U64 => write!(out, "::std::uint64_t"),
1242 Usize => write!(out, "::std::size_t"),
1243 I8 => write!(out, "::std::int8_t"),
1244 I16 => write!(out, "::std::int16_t"),
1245 I32 => write!(out, "::std::int32_t"),
1246 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001247 Isize => write!(out, "::rust::isize"),
1248 F32 => write!(out, "float"),
1249 F64 => write!(out, "double"),
1250 CxxString => write!(out, "::std::string"),
1251 RustString => write!(out, "::rust::String"),
1252 }
1253}
1254
David Tolnaya7c2ea12020-10-30 21:32:53 -07001255fn write_type_space(out: &mut OutFile, ty: &Type) {
1256 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001257 write_space_after_type(out, ty);
1258}
1259
1260fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001261 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001262 Type::Ident(_)
1263 | Type::RustBox(_)
1264 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001265 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001266 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001267 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001268 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001269 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001270 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001271 | Type::Fn(_)
1272 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001273 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001274 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001275 }
1276}
1277
David Tolnay1bdb4712020-11-25 07:27:54 -08001278#[derive(Copy, Clone)]
1279enum UniquePtr<'a> {
David Tolnay75ea17c2020-12-06 21:08:34 -08001280 Ident(&'a RustName),
1281 CxxVector(&'a RustName),
David Tolnay1bdb4712020-11-25 07:27:54 -08001282}
1283
1284trait ToTypename {
1285 fn to_typename(&self, types: &Types) -> String;
1286}
1287
David Tolnay75ea17c2020-12-06 21:08:34 -08001288impl ToTypename for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001289 fn to_typename(&self, types: &Types) -> String {
1290 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001291 }
1292}
1293
David Tolnay1bdb4712020-11-25 07:27:54 -08001294impl<'a> ToTypename for UniquePtr<'a> {
1295 fn to_typename(&self, types: &Types) -> String {
1296 match self {
1297 UniquePtr::Ident(ident) => ident.to_typename(types),
1298 UniquePtr::CxxVector(element) => {
1299 format!("::std::vector<{}>", element.to_typename(types))
1300 }
1301 }
1302 }
1303}
1304
1305trait ToMangled {
1306 fn to_mangled(&self, types: &Types) -> Symbol;
1307}
1308
David Tolnay75ea17c2020-12-06 21:08:34 -08001309impl ToMangled for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001310 fn to_mangled(&self, types: &Types) -> Symbol {
1311 self.to_symbol(types)
1312 }
1313}
1314
1315impl<'a> ToMangled for UniquePtr<'a> {
1316 fn to_mangled(&self, types: &Types) -> Symbol {
1317 match self {
1318 UniquePtr::Ident(ident) => ident.to_mangled(types),
1319 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1320 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001321 }
1322}
1323
David Tolnaya7c2ea12020-10-30 21:32:53 -07001324fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001325 if out.header {
1326 return;
1327 }
1328
1329 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001330 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001331 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001332 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001333 if let Type::RustBox(ptr) = ty {
1334 if let Type::Ident(inner) = &ptr.inner {
1335 if Atom::from(&inner.rust).is_none()
1336 && (!out.types.aliases.contains_key(&inner.rust)
1337 || out.types.explicit_impls.contains(ty))
1338 {
1339 out.next_section();
1340 write_rust_box_extern(out, &out.types.resolve(&inner));
1341 }
David Tolnay7db73692019-10-20 14:51:12 -04001342 }
David Tolnayf33bc242020-12-04 18:27:02 -08001343 } else if let Type::RustVec(vec) = ty {
1344 if let Type::Ident(inner) = &vec.inner {
1345 if Atom::from(&inner.rust).is_none()
1346 && (!out.types.aliases.contains_key(&inner.rust)
1347 || out.types.explicit_impls.contains(ty))
1348 {
David Tolnay6787be62020-04-25 11:01:02 -07001349 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001350 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001351 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001352 }
David Tolnay7db73692019-10-20 14:51:12 -04001353 } else if let Type::UniquePtr(ptr) = ty {
1354 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001355 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001356 && (!out.types.aliases.contains_key(&inner.rust)
1357 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001358 {
David Tolnay7db73692019-10-20 14:51:12 -04001359 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001360 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001361 }
1362 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001363 } else if let Type::SharedPtr(ptr) = ty {
1364 if let Type::Ident(inner) = &ptr.inner {
1365 if Atom::from(&inner.rust).is_none()
David Tolnayb7453812020-12-01 21:46:55 -08001366 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnayb3b24a12020-12-01 15:27:43 -08001367 || out.types.explicit_impls.contains(ty))
1368 {
1369 out.next_section();
1370 write_shared_ptr(out, inner);
1371 }
1372 }
David Tolnay215e77f2020-12-28 17:09:48 -08001373 } else if let Type::WeakPtr(ptr) = ty {
1374 if let Type::Ident(inner) = &ptr.inner {
1375 if Atom::from(&inner.rust).is_none()
1376 && (!out.types.aliases.contains_key(&inner.rust)
1377 || out.types.explicit_impls.contains(ty))
1378 {
1379 out.next_section();
1380 write_weak_ptr(out, inner);
1381 }
1382 }
David Tolnayf33bc242020-12-04 18:27:02 -08001383 } else if let Type::CxxVector(vector) = ty {
1384 if let Type::Ident(inner) = &vector.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001385 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001386 && (!out.types.aliases.contains_key(&inner.rust)
1387 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001388 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001389 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001390 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001391 }
1392 }
1393 }
1394 }
David Tolnay0c033e32020-11-01 15:15:48 -08001395 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001396
David Tolnay0c033e32020-11-01 15:15:48 -08001397 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001398 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001399 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001400 if let Type::RustBox(ptr) = ty {
1401 if let Type::Ident(inner) = &ptr.inner {
1402 if Atom::from(&inner.rust).is_none()
1403 && (!out.types.aliases.contains_key(&inner.rust)
1404 || out.types.explicit_impls.contains(ty))
1405 {
1406 write_rust_box_impl(out, &out.types.resolve(&inner));
1407 }
David Tolnay7db73692019-10-20 14:51:12 -04001408 }
David Tolnayf33bc242020-12-04 18:27:02 -08001409 } else if let Type::RustVec(vec) = ty {
1410 if let Type::Ident(inner) = &vec.inner {
1411 if Atom::from(&inner.rust).is_none()
1412 && (!out.types.aliases.contains_key(&inner.rust)
1413 || out.types.explicit_impls.contains(ty))
1414 {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001415 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001416 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001417 }
David Tolnay7db73692019-10-20 14:51:12 -04001418 }
1419 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001420 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001421 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001422}
1423
David Tolnay8faec772020-11-02 00:18:19 -08001424fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001425 let inner = ident.to_fully_qualified();
1426 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001427
David Tolnay7db73692019-10-20 14:51:12 -04001428 writeln!(
1429 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001430 "{} *cxxbridge1$box${}$alloc() noexcept;",
1431 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001432 );
1433 writeln!(
1434 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001435 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1436 instance, inner,
1437 );
1438 writeln!(
1439 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001440 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001441 instance, inner,
1442 );
David Tolnay7db73692019-10-20 14:51:12 -04001443}
1444
David Tolnay75ea17c2020-12-06 21:08:34 -08001445fn write_rust_vec_extern(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001446 let inner = element.to_typename(out.types);
1447 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001448
David Tolnay12320e12020-12-09 23:09:36 -08001449 out.include.cstddef = true;
1450
Myron Ahneba35cf2020-02-05 19:41:51 +07001451 writeln!(
1452 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001453 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001454 instance, inner,
1455 );
1456 writeln!(
1457 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001458 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001459 instance, inner,
1460 );
1461 writeln!(
1462 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001463 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001464 instance, inner,
1465 );
David Tolnay219c0792020-04-24 20:31:37 -07001466 writeln!(
1467 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001468 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001469 instance, inner,
1470 );
1471 writeln!(
1472 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001473 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001474 inner, instance,
1475 );
David Tolnay503d0192020-04-24 22:18:56 -07001476 writeln!(
1477 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001478 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001479 instance, inner,
1480 );
1481 writeln!(
1482 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001483 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001484 instance, inner,
1485 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001486}
1487
David Tolnay8faec772020-11-02 00:18:19 -08001488fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001489 let inner = ident.to_fully_qualified();
1490 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001491
1492 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001493 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001494 writeln!(
1495 out,
1496 "{} *Box<{}>::allocation::alloc() noexcept {{",
1497 inner, inner,
1498 );
David Tolnayc4b34222020-12-12 13:06:26 -08001499 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001500 writeln!(out, "}}");
1501
1502 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001503 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001504 writeln!(
1505 out,
David Tolnaye5703162020-12-12 16:26:35 -08001506 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001507 inner, inner,
1508 );
1509 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1510 writeln!(out, "}}");
1511
1512 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001513 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001514 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001515 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001516 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001517}
1518
David Tolnay75ea17c2020-12-06 21:08:34 -08001519fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001520 let inner = element.to_typename(out.types);
1521 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001522
David Tolnay12320e12020-12-09 23:09:36 -08001523 out.include.cstddef = true;
1524
Myron Ahneba35cf2020-02-05 19:41:51 +07001525 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001526 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001527 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001528 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001529 writeln!(out, "}}");
1530
1531 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001532 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001533 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001534 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001535 writeln!(out, "}}");
1536
1537 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001538 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001539 writeln!(
1540 out,
1541 "::std::size_t Vec<{}>::size() const noexcept {{",
1542 inner,
1543 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001544 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001545 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001546
1547 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001548 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001549 writeln!(
1550 out,
1551 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1552 inner,
1553 );
David Tolnay381d9532020-12-11 13:59:45 -08001554 writeln!(
1555 out,
1556 " return cxxbridge1$rust_vec${}$capacity(this);",
1557 instance,
1558 );
David Tolnaydc62d712020-12-11 13:51:53 -08001559 writeln!(out, "}}");
1560
1561 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001562 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001563 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001564 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001565 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001566
1567 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001568 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001569 writeln!(
1570 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001571 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001572 inner,
1573 );
1574 writeln!(
1575 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001576 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001577 instance,
1578 );
1579 writeln!(out, "}}");
1580
1581 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001582 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001583 writeln!(
1584 out,
1585 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1586 inner,
1587 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001588 writeln!(
1589 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001590 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001591 instance,
1592 );
1593 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001594}
1595
David Tolnay75ea17c2020-12-06 21:08:34 -08001596fn write_unique_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001597 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001598 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001599}
1600
1601// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001602fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001603 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001604 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001605 let inner = ty.to_typename(out.types);
1606 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001607
David Tolnay63da4d32020-04-25 09:41:12 -07001608 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001609 // Some aliases are to opaque types; some are to trivial types. We can't
1610 // know at code generation time, so we generate both C++ and Rust side
1611 // bindings for a "new" method anyway. But the Rust code can't be called
1612 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001613 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001614 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001615 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001616 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001617 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001618 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001619 };
1620
David Tolnay53462762020-11-25 07:08:10 -08001621 let conditional_delete = match ty {
1622 UniquePtr::Ident(ident) => {
1623 !out.types.structs.contains_key(&ident.rust)
1624 && !out.types.enums.contains_key(&ident.rust)
1625 }
1626 UniquePtr::CxxVector(_) => false,
1627 };
1628
1629 if conditional_delete {
1630 out.builtin.is_complete = true;
1631 let definition = match ty {
1632 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1633 UniquePtr::CxxVector(_) => unreachable!(),
1634 };
1635 writeln!(
1636 out,
David Tolnay75068632020-12-26 22:15:17 -08001637 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001638 inner, definition,
1639 );
1640 }
David Tolnay7db73692019-10-20 14:51:12 -04001641 writeln!(
1642 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001643 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001644 inner,
1645 );
1646 writeln!(
1647 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001648 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001649 inner,
1650 );
1651 writeln!(
1652 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001653 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001654 instance, inner,
1655 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001656 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001657 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001658 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001659 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001660 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001661 out,
David Tolnay0b881402020-12-01 21:05:08 -08001662 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1663 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001664 );
David Tolnay63da4d32020-04-25 09:41:12 -07001665 writeln!(
1666 out,
David Tolnay0b881402020-12-01 21:05:08 -08001667 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1668 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001669 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001670 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001671 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001672 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001673 }
David Tolnay7db73692019-10-20 14:51:12 -04001674 writeln!(
1675 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001676 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001677 instance, inner, inner,
1678 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001679 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001680 writeln!(out, "}}");
1681 writeln!(
1682 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001683 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001684 inner, instance, inner,
1685 );
1686 writeln!(out, " return ptr.get();");
1687 writeln!(out, "}}");
1688 writeln!(
1689 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001690 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001691 inner, instance, inner,
1692 );
1693 writeln!(out, " return ptr.release();");
1694 writeln!(out, "}}");
1695 writeln!(
1696 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001697 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001698 instance, inner,
1699 );
David Tolnay53462762020-11-25 07:08:10 -08001700 if conditional_delete {
1701 out.builtin.deleter_if = true;
1702 writeln!(
1703 out,
David Tolnay75068632020-12-26 22:15:17 -08001704 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001705 inner,
1706 );
1707 } else {
1708 writeln!(out, " ptr->~unique_ptr();");
1709 }
David Tolnay7db73692019-10-20 14:51:12 -04001710 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001711}
Myron Ahneba35cf2020-02-05 19:41:51 +07001712
David Tolnay75ea17c2020-12-06 21:08:34 -08001713fn write_shared_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnayb3b24a12020-12-01 15:27:43 -08001714 let resolved = out.types.resolve(ident);
1715 let inner = resolved.to_fully_qualified();
1716 let instance = ident.to_symbol(out.types);
1717
David Tolnayb3b24a12020-12-01 15:27:43 -08001718 out.include.new = true;
1719 out.include.utility = true;
1720
1721 // Some aliases are to opaque types; some are to trivial types. We can't
1722 // know at code generation time, so we generate both C++ and Rust side
1723 // bindings for a "new" method anyway. But the Rust code can't be called for
1724 // Opaque types because the 'new' method is not implemented.
1725 let can_construct_from_value = out.types.structs.contains_key(&ident.rust)
1726 || out.types.enums.contains_key(&ident.rust)
1727 || out.types.aliases.contains_key(&ident.rust);
1728
David Tolnayb3b24a12020-12-01 15:27:43 -08001729 writeln!(
1730 out,
1731 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1732 inner,
1733 );
1734 writeln!(
1735 out,
1736 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1737 inner,
1738 );
1739 writeln!(
1740 out,
1741 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1742 instance, inner,
1743 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001744 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001745 writeln!(out, "}}");
1746 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001747 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001748 writeln!(
1749 out,
David Tolnay0b881402020-12-01 21:05:08 -08001750 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1751 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001752 );
1753 writeln!(
1754 out,
David Tolnay0b881402020-12-01 21:05:08 -08001755 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1756 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001757 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001758 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001759 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001760 writeln!(out, "}}");
1761 }
1762 writeln!(
1763 out,
1764 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1765 instance, inner, inner,
1766 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001767 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001768 writeln!(out, "}}");
1769 writeln!(
1770 out,
1771 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1772 inner, instance, inner,
1773 );
1774 writeln!(out, " return self.get();");
1775 writeln!(out, "}}");
1776 writeln!(
1777 out,
1778 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1779 instance, inner,
1780 );
1781 writeln!(out, " self->~shared_ptr();");
1782 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001783}
1784
David Tolnay215e77f2020-12-28 17:09:48 -08001785fn write_weak_ptr(out: &mut OutFile, ident: &RustName) {
1786 let resolved = out.types.resolve(ident);
1787 let inner = resolved.to_fully_qualified();
1788 let instance = ident.to_symbol(out.types);
1789
1790 out.include.new = true;
1791 out.include.utility = true;
1792
1793 writeln!(
1794 out,
1795 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1796 inner,
1797 );
1798 writeln!(
1799 out,
1800 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1801 inner,
1802 );
1803 writeln!(
1804 out,
1805 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1806 instance, inner,
1807 );
1808 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1809 writeln!(out, "}}");
1810 writeln!(
1811 out,
1812 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1813 instance, inner, inner,
1814 );
1815 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1816 writeln!(out, "}}");
1817 writeln!(
1818 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001819 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1820 instance, inner, inner,
1821 );
1822 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1823 writeln!(out, "}}");
1824 writeln!(
1825 out,
David Tolnay7a487852020-12-28 18:02:25 -08001826 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1827 instance, inner, inner,
1828 );
1829 writeln!(
1830 out,
1831 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1832 inner,
1833 );
1834 writeln!(out, "}}");
1835 writeln!(
1836 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001837 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1838 instance, inner,
1839 );
1840 writeln!(out, " self->~weak_ptr();");
1841 writeln!(out, "}}");
1842}
1843
David Tolnay75ea17c2020-12-06 21:08:34 -08001844fn write_cxx_vector(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001845 let inner = element.to_typename(out.types);
1846 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001847
David Tolnay12320e12020-12-09 23:09:36 -08001848 out.include.cstddef = true;
1849
Myron Ahneba35cf2020-02-05 19:41:51 +07001850 writeln!(
1851 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001852 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001853 instance, inner,
1854 );
1855 writeln!(out, " return s.size();");
1856 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001857 writeln!(
1858 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001859 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001860 inner, instance, inner,
1861 );
David Tolnay767e00d2020-12-21 17:12:27 -08001862 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001863 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001864
David Tolnay70820672020-12-07 11:15:14 -08001865 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001866 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001867}