blob: 406018af4768ccd09b07b6bbe9a1e88081c68af4 [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 Tolnaydde63022021-03-26 22:22:35 -04006use crate::syntax::instantiate::{ImplKey, NamedImplKey};
David Tolnaye352c1e2020-12-31 16:41:05 -08007use crate::syntax::map::UnorderedMap as Map;
David Tolnaye5cd1482021-01-02 21:04:32 -08008use crate::syntax::set::UnorderedSet;
David Tolnay891061b2020-04-19 22:42:33 -07009use crate::syntax::symbol::Symbol;
David Tolnay5b1d8632020-12-20 22:05:11 -080010use crate::syntax::trivial::{self, TrivialReason};
Adrian Taylorc8713432020-10-21 18:20:55 -070011use crate::syntax::{
David Tolnay3abed472020-12-31 23:34:53 -080012 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, Signature, Struct, Trait, Type,
13 TypeAlias, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070014};
David Tolnay7db73692019-10-20 14:51:12 -040015use proc_macro2::Ident;
16
David Tolnayac7188c2020-11-01 16:58:16 -080017pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080018 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040019 let out = &mut out_file;
20
David Tolnaydfb82d72020-11-02 00:10:10 -080021 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070022 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023
David Tolnay7b0e5102020-11-01 23:22:12 -080024 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080025 write_data_structures(out, apis);
26 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080027 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040028
David Tolnay3374d8d2020-10-31 22:18:45 -070029 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070030 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070031
David Tolnayac7188c2020-11-01 16:58:16 -080032 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070033}
34
David Tolnay7b0e5102020-11-01 23:22:12 -080035fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
36 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080037 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080038 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080039 _ => false,
40 };
Adrian Taylorc8713432020-10-21 18:20:55 -070041
David Tolnay7b0e5102020-11-01 23:22:12 -080042 let apis_by_namespace =
43 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
44
David Tolnayd920be52020-11-01 23:34:30 -080045 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080046
David Tolnayd920be52020-11-01 23:34:30 -080047 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080048 let apis = ns_entries.direct_content();
49
50 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080051 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080052 match api {
David Tolnayed6ba4a2021-01-01 14:59:40 -080053 Api::Struct(strct) => write_struct_decl(out, &strct.name),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080054 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080055 Api::CxxType(ety) => write_struct_using(out, &ety.name),
David Tolnayed6ba4a2021-01-01 14:59:40 -080056 Api::RustType(ety) => write_struct_decl(out, &ety.name),
David Tolnay7b0e5102020-11-01 23:22:12 -080057 _ => unreachable!(),
58 }
David Tolnay7db73692019-10-20 14:51:12 -040059 }
David Tolnay7db73692019-10-20 14:51:12 -040060
David Tolnay7b0e5102020-11-01 23:22:12 -080061 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080062 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
63 write(out, nested_ns_entries, indent + 2);
64 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080065 }
Adrian Taylorf9213622020-10-31 22:25:42 -070066 }
67}
68
David Tolnaye1109d92020-11-01 20:51:56 -080069fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnaye352c1e2020-12-31 16:41:05 -080070 let mut methods_for_type = Map::new();
David Tolnay630af882020-10-31 22:03:47 -070071 for api in apis {
David Tolnay102c7ea2020-11-08 18:58:09 -080072 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayf94bef12020-04-17 14:46:42 -070073 if let Some(receiver) = &efn.sig.receiver {
74 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070075 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070076 .or_insert_with(Vec::new)
77 .push(efn);
78 }
79 }
80 }
Joel Galenson968738f2020-04-15 14:19:33 -070081
David Tolnaye352c1e2020-12-31 16:41:05 -080082 let mut structs_written = UnorderedSet::new();
David Tolnay5439fa12020-11-03 18:45:01 -080083 let mut toposorted_structs = out.types.toposorted_structs.iter();
David Tolnay9622b462020-11-02 21:34:42 -080084 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070085 match api {
David Tolnay5439fa12020-11-03 18:45:01 -080086 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
87 for next in &mut toposorted_structs {
88 if !out.types.cxx.contains(&strct.name.rust) {
89 out.next_section();
David Tolnay102c7ea2020-11-08 18:58:09 -080090 let methods = methods_for_type
91 .get(&strct.name.rust)
92 .map(Vec::as_slice)
93 .unwrap_or_default();
94 write_struct(out, next, methods);
David Tolnay5439fa12020-11-03 18:45:01 -080095 }
96 structs_written.insert(&next.name.rust);
97 if next.name.rust == strct.name.rust {
98 break;
99 }
100 }
101 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700102 Api::Enum(enm) => {
103 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800104 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700105 check_enum(out, enm);
106 } else {
107 write_enum(out, enm);
108 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700109 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700110 Api::RustType(ety) => {
David Tolnay8d067de2020-12-26 16:18:23 -0800111 out.next_section();
112 let methods = methods_for_type
113 .get(&ety.name.rust)
114 .map(Vec::as_slice)
115 .unwrap_or_default();
116 write_opaque_type(out, ety, methods);
David Tolnayc1fe0052020-04-17 15:15:06 -0700117 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700118 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400119 }
120 }
121
David Tolnay8b229532021-01-02 18:30:45 -0800122 if out.header {
123 return;
124 }
125
David Tolnay440e24a2021-01-01 23:25:34 -0800126 out.set_namespace(Default::default());
127
David Tolnay8b229532021-01-02 18:30:45 -0800128 out.next_section();
129 for api in apis {
130 if let Api::TypeAlias(ety) = api {
131 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
132 check_trivial_extern_type(out, ety, reasons)
David Tolnayfabca772020-10-03 21:25:41 -0700133 }
134 }
135 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800136}
137
David Tolnaye1109d92020-11-01 20:51:56 -0800138fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700139 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400140 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800141 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800142 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800143 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800144 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
145 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
146 _ => {}
147 }
David Tolnay7db73692019-10-20 14:51:12 -0400148 }
David Tolnay7da38202020-11-27 17:36:16 -0800149
150 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400151 }
152
153 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800154 match api {
155 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800156 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800157 Api::RustFunction(efn) => {
158 out.next_section();
159 write_rust_function_shim(out, efn);
160 }
161 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400162 }
163 }
David Tolnay7db73692019-10-20 14:51:12 -0400164}
165
David Tolnay7da38202020-11-27 17:36:16 -0800166fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
167 out.set_namespace(Default::default());
168 out.begin_block(Block::Namespace("std"));
169
170 for api in apis {
171 if let Api::Struct(strct) = api {
172 if derive::contains(&strct.derives, Trait::Hash) {
173 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800174 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800175 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800176 let qualified = strct.name.to_fully_qualified();
177 writeln!(out, "template <> struct hash<{}> {{", qualified);
178 writeln!(
179 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800180 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800181 qualified,
182 );
183 let link_name = mangle::operator(&strct.name, "hash");
184 write!(out, " return ::");
185 for name in &strct.name.namespace {
186 write!(out, "{}::", name);
187 }
188 writeln!(out, "{}(self);", link_name);
189 writeln!(out, " }}");
190 writeln!(out, "}};");
191 }
192 }
193 }
194
195 out.end_block(Block::Namespace("std"));
196}
197
David Tolnaydfb82d72020-11-02 00:10:10 -0800198fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
199 for api in apis {
200 if let Api::Include(include) = api {
201 out.include.insert(include);
202 }
203 }
204
David Tolnaya7c2ea12020-10-30 21:32:53 -0700205 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400206 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700207 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700208 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
209 | Some(I64) => out.include.cstdint = true,
210 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800211 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700212 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800213 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800214 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700215 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800216 Type::RustBox(_) => out.builtin.rust_box = true,
217 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700218 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay215e77f2020-12-28 17:09:48 -0800219 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800220 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700221 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800222 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800223 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800224 Type::Array(_) => out.include.array = true,
Adrian Taylor38ae2282021-01-23 10:20:32 -0800225 Type::Ref(_) | Type::Void(_) | Type::Ptr(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400226 }
227 }
David Tolnayec66d112020-10-31 21:00:26 -0700228}
David Tolnayf51447e2020-03-06 14:14:27 -0800229
David Tolnay102c7ea2020-11-08 18:58:09 -0800230fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800231 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800232 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800233
David Tolnay17a934c2020-11-02 00:40:04 -0800234 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800235 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700236 writeln!(out, "#ifndef {}", guard);
237 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400238 for line in strct.doc.to_string().lines() {
239 writeln!(out, "//{}", line);
240 }
David Tolnay17a934c2020-11-02 00:40:04 -0800241 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800242
David Tolnay7db73692019-10-20 14:51:12 -0400243 for field in &strct.fields {
David Tolnay5573e522020-12-31 11:07:38 -0800244 for line in field.doc.to_string().lines() {
245 writeln!(out, " //{}", line);
246 }
David Tolnay7db73692019-10-20 14:51:12 -0400247 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700248 write_type_space(out, &field.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800249 writeln!(out, "{};", field.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400250 }
David Tolnay84389352020-11-27 17:12:10 -0800251
David Tolnay5554ebd2020-12-10 11:34:41 -0800252 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800253
David Tolnay102c7ea2020-11-08 18:58:09 -0800254 for method in methods {
255 write!(out, " ");
256 let sig = &method.sig;
257 let local_name = method.name.cxx.to_string();
258 write_rust_function_shim_decl(out, &local_name, sig, false);
259 writeln!(out, ";");
260 }
David Tolnay84389352020-11-27 17:12:10 -0800261
David Tolnayb960ed22020-11-27 14:34:30 -0800262 if operator_eq {
263 writeln!(
264 out,
265 " bool operator==(const {} &) const noexcept;",
266 strct.name.cxx,
267 );
268 writeln!(
269 out,
270 " bool operator!=(const {} &) const noexcept;",
271 strct.name.cxx,
272 );
273 }
David Tolnay84389352020-11-27 17:12:10 -0800274
275 if operator_ord {
276 writeln!(
277 out,
278 " bool operator<(const {} &) const noexcept;",
279 strct.name.cxx,
280 );
281 writeln!(
282 out,
283 " bool operator<=(const {} &) const noexcept;",
284 strct.name.cxx,
285 );
286 writeln!(
287 out,
288 " bool operator>(const {} &) const noexcept;",
289 strct.name.cxx,
290 );
291 writeln!(
292 out,
293 " bool operator>=(const {} &) const noexcept;",
294 strct.name.cxx,
295 );
296 }
297
David Tolnay5554ebd2020-12-10 11:34:41 -0800298 out.include.type_traits = true;
299 writeln!(out, " using IsRelocatable = ::std::true_type;");
300
David Tolnay7db73692019-10-20 14:51:12 -0400301 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700302 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400303}
304
David Tolnayed6ba4a2021-01-01 14:59:40 -0800305fn write_struct_decl(out: &mut OutFile, ident: &Pair) {
306 writeln!(out, "struct {};", ident.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400307}
308
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800309fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800310 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800311 write_atom(out, enm.repr);
312 writeln!(out, ";");
313}
314
David Tolnay8faec772020-11-02 00:18:19 -0800315fn write_struct_using(out: &mut OutFile, ident: &Pair) {
316 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800317}
318
David Tolnay8d067de2020-12-26 16:18:23 -0800319fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800320 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800321 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700322 writeln!(out, "#ifndef {}", guard);
323 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700324 for line in ety.doc.to_string().lines() {
325 writeln!(out, "//{}", line);
326 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800327
David Tolnay365fc7c2020-11-25 16:08:13 -0800328 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800329 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800330 out,
331 "struct {} final : public ::rust::Opaque {{",
332 ety.name.cxx,
333 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800334
Joel Galenson968738f2020-04-15 14:19:33 -0700335 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800336 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700337 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800338 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700339 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800340 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800341 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800342
David Tolnay8f940cf2021-01-02 18:45:00 -0800343 writeln!(out, " ~{}() = delete;", ety.name.cxx);
344 writeln!(out);
David Tolnay6ba262f2020-12-26 22:23:50 -0800345
David Tolnayee6ecfc2020-12-26 21:54:37 -0800346 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800347 out.include.cstddef = true;
348 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800349 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800350 writeln!(out, " struct layout {{");
351 writeln!(out, " static ::std::size_t size() noexcept;");
352 writeln!(out, " static ::std::size_t align() noexcept;");
353 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700354 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700355 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700356}
357
David Tolnay0b9b9f82020-11-01 20:41:00 -0800358fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800359 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800360 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700361 writeln!(out, "#ifndef {}", guard);
362 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700363 for line in enm.doc.to_string().lines() {
364 writeln!(out, "//{}", line);
365 }
David Tolnay17a934c2020-11-02 00:40:04 -0800366 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700367 write_atom(out, enm.repr);
368 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700369 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800370 for line in variant.doc.to_string().lines() {
371 writeln!(out, " //{}", line);
372 }
David Tolnaye6f62142020-12-21 16:00:41 -0800373 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700374 }
375 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700376 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700377}
378
David Tolnay0b9b9f82020-11-01 20:41:00 -0800379fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800380 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800381 out.include.type_traits = true;
382 writeln!(
383 out,
384 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
385 enm.name.cxx,
386 );
David Tolnay17a934c2020-11-02 00:40:04 -0800387 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700388 write_atom(out, enm.repr);
389 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700390 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700391 write!(out, "static_assert(static_cast<");
392 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700393 writeln!(
394 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700395 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800396 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700397 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700398 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700399}
400
David Tolnay5b1d8632020-12-20 22:05:11 -0800401fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800402 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700403 // necessary for soundness. That's because triviality is always declared by
404 // the user in the form of an unsafe impl of cxx::ExternType:
405 //
406 // unsafe impl ExternType for MyType {
407 // type Id = cxx::type_id!("...");
408 // type Kind = cxx::kind::Trivial;
409 // }
410 //
411 // Since the user went on the record with their unsafe impl to unsafely
412 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800413 // them if that were wrong. However, in practice correctly reasoning about
414 // the relocatability of C++ types is challenging, particularly if the type
415 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700416 //
David Tolnay174bd952020-11-02 09:23:12 -0800417 // There may be legitimate reasons to opt out of this assertion for support
418 // of types that the programmer knows are soundly Rust-movable despite not
419 // being recognized as such by the C++ type system due to a move constructor
420 // or destructor. To opt out of the relocatability check, they need to do
421 // one of the following things in any header used by `include!` in their
422 // bridge.
423 //
424 // --- if they define the type:
425 // struct MyType {
426 // ...
427 // + using IsRelocatable = std::true_type;
428 // };
429 //
430 // --- otherwise:
431 // + template <>
432 // + struct rust::IsRelocatable<MyType> : std::true_type {};
433 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700434
David Tolnay5b1d8632020-12-20 22:05:11 -0800435 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800436 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800437 writeln!(out, "static_assert(");
438 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
439 writeln!(
440 out,
441 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
442 id.trim_start_matches("::"),
443 trivial::as_what(&alias.name, reasons),
444 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700445}
446
David Tolnayb960ed22020-11-27 14:34:30 -0800447fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
448 out.set_namespace(&strct.name.namespace);
449 out.begin_block(Block::ExternC);
450
451 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800452 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800453 writeln!(
454 out,
455 "bool {}(const {1} &, const {1} &) noexcept;",
456 link_name, strct.name.cxx,
457 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800458
459 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800460 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800461 writeln!(
462 out,
463 "bool {}(const {1} &, const {1} &) noexcept;",
464 link_name, strct.name.cxx,
465 );
466 }
David Tolnayb960ed22020-11-27 14:34:30 -0800467 }
468
David Tolnay84389352020-11-27 17:12:10 -0800469 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800470 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800471 writeln!(
472 out,
473 "bool {}(const {1} &, const {1} &) noexcept;",
474 link_name, strct.name.cxx,
475 );
476
David Tolnaya05f9402020-11-27 17:44:05 -0800477 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800478 writeln!(
479 out,
480 "bool {}(const {1} &, const {1} &) noexcept;",
481 link_name, strct.name.cxx,
482 );
483
484 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800485 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800486 writeln!(
487 out,
488 "bool {}(const {1} &, const {1} &) noexcept;",
489 link_name, strct.name.cxx,
490 );
491
David Tolnaya05f9402020-11-27 17:44:05 -0800492 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800493 writeln!(
494 out,
495 "bool {}(const {1} &, const {1} &) noexcept;",
496 link_name, strct.name.cxx,
497 );
498 }
499 }
500
David Tolnay7da38202020-11-27 17:36:16 -0800501 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800502 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800503 let link_name = mangle::operator(&strct.name, "hash");
504 writeln!(
505 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800506 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800507 link_name, strct.name.cxx,
508 );
509 }
510
David Tolnayb960ed22020-11-27 14:34:30 -0800511 out.end_block(Block::ExternC);
512}
513
514fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
515 if out.header {
516 return;
517 }
518
519 out.set_namespace(&strct.name.namespace);
520
521 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800522 out.next_section();
523 writeln!(
524 out,
525 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
526 strct.name.cxx,
527 );
David Tolnaya05f9402020-11-27 17:44:05 -0800528 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800529 writeln!(out, " return {}(*this, rhs);", link_name);
530 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800531
David Tolnayb960ed22020-11-27 14:34:30 -0800532 out.next_section();
533 writeln!(
534 out,
535 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
536 strct.name.cxx,
537 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800538 if derive::contains(&strct.derives, Trait::Eq) {
539 writeln!(out, " return !(*this == rhs);");
540 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800541 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800542 writeln!(out, " return {}(*this, rhs);", link_name);
543 }
David Tolnayb960ed22020-11-27 14:34:30 -0800544 writeln!(out, "}}");
545 }
David Tolnay84389352020-11-27 17:12:10 -0800546
547 if derive::contains(&strct.derives, Trait::PartialOrd) {
548 out.next_section();
549 writeln!(
550 out,
551 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
552 strct.name.cxx,
553 );
David Tolnaya05f9402020-11-27 17:44:05 -0800554 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800555 writeln!(out, " return {}(*this, rhs);", link_name);
556 writeln!(out, "}}");
557
558 out.next_section();
559 writeln!(
560 out,
561 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
562 strct.name.cxx,
563 );
David Tolnaya05f9402020-11-27 17:44:05 -0800564 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800565 writeln!(out, " return {}(*this, rhs);", link_name);
566 writeln!(out, "}}");
567
568 out.next_section();
569 writeln!(
570 out,
571 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
572 strct.name.cxx,
573 );
574 if derive::contains(&strct.derives, Trait::Ord) {
575 writeln!(out, " return !(*this <= rhs);");
576 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800577 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800578 writeln!(out, " return {}(*this, rhs);", link_name);
579 }
580 writeln!(out, "}}");
581
582 out.next_section();
583 writeln!(
584 out,
585 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
586 strct.name.cxx,
587 );
588 if derive::contains(&strct.derives, Trait::Ord) {
589 writeln!(out, " return !(*this < rhs);");
590 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800591 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800592 writeln!(out, " return {}(*this, rhs);", link_name);
593 }
594 writeln!(out, "}}");
595 }
David Tolnayb960ed22020-11-27 14:34:30 -0800596}
597
David Tolnay358bc4b2020-12-26 22:37:57 -0800598fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
599 out.set_namespace(&ety.name.namespace);
600 out.begin_block(Block::ExternC);
601
602 let link_name = mangle::operator(&ety.name, "sizeof");
603 writeln!(out, "::std::size_t {}() noexcept;", link_name);
604
605 let link_name = mangle::operator(&ety.name, "alignof");
606 writeln!(out, "::std::size_t {}() noexcept;", link_name);
607
608 out.end_block(Block::ExternC);
609}
610
611fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
612 if out.header {
613 return;
614 }
615
616 out.set_namespace(&ety.name.namespace);
617
618 out.next_section();
619 let link_name = mangle::operator(&ety.name, "sizeof");
620 writeln!(
621 out,
622 "::std::size_t {}::layout::size() noexcept {{",
623 ety.name.cxx,
624 );
625 writeln!(out, " return {}();", link_name);
626 writeln!(out, "}}");
627
628 out.next_section();
629 let link_name = mangle::operator(&ety.name, "alignof");
630 writeln!(
631 out,
632 "::std::size_t {}::layout::align() noexcept {{",
633 ety.name.cxx,
634 );
635 writeln!(out, " return {}();", link_name);
636 writeln!(out, "}}");
637}
638
David Tolnay1eadd262020-12-29 16:42:08 -0800639fn begin_function_definition(out: &mut OutFile) {
640 if let Some(annotation) = &out.opt.cxx_impl_annotations {
641 write!(out, "{} ", annotation);
642 }
643}
644
David Tolnay0b9b9f82020-11-01 20:41:00 -0800645fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800646 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800647 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800648 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800649 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700650 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700651 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700652 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700653 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700654 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700655 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700656 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700657 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700658 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800659 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700660 write!(out, "const ");
661 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700662 write!(
663 out,
664 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800665 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700666 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700667 }
David Tolnay7db73692019-10-20 14:51:12 -0400668 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700669 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400670 write!(out, ", ");
671 }
David Tolnaya46a2372020-03-06 10:03:48 -0800672 if arg.ty == RustString {
673 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700674 } else if let Type::RustVec(_) = arg.ty {
675 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800676 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700677 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400678 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700679 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400680 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700681 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400682 write!(out, ", ");
683 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700684 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400685 write!(out, "*return$");
686 }
687 writeln!(out, ") noexcept {{");
688 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700689 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700690 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800691 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700692 Some(receiver) => write!(
693 out,
694 "({}::*{}$)(",
David Tolnay1e5fe232021-01-01 18:11:40 -0800695 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800696 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700697 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700698 }
David Tolnay7db73692019-10-20 14:51:12 -0400699 for (i, arg) in efn.args.iter().enumerate() {
700 if i > 0 {
701 write!(out, ", ");
702 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700703 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400704 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700705 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700706 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800707 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700708 write!(out, " const");
709 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700710 }
711 write!(out, " = ");
712 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800713 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700714 Some(receiver) => write!(
715 out,
716 "&{}::{}",
David Tolnay1e5fe232021-01-01 18:11:40 -0800717 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800718 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700719 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700720 }
721 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400722 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700723 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700724 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700725 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700726 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700727 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700728 writeln!(out, " [&] {{");
729 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700730 }
David Tolnay7db73692019-10-20 14:51:12 -0400731 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700732 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400733 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700734 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400735 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700736 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400737 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700738 }
David Tolnayfe77f332021-01-02 17:39:51 -0800739 match &efn.ret {
740 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnaye5cd1482021-01-02 21:04:32 -0800741 Some(Type::Str(_)) if !indirect_return => {
742 out.builtin.rust_str_repr = true;
743 write!(out, "::rust::impl<::rust::Str>::repr(");
744 }
745 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
746 out.builtin.rust_slice_repr = true;
747 write!(out, "::rust::impl<");
748 write_type(out, ty);
749 write!(out, ">::repr(");
750 }
David Tolnayfe77f332021-01-02 17:39:51 -0800751 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400752 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700753 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800754 None => write!(out, "{}$(", efn.name.rust),
755 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700756 }
David Tolnay7db73692019-10-20 14:51:12 -0400757 for (i, arg) in efn.args.iter().enumerate() {
758 if i > 0 {
759 write!(out, ", ");
760 }
761 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700762 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800763 write!(out, "::from_raw({})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400764 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700765 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800766 write!(out, "({})", arg.name.cxx);
David Tolnaya46a2372020-03-06 10:03:48 -0800767 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700768 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800769 write!(
770 out,
771 "::rust::String(::rust::unsafe_bitcopy, *{})",
David Tolnay84ed6ad2021-01-01 15:30:14 -0800772 arg.name.cxx,
David Tolnaycc3767f2020-03-06 10:41:51 -0800773 );
David Tolnay313b10e2020-04-25 16:30:51 -0700774 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700775 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700776 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800777 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.name.cxx);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700778 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700779 out.include.utility = true;
David Tolnay84ed6ad2021-01-01 15:30:14 -0800780 write!(out, "::std::move(*{})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400781 } else {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800782 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400783 }
784 }
785 write!(out, ")");
786 match &efn.ret {
787 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
788 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaye5cd1482021-01-02 21:04:32 -0800789 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400790 _ => {}
791 }
792 if indirect_return {
793 write!(out, ")");
794 }
795 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700796 if efn.throws {
797 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800798 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700799 writeln!(out, " throw$.ptr = nullptr;");
800 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700801 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700802 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700803 writeln!(
804 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800805 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700806 );
David Tolnay5d121442020-03-17 22:14:40 -0700807 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700808 writeln!(out, " return throw$;");
809 }
David Tolnay7db73692019-10-20 14:51:12 -0400810 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700811 for arg in &efn.args {
812 if let Type::Fn(f) = &arg.ty {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800813 let var = &arg.name;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700814 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700815 }
816 }
David Tolnayca563ee2020-11-01 20:12:27 -0800817 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700818}
819
David Tolnay84ed6ad2021-01-01 15:30:14 -0800820fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700821 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700822 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700823 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700824
825 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700826 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
827 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400828}
829
David Tolnay0b9b9f82020-11-01 20:41:00 -0800830fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800831 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800832 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700833 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700834 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700835 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800836 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700837}
838
839fn write_rust_function_decl_impl(
840 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700841 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700842 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700843 indirect_call: bool,
844) {
David Tolnay04770742020-11-01 13:50:50 -0800845 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700846 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700847 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700848 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700849 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700850 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700851 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700852 write!(out, "{}(", link_name);
853 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700854 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800855 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700856 write!(out, "const ");
857 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700858 write!(
859 out,
860 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800861 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700862 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700863 needs_comma = true;
864 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700865 for arg in &sig.args {
866 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400867 write!(out, ", ");
868 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700869 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700870 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400871 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700872 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700873 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400874 write!(out, ", ");
875 }
David Tolnay201cb742021-03-26 19:22:16 -0400876 match sig.ret.as_ref().unwrap() {
877 Type::Ref(ret) => {
878 write_pointee_type(out, &ret.inner, ret.mutable);
879 write!(out, " *");
880 }
881 ret => write_type_space(out, ret),
882 }
David Tolnay7db73692019-10-20 14:51:12 -0400883 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700884 needs_comma = true;
885 }
886 if indirect_call {
887 if needs_comma {
888 write!(out, ", ");
889 }
890 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400891 }
892 writeln!(out, ") noexcept;");
893}
894
David Tolnay0b9b9f82020-11-01 20:41:00 -0800895fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800896 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400897 for line in efn.doc.to_string().lines() {
898 writeln!(out, "//{}", line);
899 }
David Tolnaya73853b2020-04-20 01:19:56 -0700900 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800901 None => efn.name.cxx.to_string(),
David Tolnay1e5fe232021-01-01 18:11:40 -0800902 Some(receiver) => format!(
903 "{}::{}",
904 out.types.resolve(&receiver.ty).name.cxx,
905 efn.name.cxx,
906 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700907 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700908 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700909 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700910 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700911}
912
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700913fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700914 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700915 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700916 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700917 indirect_call: bool,
918) {
David Cattermoleb9bf5e72021-01-02 23:21:21 +0000919 begin_function_definition(out);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700920 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700921 write!(out, "{}(", local_name);
922 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400923 if i > 0 {
924 write!(out, ", ");
925 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700926 write_type_space(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800927 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400928 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700929 if indirect_call {
930 if !sig.args.is_empty() {
931 write!(out, ", ");
932 }
933 write!(out, "void *extern$");
934 }
David Tolnay1e548172020-03-16 13:37:09 -0700935 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700936 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800937 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700938 write!(out, " const");
939 }
940 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700941 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700942 write!(out, " noexcept");
943 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700944}
945
946fn write_rust_function_shim_impl(
947 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700948 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700949 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700950 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700951 indirect_call: bool,
952) {
953 if out.header && sig.receiver.is_some() {
954 // We've already defined this inside the struct.
955 return;
956 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700957 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400958 if out.header {
959 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700960 return;
David Tolnay7db73692019-10-20 14:51:12 -0400961 }
David Tolnay439cde22020-04-20 00:46:25 -0700962 writeln!(out, " {{");
963 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700964 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700965 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700966 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700967 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700968 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800969 writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -0700970 }
971 }
972 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700973 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700974 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700975 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700976 write!(out, "::rust::MaybeUninit<");
David Tolnay201cb742021-03-26 19:22:16 -0400977 match sig.ret.as_ref().unwrap() {
978 Type::Ref(ret) => {
979 write_pointee_type(out, &ret.inner, ret.mutable);
980 write!(out, " *");
981 }
982 ret => write_type(out, ret),
983 }
David Tolnay439cde22020-04-20 00:46:25 -0700984 writeln!(out, "> return$;");
985 write!(out, " ");
986 } else if let Some(ret) = &sig.ret {
987 write!(out, "return ");
988 match ret {
989 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700990 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700991 write!(out, "::from_raw(");
992 }
993 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700994 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700995 write!(out, "(");
996 }
997 Type::Ref(_) => write!(out, "*"),
David Tolnaye5cd1482021-01-02 21:04:32 -0800998 Type::Str(_) => {
999 out.builtin.rust_str_new_unchecked = true;
1000 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
1001 }
1002 Type::SliceRef(_) => {
1003 out.builtin.rust_slice_new = true;
1004 write!(out, "::rust::impl<");
1005 write_type(out, ret);
1006 write!(out, ">::slice(");
1007 }
David Tolnay439cde22020-04-20 00:46:25 -07001008 _ => {}
1009 }
1010 }
1011 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -07001012 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001013 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001014 }
1015 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001016 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001017 if sig.receiver.is_some() {
1018 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001019 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001020 }
David Tolnay9d840362020-11-10 08:50:40 -08001021 for arg in &sig.args {
1022 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001023 write!(out, ", ");
1024 }
David Tolnay681f5c82021-01-01 22:56:27 -08001025 if out.types.needs_indirect_abi(&arg.ty) {
1026 write!(out, "&");
David Tolnay439cde22020-04-20 00:46:25 -07001027 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001028 write!(out, "{}", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -07001029 match &arg.ty {
1030 Type::RustBox(_) => write!(out, ".into_raw()"),
1031 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001032 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001033 _ => {}
1034 }
David Tolnay9d840362020-11-10 08:50:40 -08001035 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001036 }
1037 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001038 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001039 write!(out, ", ");
1040 }
1041 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001042 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001043 }
1044 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001045 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001046 write!(out, ", ");
1047 }
1048 write!(out, "extern$");
1049 }
1050 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001051 if !indirect_return {
1052 if let Some(ret) = &sig.ret {
David Tolnaye5cd1482021-01-02 21:04:32 -08001053 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
1054 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001055 }
David Tolnay439cde22020-04-20 00:46:25 -07001056 }
1057 }
1058 writeln!(out, ";");
1059 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001060 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001061 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001062 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001063 writeln!(out, " }}");
1064 }
1065 if indirect_return {
David Tolnay201cb742021-03-26 19:22:16 -04001066 write!(out, " return ");
1067 match sig.ret.as_ref().unwrap() {
1068 Type::Ref(_) => write!(out, "*return$.value"),
1069 _ => {
1070 out.include.utility = true;
1071 write!(out, "::std::move(return$.value)");
1072 }
1073 }
1074 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -07001075 }
1076 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001077}
1078
David Tolnaya7c2ea12020-10-30 21:32:53 -07001079fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001080 match ty {
1081 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001082 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001083 }
1084}
1085
David Tolnay75dca2e2020-03-25 20:17:52 -07001086fn indirect_return(sig: &Signature, types: &Types) -> bool {
1087 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001088 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001089 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001090}
1091
David Tolnaya7c2ea12020-10-30 21:32:53 -07001092fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001093 match ty {
1094 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001095 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001096 write!(out, "*");
1097 }
1098 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001099 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001100 write!(out, "const ");
1101 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001102 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001103 write!(out, " *");
1104 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001105 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001106 }
1107}
1108
David Tolnaya7c2ea12020-10-30 21:32:53 -07001109fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1110 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001111 match ty {
1112 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001113 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001114 _ => write_space_after_type(out, ty),
1115 }
1116}
1117
David Tolnaya7c2ea12020-10-30 21:32:53 -07001118fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001119 match ty {
1120 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001121 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001122 write!(out, "*");
1123 }
David Tolnay4a441222020-01-25 16:24:27 -08001124 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001125 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001126 write!(out, "const ");
1127 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001128 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001129 write!(out, " *");
1130 }
David Tolnaye5cd1482021-01-02 21:04:32 -08001131 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnayfe77f332021-01-02 17:39:51 -08001132 out.builtin.repr_fat = true;
David Tolnaye5cd1482021-01-02 21:04:32 -08001133 write!(out, "::rust::repr::Fat ");
David Tolnayfe77f332021-01-02 17:39:51 -08001134 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001135 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1136 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001137 }
1138}
1139
David Tolnaya7c2ea12020-10-30 21:32:53 -07001140fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001141 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001142 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001143 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001144 write!(out, "*");
1145 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001146 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001147 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001148 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001149 write!(out, "*");
1150 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001151 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -04001152}
1153
David Tolnaya7c2ea12020-10-30 21:32:53 -07001154fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001155 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001156 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001157 Some(atom) => write_atom(out, atom),
David Tolnay1e5fe232021-01-01 18:11:40 -08001158 None => write!(
1159 out,
1160 "{}",
1161 out.types.resolve(ident).name.to_fully_qualified(),
1162 ),
David Tolnay7db73692019-10-20 14:51:12 -04001163 },
1164 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001165 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001166 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001167 write!(out, ">");
1168 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001169 Type::RustVec(ty) => {
1170 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001171 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001172 write!(out, ">");
1173 }
David Tolnay7db73692019-10-20 14:51:12 -04001174 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001175 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001176 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001177 write!(out, ">");
1178 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001179 Type::SharedPtr(ptr) => {
1180 write!(out, "::std::shared_ptr<");
1181 write_type(out, &ptr.inner);
1182 write!(out, ">");
1183 }
David Tolnay215e77f2020-12-28 17:09:48 -08001184 Type::WeakPtr(ptr) => {
1185 write!(out, "::std::weak_ptr<");
1186 write_type(out, &ptr.inner);
1187 write!(out, ">");
1188 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001189 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001190 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001191 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001192 write!(out, ">");
1193 }
David Tolnay7db73692019-10-20 14:51:12 -04001194 Type::Ref(r) => {
David Tolnay5d6429d2021-03-26 19:19:15 -04001195 write_pointee_type(out, &r.inner, r.mutable);
David Tolnay7db73692019-10-20 14:51:12 -04001196 write!(out, " &");
1197 }
Adrian Taylor38ae2282021-01-23 10:20:32 -08001198 Type::Ptr(p) => {
David Tolnay5d6429d2021-03-26 19:19:15 -04001199 write_pointee_type(out, &p.inner, p.mutable);
Adrian Taylor38ae2282021-01-23 10:20:32 -08001200 write!(out, " *");
1201 }
David Tolnay7db73692019-10-20 14:51:12 -04001202 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001203 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001204 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001205 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001206 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001207 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001208 write!(out, "const ");
1209 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001210 write_type(out, &slice.inner);
1211 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001212 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001213 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001214 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001215 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001216 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001217 None => write!(out, "void"),
1218 }
1219 write!(out, "(");
1220 for (i, arg) in f.args.iter().enumerate() {
1221 if i > 0 {
1222 write!(out, ", ");
1223 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001224 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001225 }
1226 write!(out, ")>");
1227 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001228 Type::Array(a) => {
1229 write!(out, "::std::array<");
1230 write_type(out, &a.inner);
1231 write!(out, ", {}>", &a.len);
1232 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001233 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001234 }
1235}
1236
David Tolnay5d6429d2021-03-26 19:19:15 -04001237// Write just the T type behind a &T or &mut T or *const T or *mut T.
1238fn write_pointee_type(out: &mut OutFile, inner: &Type, mutable: bool) {
1239 if let Type::Ptr(_) = inner {
1240 write_type_space(out, inner);
1241 if !mutable {
1242 write!(out, "const");
1243 }
1244 } else {
1245 if !mutable {
1246 write!(out, "const ");
1247 }
1248 write_type(out, inner);
1249 }
1250}
1251
David Tolnayf6a89f22020-05-10 23:39:27 -07001252fn write_atom(out: &mut OutFile, atom: Atom) {
1253 match atom {
1254 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001255 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001256 U8 => write!(out, "::std::uint8_t"),
1257 U16 => write!(out, "::std::uint16_t"),
1258 U32 => write!(out, "::std::uint32_t"),
1259 U64 => write!(out, "::std::uint64_t"),
1260 Usize => write!(out, "::std::size_t"),
1261 I8 => write!(out, "::std::int8_t"),
1262 I16 => write!(out, "::std::int16_t"),
1263 I32 => write!(out, "::std::int32_t"),
1264 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001265 Isize => write!(out, "::rust::isize"),
1266 F32 => write!(out, "float"),
1267 F64 => write!(out, "double"),
1268 CxxString => write!(out, "::std::string"),
1269 RustString => write!(out, "::rust::String"),
1270 }
1271}
1272
David Tolnaya7c2ea12020-10-30 21:32:53 -07001273fn write_type_space(out: &mut OutFile, ty: &Type) {
1274 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001275 write_space_after_type(out, ty);
1276}
1277
1278fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001279 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001280 Type::Ident(_)
1281 | Type::RustBox(_)
1282 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001283 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001284 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001285 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001286 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001287 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001288 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001289 | Type::Fn(_)
1290 | Type::Array(_) => write!(out, " "),
Adrian Taylor38ae2282021-01-23 10:20:32 -08001291 Type::Ref(_) | Type::Ptr(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001292 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001293 }
1294}
1295
David Tolnay1bdb4712020-11-25 07:27:54 -08001296#[derive(Copy, Clone)]
1297enum UniquePtr<'a> {
David Tolnay3abed472020-12-31 23:34:53 -08001298 Ident(&'a Ident),
1299 CxxVector(&'a Ident),
David Tolnay1bdb4712020-11-25 07:27:54 -08001300}
1301
1302trait ToTypename {
1303 fn to_typename(&self, types: &Types) -> String;
1304}
1305
David Tolnay3abed472020-12-31 23:34:53 -08001306impl ToTypename for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001307 fn to_typename(&self, types: &Types) -> String {
David Tolnay1e5fe232021-01-01 18:11:40 -08001308 types.resolve(self).name.to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001309 }
1310}
1311
David Tolnay1bdb4712020-11-25 07:27:54 -08001312impl<'a> ToTypename for UniquePtr<'a> {
1313 fn to_typename(&self, types: &Types) -> String {
1314 match self {
1315 UniquePtr::Ident(ident) => ident.to_typename(types),
1316 UniquePtr::CxxVector(element) => {
1317 format!("::std::vector<{}>", element.to_typename(types))
1318 }
1319 }
1320 }
1321}
1322
1323trait ToMangled {
1324 fn to_mangled(&self, types: &Types) -> Symbol;
1325}
1326
David Tolnay3abed472020-12-31 23:34:53 -08001327impl ToMangled for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001328 fn to_mangled(&self, types: &Types) -> Symbol {
David Tolnay1e5fe232021-01-01 18:11:40 -08001329 types.resolve(self).name.to_symbol()
David Tolnay1bdb4712020-11-25 07:27:54 -08001330 }
1331}
1332
1333impl<'a> ToMangled for UniquePtr<'a> {
1334 fn to_mangled(&self, types: &Types) -> Symbol {
1335 match self {
1336 UniquePtr::Ident(ident) => ident.to_mangled(types),
1337 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1338 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001339 }
1340}
1341
David Tolnaya7c2ea12020-10-30 21:32:53 -07001342fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001343 if out.header {
1344 return;
1345 }
1346
1347 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001348 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001349 out.begin_block(Block::ExternC);
David Tolnay3abed472020-12-31 23:34:53 -08001350 for impl_key in out.types.impls.keys() {
1351 out.next_section();
David Tolnaydde63022021-03-26 22:22:35 -04001352 match *impl_key {
David Tolnay3abed472020-12-31 23:34:53 -08001353 ImplKey::RustBox(ident) => write_rust_box_extern(out, ident),
1354 ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident),
1355 ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident),
1356 ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident),
1357 ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident),
1358 ImplKey::CxxVector(ident) => write_cxx_vector(out, ident),
David Tolnay7db73692019-10-20 14:51:12 -04001359 }
1360 }
David Tolnay0c033e32020-11-01 15:15:48 -08001361 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001362
David Tolnay0c033e32020-11-01 15:15:48 -08001363 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001364 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3abed472020-12-31 23:34:53 -08001365 for impl_key in out.types.impls.keys() {
David Tolnaydde63022021-03-26 22:22:35 -04001366 match *impl_key {
David Tolnay3abed472020-12-31 23:34:53 -08001367 ImplKey::RustBox(ident) => write_rust_box_impl(out, ident),
1368 ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident),
1369 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -04001370 }
1371 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001372 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001373 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001374}
1375
David Tolnaydde63022021-03-26 22:22:35 -04001376fn write_rust_box_extern(out: &mut OutFile, key: NamedImplKey) {
1377 let ident = key.rust;
David Tolnay3abed472020-12-31 23:34:53 -08001378 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001379 let inner = resolve.name.to_fully_qualified();
1380 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001381
David Tolnay7db73692019-10-20 14:51:12 -04001382 writeln!(
1383 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001384 "{} *cxxbridge1$box${}$alloc() noexcept;",
1385 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001386 );
1387 writeln!(
1388 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001389 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1390 instance, inner,
1391 );
1392 writeln!(
1393 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001394 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001395 instance, inner,
1396 );
David Tolnay7db73692019-10-20 14:51:12 -04001397}
1398
David Tolnaydde63022021-03-26 22:22:35 -04001399fn write_rust_vec_extern(out: &mut OutFile, key: NamedImplKey) {
1400 let element = key.rust;
David Tolnay1bdb4712020-11-25 07:27:54 -08001401 let inner = element.to_typename(out.types);
1402 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001403
David Tolnay12320e12020-12-09 23:09:36 -08001404 out.include.cstddef = true;
1405
Myron Ahneba35cf2020-02-05 19:41:51 +07001406 writeln!(
1407 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001408 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001409 instance, inner,
1410 );
1411 writeln!(
1412 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001413 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001414 instance, inner,
1415 );
1416 writeln!(
1417 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001418 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001419 instance, inner,
1420 );
David Tolnay219c0792020-04-24 20:31:37 -07001421 writeln!(
1422 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001423 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001424 instance, inner,
1425 );
1426 writeln!(
1427 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001428 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001429 inner, instance,
1430 );
David Tolnay503d0192020-04-24 22:18:56 -07001431 writeln!(
1432 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001433 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001434 instance, inner,
1435 );
1436 writeln!(
1437 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001438 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001439 instance, inner,
1440 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001441}
1442
David Tolnaydde63022021-03-26 22:22:35 -04001443fn write_rust_box_impl(out: &mut OutFile, key: NamedImplKey) {
1444 let ident = key.rust;
David Tolnay3abed472020-12-31 23:34:53 -08001445 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001446 let inner = resolve.name.to_fully_qualified();
1447 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001448
1449 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001450 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001451 writeln!(
1452 out,
1453 "{} *Box<{}>::allocation::alloc() noexcept {{",
1454 inner, inner,
1455 );
David Tolnayc4b34222020-12-12 13:06:26 -08001456 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001457 writeln!(out, "}}");
1458
1459 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001460 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001461 writeln!(
1462 out,
David Tolnaye5703162020-12-12 16:26:35 -08001463 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001464 inner, inner,
1465 );
1466 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1467 writeln!(out, "}}");
1468
1469 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001470 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001471 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001472 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001473 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001474}
1475
David Tolnaydde63022021-03-26 22:22:35 -04001476fn write_rust_vec_impl(out: &mut OutFile, key: NamedImplKey) {
1477 let element = key.rust;
David Tolnay1bdb4712020-11-25 07:27:54 -08001478 let inner = element.to_typename(out.types);
1479 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001480
David Tolnay12320e12020-12-09 23:09:36 -08001481 out.include.cstddef = true;
1482
Myron Ahneba35cf2020-02-05 19:41:51 +07001483 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001484 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001485 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001486 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001487 writeln!(out, "}}");
1488
1489 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001490 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001491 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001492 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001493 writeln!(out, "}}");
1494
1495 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001496 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001497 writeln!(
1498 out,
1499 "::std::size_t Vec<{}>::size() const noexcept {{",
1500 inner,
1501 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001502 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001503 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001504
1505 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001506 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001507 writeln!(
1508 out,
1509 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1510 inner,
1511 );
David Tolnay381d9532020-12-11 13:59:45 -08001512 writeln!(
1513 out,
1514 " return cxxbridge1$rust_vec${}$capacity(this);",
1515 instance,
1516 );
David Tolnaydc62d712020-12-11 13:51:53 -08001517 writeln!(out, "}}");
1518
1519 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001520 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001521 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001522 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001523 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001524
1525 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001526 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001527 writeln!(
1528 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001529 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001530 inner,
1531 );
1532 writeln!(
1533 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001534 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001535 instance,
1536 );
1537 writeln!(out, "}}");
1538
1539 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001540 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001541 writeln!(
1542 out,
1543 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1544 inner,
1545 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001546 writeln!(
1547 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001548 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001549 instance,
1550 );
1551 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001552}
1553
David Tolnaydde63022021-03-26 22:22:35 -04001554fn write_unique_ptr(out: &mut OutFile, key: NamedImplKey) {
1555 let ty = UniquePtr::Ident(key.rust);
David Tolnay1bdb4712020-11-25 07:27:54 -08001556 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001557}
1558
1559// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001560fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001561 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001562 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001563 let inner = ty.to_typename(out.types);
1564 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001565
David Tolnay63da4d32020-04-25 09:41:12 -07001566 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001567 // Some aliases are to opaque types; some are to trivial types. We can't
1568 // know at code generation time, so we generate both C++ and Rust side
1569 // bindings for a "new" method anyway. But the Rust code can't be called
1570 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001571 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001572 out.types.structs.contains_key(ident)
1573 || out.types.enums.contains_key(ident)
1574 || out.types.aliases.contains_key(ident)
David Tolnayca0f9da2020-10-16 13:16:17 -07001575 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001576 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001577 };
1578
David Tolnay53462762020-11-25 07:08:10 -08001579 let conditional_delete = match ty {
1580 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001581 !out.types.structs.contains_key(ident) && !out.types.enums.contains_key(ident)
David Tolnay53462762020-11-25 07:08:10 -08001582 }
1583 UniquePtr::CxxVector(_) => false,
1584 };
1585
1586 if conditional_delete {
1587 out.builtin.is_complete = true;
1588 let definition = match ty {
David Tolnay1e5fe232021-01-01 18:11:40 -08001589 UniquePtr::Ident(ty) => &out.types.resolve(ty).name.cxx,
David Tolnay53462762020-11-25 07:08:10 -08001590 UniquePtr::CxxVector(_) => unreachable!(),
1591 };
1592 writeln!(
1593 out,
David Tolnay75068632020-12-26 22:15:17 -08001594 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001595 inner, definition,
1596 );
1597 }
David Tolnay7db73692019-10-20 14:51:12 -04001598 writeln!(
1599 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001600 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001601 inner,
1602 );
1603 writeln!(
1604 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001605 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001606 inner,
1607 );
1608 writeln!(
1609 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001610 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001611 instance, inner,
1612 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001613 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001614 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001615 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001616 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001617 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001618 out,
David Tolnay0b881402020-12-01 21:05:08 -08001619 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1620 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001621 );
David Tolnay63da4d32020-04-25 09:41:12 -07001622 writeln!(
1623 out,
David Tolnay0b881402020-12-01 21:05:08 -08001624 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1625 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001626 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001627 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001628 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001629 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001630 }
David Tolnay7db73692019-10-20 14:51:12 -04001631 writeln!(
1632 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001633 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001634 instance, inner, inner,
1635 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001636 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001637 writeln!(out, "}}");
1638 writeln!(
1639 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001640 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001641 inner, instance, inner,
1642 );
1643 writeln!(out, " return ptr.get();");
1644 writeln!(out, "}}");
1645 writeln!(
1646 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001647 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001648 inner, instance, inner,
1649 );
1650 writeln!(out, " return ptr.release();");
1651 writeln!(out, "}}");
1652 writeln!(
1653 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001654 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001655 instance, inner,
1656 );
David Tolnay53462762020-11-25 07:08:10 -08001657 if conditional_delete {
1658 out.builtin.deleter_if = true;
1659 writeln!(
1660 out,
David Tolnay75068632020-12-26 22:15:17 -08001661 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001662 inner,
1663 );
1664 } else {
1665 writeln!(out, " ptr->~unique_ptr();");
1666 }
David Tolnay7db73692019-10-20 14:51:12 -04001667 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001668}
Myron Ahneba35cf2020-02-05 19:41:51 +07001669
David Tolnaydde63022021-03-26 22:22:35 -04001670fn write_shared_ptr(out: &mut OutFile, key: NamedImplKey) {
1671 let ident = key.rust;
David Tolnay95bc57f2021-01-01 13:29:44 -08001672 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001673 let inner = resolve.name.to_fully_qualified();
1674 let instance = resolve.name.to_symbol();
David Tolnayb3b24a12020-12-01 15:27:43 -08001675
David Tolnayb3b24a12020-12-01 15:27:43 -08001676 out.include.new = true;
1677 out.include.utility = true;
1678
1679 // Some aliases are to opaque types; some are to trivial types. We can't
1680 // know at code generation time, so we generate both C++ and Rust side
1681 // bindings for a "new" method anyway. But the Rust code can't be called for
1682 // Opaque types because the 'new' method is not implemented.
David Tolnay3abed472020-12-31 23:34:53 -08001683 let can_construct_from_value = out.types.structs.contains_key(ident)
1684 || out.types.enums.contains_key(ident)
1685 || out.types.aliases.contains_key(ident);
David Tolnayb3b24a12020-12-01 15:27:43 -08001686
David Tolnayb3b24a12020-12-01 15:27:43 -08001687 writeln!(
1688 out,
1689 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1690 inner,
1691 );
1692 writeln!(
1693 out,
1694 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1695 inner,
1696 );
1697 writeln!(
1698 out,
1699 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1700 instance, inner,
1701 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001702 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001703 writeln!(out, "}}");
1704 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001705 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001706 writeln!(
1707 out,
David Tolnay0b881402020-12-01 21:05:08 -08001708 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1709 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001710 );
1711 writeln!(
1712 out,
David Tolnay0b881402020-12-01 21:05:08 -08001713 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1714 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001715 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001716 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001717 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001718 writeln!(out, "}}");
1719 }
1720 writeln!(
1721 out,
1722 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1723 instance, inner, inner,
1724 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001725 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001726 writeln!(out, "}}");
1727 writeln!(
1728 out,
1729 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1730 inner, instance, inner,
1731 );
1732 writeln!(out, " return self.get();");
1733 writeln!(out, "}}");
1734 writeln!(
1735 out,
1736 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1737 instance, inner,
1738 );
1739 writeln!(out, " self->~shared_ptr();");
1740 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001741}
1742
David Tolnaydde63022021-03-26 22:22:35 -04001743fn write_weak_ptr(out: &mut OutFile, key: NamedImplKey) {
1744 let ident = key.rust;
David Tolnay95bc57f2021-01-01 13:29:44 -08001745 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001746 let inner = resolve.name.to_fully_qualified();
1747 let instance = resolve.name.to_symbol();
David Tolnay215e77f2020-12-28 17:09:48 -08001748
1749 out.include.new = true;
1750 out.include.utility = true;
1751
1752 writeln!(
1753 out,
1754 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1755 inner,
1756 );
1757 writeln!(
1758 out,
1759 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1760 inner,
1761 );
1762 writeln!(
1763 out,
1764 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1765 instance, inner,
1766 );
1767 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1768 writeln!(out, "}}");
1769 writeln!(
1770 out,
1771 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1772 instance, inner, inner,
1773 );
1774 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1775 writeln!(out, "}}");
1776 writeln!(
1777 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001778 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1779 instance, inner, inner,
1780 );
1781 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1782 writeln!(out, "}}");
1783 writeln!(
1784 out,
David Tolnay7a487852020-12-28 18:02:25 -08001785 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1786 instance, inner, inner,
1787 );
1788 writeln!(
1789 out,
1790 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1791 inner,
1792 );
1793 writeln!(out, "}}");
1794 writeln!(
1795 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001796 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1797 instance, inner,
1798 );
1799 writeln!(out, " self->~weak_ptr();");
1800 writeln!(out, "}}");
1801}
1802
David Tolnaydde63022021-03-26 22:22:35 -04001803fn write_cxx_vector(out: &mut OutFile, key: NamedImplKey) {
1804 let element = key.rust;
David Tolnay1bdb4712020-11-25 07:27:54 -08001805 let inner = element.to_typename(out.types);
1806 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001807
David Tolnay12320e12020-12-09 23:09:36 -08001808 out.include.cstddef = true;
1809
Myron Ahneba35cf2020-02-05 19:41:51 +07001810 writeln!(
1811 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001812 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001813 instance, inner,
1814 );
1815 writeln!(out, " return s.size();");
1816 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001817 writeln!(
1818 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001819 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001820 inner, instance, inner,
1821 );
David Tolnay767e00d2020-12-21 17:12:27 -08001822 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001823 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001824
David Tolnay70820672020-12-07 11:15:14 -08001825 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001826 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001827}