blob: d2dbe025cb0ce3a326699a6e7e6390463e19fb40 [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 Tolnaye352c1e2020-12-31 16:41:05 -08006use crate::syntax::map::UnorderedMap as Map;
7use crate::syntax::set::UnorderedSet;
David Tolnay891061b2020-04-19 22:42:33 -07008use crate::syntax::symbol::Symbol;
David Tolnay5b1d8632020-12-20 22:05:11 -08009use crate::syntax::trivial::{self, TrivialReason};
Adrian Taylorc8713432020-10-21 18:20:55 -070010use crate::syntax::{
David Tolnay75ea17c2020-12-06 21:08:34 -080011 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, RustName, Signature, Struct, Trait,
David Tolnay5b1d8632020-12-20 22:05:11 -080012 Type, TypeAlias, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070013};
David Tolnay7db73692019-10-20 14:51:12 -040014use proc_macro2::Ident;
15
David Tolnayac7188c2020-11-01 16:58:16 -080016pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080017 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040018 let out = &mut out_file;
19
David Tolnaydfb82d72020-11-02 00:10:10 -080020 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070021 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040022
David Tolnay7b0e5102020-11-01 23:22:12 -080023 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080024 write_data_structures(out, apis);
25 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080026 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040027
David Tolnay3374d8d2020-10-31 22:18:45 -070028 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070029 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070030
David Tolnayac7188c2020-11-01 16:58:16 -080031 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070032}
33
David Tolnay7b0e5102020-11-01 23:22:12 -080034fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
35 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080036 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080037 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080038 _ => false,
39 };
Adrian Taylorc8713432020-10-21 18:20:55 -070040
David Tolnay7b0e5102020-11-01 23:22:12 -080041 let apis_by_namespace =
42 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
43
David Tolnayd920be52020-11-01 23:34:30 -080044 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080045
David Tolnayd920be52020-11-01 23:34:30 -080046 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080047 let apis = ns_entries.direct_content();
48
49 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080050 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080051 match api {
David Tolnay17a934c2020-11-02 00:40:04 -080052 Api::Struct(strct) => write_struct_decl(out, &strct.name.cxx),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080053 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080054 Api::CxxType(ety) => write_struct_using(out, &ety.name),
55 Api::RustType(ety) => write_struct_decl(out, &ety.name.cxx),
David Tolnay7b0e5102020-11-01 23:22:12 -080056 _ => unreachable!(),
57 }
David Tolnay7db73692019-10-20 14:51:12 -040058 }
David Tolnay7db73692019-10-20 14:51:12 -040059
David Tolnay7b0e5102020-11-01 23:22:12 -080060 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080061 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
62 write(out, nested_ns_entries, indent + 2);
63 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080064 }
Adrian Taylorf9213622020-10-31 22:25:42 -070065 }
66}
67
David Tolnaye1109d92020-11-01 20:51:56 -080068fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnaye352c1e2020-12-31 16:41:05 -080069 let mut methods_for_type = Map::new();
David Tolnay630af882020-10-31 22:03:47 -070070 for api in apis {
David Tolnay102c7ea2020-11-08 18:58:09 -080071 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayf94bef12020-04-17 14:46:42 -070072 if let Some(receiver) = &efn.sig.receiver {
73 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070074 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070075 .or_insert_with(Vec::new)
76 .push(efn);
77 }
78 }
79 }
Joel Galenson968738f2020-04-15 14:19:33 -070080
David Tolnaye352c1e2020-12-31 16:41:05 -080081 let mut structs_written = UnorderedSet::new();
David Tolnay5439fa12020-11-03 18:45:01 -080082 let mut toposorted_structs = out.types.toposorted_structs.iter();
David Tolnay9622b462020-11-02 21:34:42 -080083 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070084 match api {
David Tolnay5439fa12020-11-03 18:45:01 -080085 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
86 for next in &mut toposorted_structs {
87 if !out.types.cxx.contains(&strct.name.rust) {
88 out.next_section();
David Tolnay102c7ea2020-11-08 18:58:09 -080089 let methods = methods_for_type
90 .get(&strct.name.rust)
91 .map(Vec::as_slice)
92 .unwrap_or_default();
93 write_struct(out, next, methods);
David Tolnay5439fa12020-11-03 18:45:01 -080094 }
95 structs_written.insert(&next.name.rust);
96 if next.name.rust == strct.name.rust {
97 break;
98 }
99 }
100 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700101 Api::Enum(enm) => {
102 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800103 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700104 check_enum(out, enm);
105 } else {
106 write_enum(out, enm);
107 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700108 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700109 Api::RustType(ety) => {
David Tolnay8d067de2020-12-26 16:18:23 -0800110 out.next_section();
111 let methods = methods_for_type
112 .get(&ety.name.rust)
113 .map(Vec::as_slice)
114 .unwrap_or_default();
115 write_opaque_type(out, ety, methods);
David Tolnayc1fe0052020-04-17 15:15:06 -0700116 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700117 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400118 }
119 }
120
David Tolnayfabca772020-10-03 21:25:41 -0700121 out.next_section();
122 for api in apis {
123 if let Api::TypeAlias(ety) = api {
Adrian Taylorf831a5a2020-12-07 16:49:19 -0800124 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
David Tolnay5b1d8632020-12-20 22:05:11 -0800125 check_trivial_extern_type(out, ety, reasons)
David Tolnayfabca772020-10-03 21:25:41 -0700126 }
127 }
128 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800129}
130
David Tolnaye1109d92020-11-01 20:51:56 -0800131fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700132 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400133 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800134 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800135 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800136 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800137 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
138 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
139 _ => {}
140 }
David Tolnay7db73692019-10-20 14:51:12 -0400141 }
David Tolnay7da38202020-11-27 17:36:16 -0800142
143 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400144 }
145
146 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800147 match api {
148 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800149 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800150 Api::RustFunction(efn) => {
151 out.next_section();
152 write_rust_function_shim(out, efn);
153 }
154 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400155 }
156 }
David Tolnay7db73692019-10-20 14:51:12 -0400157}
158
David Tolnay7da38202020-11-27 17:36:16 -0800159fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
160 out.set_namespace(Default::default());
161 out.begin_block(Block::Namespace("std"));
162
163 for api in apis {
164 if let Api::Struct(strct) = api {
165 if derive::contains(&strct.derives, Trait::Hash) {
166 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800167 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800168 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800169 let qualified = strct.name.to_fully_qualified();
170 writeln!(out, "template <> struct hash<{}> {{", qualified);
171 writeln!(
172 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800173 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800174 qualified,
175 );
176 let link_name = mangle::operator(&strct.name, "hash");
177 write!(out, " return ::");
178 for name in &strct.name.namespace {
179 write!(out, "{}::", name);
180 }
181 writeln!(out, "{}(self);", link_name);
182 writeln!(out, " }}");
183 writeln!(out, "}};");
184 }
185 }
186 }
187
188 out.end_block(Block::Namespace("std"));
189}
190
David Tolnaydfb82d72020-11-02 00:10:10 -0800191fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
192 for api in apis {
193 if let Api::Include(include) = api {
194 out.include.insert(include);
195 }
196 }
197
David Tolnaya7c2ea12020-10-30 21:32:53 -0700198 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400199 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700200 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700201 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
202 | Some(I64) => out.include.cstdint = true,
203 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800204 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700205 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800206 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800207 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700208 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800209 Type::RustBox(_) => out.builtin.rust_box = true,
210 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700211 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay215e77f2020-12-28 17:09:48 -0800212 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800213 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700214 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800215 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800216 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800217 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800218 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400219 }
220 }
David Tolnayec66d112020-10-31 21:00:26 -0700221}
David Tolnayf51447e2020-03-06 14:14:27 -0800222
David Tolnay102c7ea2020-11-08 18:58:09 -0800223fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800224 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800225 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800226
David Tolnay17a934c2020-11-02 00:40:04 -0800227 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800228 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700229 writeln!(out, "#ifndef {}", guard);
230 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400231 for line in strct.doc.to_string().lines() {
232 writeln!(out, "//{}", line);
233 }
David Tolnay17a934c2020-11-02 00:40:04 -0800234 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800235
David Tolnay7db73692019-10-20 14:51:12 -0400236 for field in &strct.fields {
David Tolnay5573e522020-12-31 11:07:38 -0800237 for line in field.doc.to_string().lines() {
238 writeln!(out, " //{}", line);
239 }
David Tolnay7db73692019-10-20 14:51:12 -0400240 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700241 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400242 writeln!(out, "{};", field.ident);
243 }
David Tolnay84389352020-11-27 17:12:10 -0800244
David Tolnay5554ebd2020-12-10 11:34:41 -0800245 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800246
David Tolnay102c7ea2020-11-08 18:58:09 -0800247 for method in methods {
248 write!(out, " ");
249 let sig = &method.sig;
250 let local_name = method.name.cxx.to_string();
251 write_rust_function_shim_decl(out, &local_name, sig, false);
252 writeln!(out, ";");
253 }
David Tolnay84389352020-11-27 17:12:10 -0800254
David Tolnayb960ed22020-11-27 14:34:30 -0800255 if operator_eq {
256 writeln!(
257 out,
258 " bool operator==(const {} &) const noexcept;",
259 strct.name.cxx,
260 );
261 writeln!(
262 out,
263 " bool operator!=(const {} &) const noexcept;",
264 strct.name.cxx,
265 );
266 }
David Tolnay84389352020-11-27 17:12:10 -0800267
268 if operator_ord {
269 writeln!(
270 out,
271 " bool operator<(const {} &) const noexcept;",
272 strct.name.cxx,
273 );
274 writeln!(
275 out,
276 " bool operator<=(const {} &) const noexcept;",
277 strct.name.cxx,
278 );
279 writeln!(
280 out,
281 " bool operator>(const {} &) const noexcept;",
282 strct.name.cxx,
283 );
284 writeln!(
285 out,
286 " bool operator>=(const {} &) const noexcept;",
287 strct.name.cxx,
288 );
289 }
290
David Tolnay5554ebd2020-12-10 11:34:41 -0800291 out.include.type_traits = true;
292 writeln!(out, " using IsRelocatable = ::std::true_type;");
293
David Tolnay7db73692019-10-20 14:51:12 -0400294 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700295 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400296}
297
298fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
299 writeln!(out, "struct {};", ident);
300}
301
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800302fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800303 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800304 write_atom(out, enm.repr);
305 writeln!(out, ";");
306}
307
David Tolnay8faec772020-11-02 00:18:19 -0800308fn write_struct_using(out: &mut OutFile, ident: &Pair) {
309 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800310}
311
David Tolnay8d067de2020-12-26 16:18:23 -0800312fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800313 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800314 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700315 writeln!(out, "#ifndef {}", guard);
316 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700317 for line in ety.doc.to_string().lines() {
318 writeln!(out, "//{}", line);
319 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800320
David Tolnay365fc7c2020-11-25 16:08:13 -0800321 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800322 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800323 out,
324 "struct {} final : public ::rust::Opaque {{",
325 ety.name.cxx,
326 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800327
Joel Galenson968738f2020-04-15 14:19:33 -0700328 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800329 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700330 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800331 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700332 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800333 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800334 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800335
David Tolnay8d067de2020-12-26 16:18:23 -0800336 if !methods.is_empty() {
337 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700338 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800339
David Tolnayee6ecfc2020-12-26 21:54:37 -0800340 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800341 out.include.cstddef = true;
342 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800343 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800344 writeln!(out, " struct layout {{");
345 writeln!(out, " static ::std::size_t size() noexcept;");
346 writeln!(out, " static ::std::size_t align() noexcept;");
347 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700348 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700349 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700350}
351
David Tolnay0b9b9f82020-11-01 20:41:00 -0800352fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800353 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800354 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700355 writeln!(out, "#ifndef {}", guard);
356 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700357 for line in enm.doc.to_string().lines() {
358 writeln!(out, "//{}", line);
359 }
David Tolnay17a934c2020-11-02 00:40:04 -0800360 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700361 write_atom(out, enm.repr);
362 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700363 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800364 for line in variant.doc.to_string().lines() {
365 writeln!(out, " //{}", line);
366 }
David Tolnaye6f62142020-12-21 16:00:41 -0800367 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700368 }
369 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700370 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700371}
372
David Tolnay0b9b9f82020-11-01 20:41:00 -0800373fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800374 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800375 out.include.type_traits = true;
376 writeln!(
377 out,
378 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
379 enm.name.cxx,
380 );
David Tolnay17a934c2020-11-02 00:40:04 -0800381 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700382 write_atom(out, enm.repr);
383 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700384 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700385 write!(out, "static_assert(static_cast<");
386 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700387 writeln!(
388 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700389 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800390 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700391 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700392 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700393}
394
David Tolnay5b1d8632020-12-20 22:05:11 -0800395fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800396 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700397 // necessary for soundness. That's because triviality is always declared by
398 // the user in the form of an unsafe impl of cxx::ExternType:
399 //
400 // unsafe impl ExternType for MyType {
401 // type Id = cxx::type_id!("...");
402 // type Kind = cxx::kind::Trivial;
403 // }
404 //
405 // Since the user went on the record with their unsafe impl to unsafely
406 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800407 // them if that were wrong. However, in practice correctly reasoning about
408 // the relocatability of C++ types is challenging, particularly if the type
409 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700410 //
David Tolnay174bd952020-11-02 09:23:12 -0800411 // There may be legitimate reasons to opt out of this assertion for support
412 // of types that the programmer knows are soundly Rust-movable despite not
413 // being recognized as such by the C++ type system due to a move constructor
414 // or destructor. To opt out of the relocatability check, they need to do
415 // one of the following things in any header used by `include!` in their
416 // bridge.
417 //
418 // --- if they define the type:
419 // struct MyType {
420 // ...
421 // + using IsRelocatable = std::true_type;
422 // };
423 //
424 // --- otherwise:
425 // + template <>
426 // + struct rust::IsRelocatable<MyType> : std::true_type {};
427 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700428
David Tolnay5b1d8632020-12-20 22:05:11 -0800429 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800430 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800431 writeln!(out, "static_assert(");
432 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
433 writeln!(
434 out,
435 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
436 id.trim_start_matches("::"),
437 trivial::as_what(&alias.name, reasons),
438 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700439}
440
David Tolnayb960ed22020-11-27 14:34:30 -0800441fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
442 out.set_namespace(&strct.name.namespace);
443 out.begin_block(Block::ExternC);
444
445 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800446 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800447 writeln!(
448 out,
449 "bool {}(const {1} &, const {1} &) noexcept;",
450 link_name, strct.name.cxx,
451 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800452
453 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800454 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800455 writeln!(
456 out,
457 "bool {}(const {1} &, const {1} &) noexcept;",
458 link_name, strct.name.cxx,
459 );
460 }
David Tolnayb960ed22020-11-27 14:34:30 -0800461 }
462
David Tolnay84389352020-11-27 17:12:10 -0800463 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800464 let link_name = mangle::operator(&strct.name, "lt");
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
David Tolnaya05f9402020-11-27 17:44:05 -0800471 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800472 writeln!(
473 out,
474 "bool {}(const {1} &, const {1} &) noexcept;",
475 link_name, strct.name.cxx,
476 );
477
478 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800479 let link_name = mangle::operator(&strct.name, "gt");
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
David Tolnaya05f9402020-11-27 17:44:05 -0800486 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800487 writeln!(
488 out,
489 "bool {}(const {1} &, const {1} &) noexcept;",
490 link_name, strct.name.cxx,
491 );
492 }
493 }
494
David Tolnay7da38202020-11-27 17:36:16 -0800495 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800496 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800497 let link_name = mangle::operator(&strct.name, "hash");
498 writeln!(
499 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800500 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800501 link_name, strct.name.cxx,
502 );
503 }
504
David Tolnayb960ed22020-11-27 14:34:30 -0800505 out.end_block(Block::ExternC);
506}
507
508fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
509 if out.header {
510 return;
511 }
512
513 out.set_namespace(&strct.name.namespace);
514
515 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800516 out.next_section();
517 writeln!(
518 out,
519 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
520 strct.name.cxx,
521 );
David Tolnaya05f9402020-11-27 17:44:05 -0800522 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800523 writeln!(out, " return {}(*this, rhs);", link_name);
524 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800525
David Tolnayb960ed22020-11-27 14:34:30 -0800526 out.next_section();
527 writeln!(
528 out,
529 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
530 strct.name.cxx,
531 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800532 if derive::contains(&strct.derives, Trait::Eq) {
533 writeln!(out, " return !(*this == rhs);");
534 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800535 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800536 writeln!(out, " return {}(*this, rhs);", link_name);
537 }
David Tolnayb960ed22020-11-27 14:34:30 -0800538 writeln!(out, "}}");
539 }
David Tolnay84389352020-11-27 17:12:10 -0800540
541 if derive::contains(&strct.derives, Trait::PartialOrd) {
542 out.next_section();
543 writeln!(
544 out,
545 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
546 strct.name.cxx,
547 );
David Tolnaya05f9402020-11-27 17:44:05 -0800548 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800549 writeln!(out, " return {}(*this, rhs);", link_name);
550 writeln!(out, "}}");
551
552 out.next_section();
553 writeln!(
554 out,
555 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
556 strct.name.cxx,
557 );
David Tolnaya05f9402020-11-27 17:44:05 -0800558 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800559 writeln!(out, " return {}(*this, rhs);", link_name);
560 writeln!(out, "}}");
561
562 out.next_section();
563 writeln!(
564 out,
565 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
566 strct.name.cxx,
567 );
568 if derive::contains(&strct.derives, Trait::Ord) {
569 writeln!(out, " return !(*this <= rhs);");
570 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800571 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800572 writeln!(out, " return {}(*this, rhs);", link_name);
573 }
574 writeln!(out, "}}");
575
576 out.next_section();
577 writeln!(
578 out,
579 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
580 strct.name.cxx,
581 );
582 if derive::contains(&strct.derives, Trait::Ord) {
583 writeln!(out, " return !(*this < rhs);");
584 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800585 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800586 writeln!(out, " return {}(*this, rhs);", link_name);
587 }
588 writeln!(out, "}}");
589 }
David Tolnayb960ed22020-11-27 14:34:30 -0800590}
591
David Tolnay358bc4b2020-12-26 22:37:57 -0800592fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
593 out.set_namespace(&ety.name.namespace);
594 out.begin_block(Block::ExternC);
595
596 let link_name = mangle::operator(&ety.name, "sizeof");
597 writeln!(out, "::std::size_t {}() noexcept;", link_name);
598
599 let link_name = mangle::operator(&ety.name, "alignof");
600 writeln!(out, "::std::size_t {}() noexcept;", link_name);
601
602 out.end_block(Block::ExternC);
603}
604
605fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
606 if out.header {
607 return;
608 }
609
610 out.set_namespace(&ety.name.namespace);
611
612 out.next_section();
613 let link_name = mangle::operator(&ety.name, "sizeof");
614 writeln!(
615 out,
616 "::std::size_t {}::layout::size() noexcept {{",
617 ety.name.cxx,
618 );
619 writeln!(out, " return {}();", link_name);
620 writeln!(out, "}}");
621
622 out.next_section();
623 let link_name = mangle::operator(&ety.name, "alignof");
624 writeln!(
625 out,
626 "::std::size_t {}::layout::align() noexcept {{",
627 ety.name.cxx,
628 );
629 writeln!(out, " return {}();", link_name);
630 writeln!(out, "}}");
631}
632
David Tolnay1eadd262020-12-29 16:42:08 -0800633fn begin_function_definition(out: &mut OutFile) {
634 if let Some(annotation) = &out.opt.cxx_impl_annotations {
635 write!(out, "{} ", annotation);
636 }
637}
638
David Tolnay0b9b9f82020-11-01 20:41:00 -0800639fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800640 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800641 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800642 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800643 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700644 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700645 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700646 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700647 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700648 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700649 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700650 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700651 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700652 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800653 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700654 write!(out, "const ");
655 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700656 write!(
657 out,
658 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800659 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700660 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700661 }
David Tolnay7db73692019-10-20 14:51:12 -0400662 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700663 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400664 write!(out, ", ");
665 }
David Tolnaya46a2372020-03-06 10:03:48 -0800666 if arg.ty == RustString {
667 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700668 } else if let Type::RustVec(_) = arg.ty {
669 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800670 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700671 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400672 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700673 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400674 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700675 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400676 write!(out, ", ");
677 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700678 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400679 write!(out, "*return$");
680 }
681 writeln!(out, ") noexcept {{");
682 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700683 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700684 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800685 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700686 Some(receiver) => write!(
687 out,
688 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700689 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800690 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700691 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700692 }
David Tolnay7db73692019-10-20 14:51:12 -0400693 for (i, arg) in efn.args.iter().enumerate() {
694 if i > 0 {
695 write!(out, ", ");
696 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700697 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400698 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700699 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700700 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800701 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700702 write!(out, " const");
703 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700704 }
705 write!(out, " = ");
706 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800707 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700708 Some(receiver) => write!(
709 out,
710 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700711 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800712 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700713 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700714 }
715 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400716 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700717 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700718 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700719 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700720 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700721 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700722 writeln!(out, " [&] {{");
723 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700724 }
David Tolnay7db73692019-10-20 14:51:12 -0400725 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700726 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400727 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700728 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400729 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700730 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400731 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700732 }
733 match &efn.ret {
734 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700735 Some(Type::Str(_)) if !indirect_return => {
736 out.builtin.rust_str_repr = true;
737 write!(out, "::rust::impl<::rust::Str>::repr(");
738 }
David Tolnay73b72642020-11-25 17:44:05 -0800739 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700740 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800741 write!(out, "::rust::impl<");
742 write_type(out, ty);
743 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700744 }
David Tolnay99642622020-03-25 13:07:35 -0700745 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400746 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700747 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800748 None => write!(out, "{}$(", efn.name.rust),
749 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700750 }
David Tolnay7db73692019-10-20 14:51:12 -0400751 for (i, arg) in efn.args.iter().enumerate() {
752 if i > 0 {
753 write!(out, ", ");
754 }
755 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700756 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400757 write!(out, "::from_raw({})", arg.ident);
758 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700759 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400760 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700761 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700762 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700763 write!(
764 out,
765 "::rust::impl<::rust::Str>::new_unchecked({})",
766 arg.ident,
767 );
David Tolnaya46a2372020-03-06 10:03:48 -0800768 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700769 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800770 write!(
771 out,
772 "::rust::String(::rust::unsafe_bitcopy, *{})",
773 arg.ident,
774 );
David Tolnay313b10e2020-04-25 16:30:51 -0700775 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700776 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700777 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700778 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800779 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800780 write_type(out, &arg.ty);
781 write!(out, "(static_cast<");
782 if slice.mutability.is_none() {
783 write!(out, "const ");
784 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800785 write_type_space(out, &slice.inner);
786 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700787 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700788 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800789 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400790 } else {
791 write!(out, "{}", arg.ident);
792 }
793 }
794 write!(out, ")");
795 match &efn.ret {
796 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
797 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800798 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400799 _ => {}
800 }
801 if indirect_return {
802 write!(out, ")");
803 }
804 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700805 if efn.throws {
806 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800807 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700808 writeln!(out, " throw$.ptr = nullptr;");
809 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700810 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700811 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700812 writeln!(
813 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800814 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700815 );
David Tolnay5d121442020-03-17 22:14:40 -0700816 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700817 writeln!(out, " return throw$;");
818 }
David Tolnay7db73692019-10-20 14:51:12 -0400819 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700820 for arg in &efn.args {
821 if let Type::Fn(f) = &arg.ty {
822 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700823 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700824 }
825 }
David Tolnayca563ee2020-11-01 20:12:27 -0800826 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700827}
828
829fn write_function_pointer_trampoline(
830 out: &mut OutFile,
831 efn: &ExternFn,
832 var: &Ident,
833 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700834) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700835 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700836 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700837 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700838
839 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700840 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
841 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400842}
843
David Tolnay0b9b9f82020-11-01 20:41:00 -0800844fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800845 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800846 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700847 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700848 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700849 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800850 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700851}
852
853fn write_rust_function_decl_impl(
854 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700855 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700856 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700857 indirect_call: bool,
858) {
David Tolnay04770742020-11-01 13:50:50 -0800859 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700860 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700861 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700862 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700863 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700864 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700865 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700866 write!(out, "{}(", link_name);
867 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700868 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800869 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700870 write!(out, "const ");
871 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700872 write!(
873 out,
874 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800875 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700876 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700877 needs_comma = true;
878 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700879 for arg in &sig.args {
880 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400881 write!(out, ", ");
882 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700883 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700884 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400885 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700886 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700887 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400888 write!(out, ", ");
889 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700890 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400891 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700892 needs_comma = true;
893 }
894 if indirect_call {
895 if needs_comma {
896 write!(out, ", ");
897 }
898 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400899 }
900 writeln!(out, ") noexcept;");
901}
902
David Tolnay0b9b9f82020-11-01 20:41:00 -0800903fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800904 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400905 for line in efn.doc.to_string().lines() {
906 writeln!(out, "//{}", line);
907 }
David Tolnaya73853b2020-04-20 01:19:56 -0700908 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800909 None => efn.name.cxx.to_string(),
910 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700911 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700912 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700913 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700914 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700915}
916
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700917fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700918 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700919 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700920 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700921 indirect_call: bool,
922) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700923 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700924 write!(out, "{}(", local_name);
925 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400926 if i > 0 {
927 write!(out, ", ");
928 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700929 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400930 write!(out, "{}", arg.ident);
931 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700932 if indirect_call {
933 if !sig.args.is_empty() {
934 write!(out, ", ");
935 }
936 write!(out, "void *extern$");
937 }
David Tolnay1e548172020-03-16 13:37:09 -0700938 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700939 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800940 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700941 write!(out, " const");
942 }
943 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700944 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700945 write!(out, " noexcept");
946 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700947}
948
949fn write_rust_function_shim_impl(
950 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700951 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700952 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700953 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700954 indirect_call: bool,
955) {
956 if out.header && sig.receiver.is_some() {
957 // We've already defined this inside the struct.
958 return;
959 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800960 if !out.header {
961 begin_function_definition(out);
962 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700963 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400964 if out.header {
965 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700966 return;
David Tolnay7db73692019-10-20 14:51:12 -0400967 }
David Tolnay439cde22020-04-20 00:46:25 -0700968 writeln!(out, " {{");
969 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700970 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700971 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700972 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700973 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700974 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700975 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
976 }
977 }
978 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700979 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700980 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700981 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700982 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700983 write_type(out, sig.ret.as_ref().unwrap());
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 Tolnay74d6d512020-10-31 22:22:03 -0700998 Type::Str(_) => {
999 out.builtin.rust_str_new_unchecked = true;
1000 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
1001 }
David Tolnay73b72642020-11-25 17:44:05 -08001002 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001003 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001004 write!(out, "::rust::impl<");
1005 write_type(out, ret);
1006 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001007 }
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 }
1025 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -07001026 Type::Str(_) => {
1027 out.builtin.rust_str_repr = true;
1028 write!(out, "::rust::impl<::rust::Str>::repr(");
1029 }
David Tolnay73b72642020-11-25 17:44:05 -08001030 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001031 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001032 write!(out, "::rust::impl<");
1033 write_type(out, &arg.ty);
1034 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001035 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001036 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -07001037 _ => {}
1038 }
1039 write!(out, "{}", arg.ident);
1040 match &arg.ty {
1041 Type::RustBox(_) => write!(out, ".into_raw()"),
1042 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -08001043 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001044 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001045 _ => {}
1046 }
David Tolnay9d840362020-11-10 08:50:40 -08001047 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001048 }
1049 if indirect_return {
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, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001054 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001055 }
1056 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001057 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001058 write!(out, ", ");
1059 }
1060 write!(out, "extern$");
1061 }
1062 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001063 if !indirect_return {
1064 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -08001065 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001066 write!(out, ")");
1067 }
David Tolnay439cde22020-04-20 00:46:25 -07001068 }
1069 }
1070 writeln!(out, ";");
1071 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001072 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001073 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001074 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001075 writeln!(out, " }}");
1076 }
1077 if indirect_return {
1078 out.include.utility = true;
1079 writeln!(out, " return ::std::move(return$.value);");
1080 }
1081 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001082}
1083
David Tolnaya7c2ea12020-10-30 21:32:53 -07001084fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001085 match ty {
1086 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001087 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001088 }
1089}
1090
David Tolnay75dca2e2020-03-25 20:17:52 -07001091fn indirect_return(sig: &Signature, types: &Types) -> bool {
1092 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001093 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001094 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001095}
1096
David Tolnaya7c2ea12020-10-30 21:32:53 -07001097fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001098 match ty {
1099 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001100 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001101 write!(out, "*");
1102 }
1103 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001104 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001105 write!(out, "const ");
1106 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001107 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001108 write!(out, " *");
1109 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001110 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001111 }
1112}
1113
David Tolnaya7c2ea12020-10-30 21:32:53 -07001114fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1115 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001116 match ty {
1117 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001118 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001119 _ => write_space_after_type(out, ty),
1120 }
1121}
1122
David Tolnaya7c2ea12020-10-30 21:32:53 -07001123fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001124 match ty {
1125 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001126 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001127 write!(out, "*");
1128 }
David Tolnay4a441222020-01-25 16:24:27 -08001129 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001130 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001131 write!(out, "const ");
1132 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001133 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001134 write!(out, " *");
1135 }
David Tolnay73b72642020-11-25 17:44:05 -08001136 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001137 out.builtin.ptr_len = true;
1138 write!(out, "::rust::repr::PtrLen ");
1139 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001140 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1141 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001142 }
1143}
1144
David Tolnaya7c2ea12020-10-30 21:32:53 -07001145fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001146 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001147 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001148 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001149 write!(out, "*");
1150 }
David Tolnay73b72642020-11-25 17:44:05 -08001151 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001152 out.builtin.ptr_len = true;
1153 write!(out, "::rust::repr::PtrLen ");
1154 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001155 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001156 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001157 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001158 write!(out, "*");
1159 }
1160 write!(out, "{}", arg.ident);
1161}
1162
David Tolnaya7c2ea12020-10-30 21:32:53 -07001163fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001164 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001165 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001166 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001167 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001168 },
1169 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001170 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001171 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001172 write!(out, ">");
1173 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001174 Type::RustVec(ty) => {
1175 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001176 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001177 write!(out, ">");
1178 }
David Tolnay7db73692019-10-20 14:51:12 -04001179 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001180 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001181 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001182 write!(out, ">");
1183 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001184 Type::SharedPtr(ptr) => {
1185 write!(out, "::std::shared_ptr<");
1186 write_type(out, &ptr.inner);
1187 write!(out, ">");
1188 }
David Tolnay215e77f2020-12-28 17:09:48 -08001189 Type::WeakPtr(ptr) => {
1190 write!(out, "::std::weak_ptr<");
1191 write_type(out, &ptr.inner);
1192 write!(out, ">");
1193 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001194 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001195 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001196 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001197 write!(out, ">");
1198 }
David Tolnay7db73692019-10-20 14:51:12 -04001199 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001200 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001201 write!(out, "const ");
1202 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001203 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001204 write!(out, " &");
1205 }
1206 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001207 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001208 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001209 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001210 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001211 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001212 write!(out, "const ");
1213 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001214 write_type(out, &slice.inner);
1215 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001216 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001217 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001218 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001219 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001220 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001221 None => write!(out, "void"),
1222 }
1223 write!(out, "(");
1224 for (i, arg) in f.args.iter().enumerate() {
1225 if i > 0 {
1226 write!(out, ", ");
1227 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001228 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001229 }
1230 write!(out, ")>");
1231 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001232 Type::Array(a) => {
1233 write!(out, "::std::array<");
1234 write_type(out, &a.inner);
1235 write!(out, ", {}>", &a.len);
1236 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001237 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001238 }
1239}
1240
David Tolnayf6a89f22020-05-10 23:39:27 -07001241fn write_atom(out: &mut OutFile, atom: Atom) {
1242 match atom {
1243 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001244 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001245 U8 => write!(out, "::std::uint8_t"),
1246 U16 => write!(out, "::std::uint16_t"),
1247 U32 => write!(out, "::std::uint32_t"),
1248 U64 => write!(out, "::std::uint64_t"),
1249 Usize => write!(out, "::std::size_t"),
1250 I8 => write!(out, "::std::int8_t"),
1251 I16 => write!(out, "::std::int16_t"),
1252 I32 => write!(out, "::std::int32_t"),
1253 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001254 Isize => write!(out, "::rust::isize"),
1255 F32 => write!(out, "float"),
1256 F64 => write!(out, "double"),
1257 CxxString => write!(out, "::std::string"),
1258 RustString => write!(out, "::rust::String"),
1259 }
1260}
1261
David Tolnaya7c2ea12020-10-30 21:32:53 -07001262fn write_type_space(out: &mut OutFile, ty: &Type) {
1263 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001264 write_space_after_type(out, ty);
1265}
1266
1267fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001268 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001269 Type::Ident(_)
1270 | Type::RustBox(_)
1271 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001272 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001273 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001274 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001275 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001276 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001277 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001278 | Type::Fn(_)
1279 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001280 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001281 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001282 }
1283}
1284
David Tolnay1bdb4712020-11-25 07:27:54 -08001285#[derive(Copy, Clone)]
1286enum UniquePtr<'a> {
David Tolnay75ea17c2020-12-06 21:08:34 -08001287 Ident(&'a RustName),
1288 CxxVector(&'a RustName),
David Tolnay1bdb4712020-11-25 07:27:54 -08001289}
1290
1291trait ToTypename {
1292 fn to_typename(&self, types: &Types) -> String;
1293}
1294
David Tolnay75ea17c2020-12-06 21:08:34 -08001295impl ToTypename for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001296 fn to_typename(&self, types: &Types) -> String {
1297 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001298 }
1299}
1300
David Tolnay1bdb4712020-11-25 07:27:54 -08001301impl<'a> ToTypename for UniquePtr<'a> {
1302 fn to_typename(&self, types: &Types) -> String {
1303 match self {
1304 UniquePtr::Ident(ident) => ident.to_typename(types),
1305 UniquePtr::CxxVector(element) => {
1306 format!("::std::vector<{}>", element.to_typename(types))
1307 }
1308 }
1309 }
1310}
1311
1312trait ToMangled {
1313 fn to_mangled(&self, types: &Types) -> Symbol;
1314}
1315
David Tolnay75ea17c2020-12-06 21:08:34 -08001316impl ToMangled for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001317 fn to_mangled(&self, types: &Types) -> Symbol {
1318 self.to_symbol(types)
1319 }
1320}
1321
1322impl<'a> ToMangled for UniquePtr<'a> {
1323 fn to_mangled(&self, types: &Types) -> Symbol {
1324 match self {
1325 UniquePtr::Ident(ident) => ident.to_mangled(types),
1326 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1327 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001328 }
1329}
1330
David Tolnaya7c2ea12020-10-30 21:32:53 -07001331fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001332 if out.header {
1333 return;
1334 }
1335
1336 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001337 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001338 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001339 for ty in out.types {
David Tolnay4c6052d2020-12-31 15:01:04 -08001340 let impl_key = match ty.impl_key() {
1341 Some(impl_key) => impl_key,
1342 None => continue,
1343 };
David Tolnayf33bc242020-12-04 18:27:02 -08001344 if let Type::RustBox(ptr) = ty {
1345 if let Type::Ident(inner) = &ptr.inner {
1346 if Atom::from(&inner.rust).is_none()
1347 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnay4c6052d2020-12-31 15:01:04 -08001348 || out.types.explicit_impls.contains_key(&impl_key))
David Tolnayf33bc242020-12-04 18:27:02 -08001349 {
1350 out.next_section();
1351 write_rust_box_extern(out, &out.types.resolve(&inner));
1352 }
David Tolnay7db73692019-10-20 14:51:12 -04001353 }
David Tolnayf33bc242020-12-04 18:27:02 -08001354 } else if let Type::RustVec(vec) = ty {
1355 if let Type::Ident(inner) = &vec.inner {
1356 if Atom::from(&inner.rust).is_none()
1357 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnay4c6052d2020-12-31 15:01:04 -08001358 || out.types.explicit_impls.contains_key(&impl_key))
David Tolnayf33bc242020-12-04 18:27:02 -08001359 {
David Tolnay6787be62020-04-25 11:01:02 -07001360 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001361 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001362 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001363 }
David Tolnay7db73692019-10-20 14:51:12 -04001364 } else if let Type::UniquePtr(ptr) = ty {
1365 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001366 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001367 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnay4c6052d2020-12-31 15:01:04 -08001368 || out.types.explicit_impls.contains_key(&impl_key))
David Tolnay7e69f892020-10-03 22:20:22 -07001369 {
David Tolnay7db73692019-10-20 14:51:12 -04001370 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001371 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001372 }
1373 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001374 } else if let Type::SharedPtr(ptr) = ty {
1375 if let Type::Ident(inner) = &ptr.inner {
1376 if Atom::from(&inner.rust).is_none()
David Tolnayb7453812020-12-01 21:46:55 -08001377 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnay4c6052d2020-12-31 15:01:04 -08001378 || out.types.explicit_impls.contains_key(&impl_key))
David Tolnayb3b24a12020-12-01 15:27:43 -08001379 {
1380 out.next_section();
1381 write_shared_ptr(out, inner);
1382 }
1383 }
David Tolnay215e77f2020-12-28 17:09:48 -08001384 } else if let Type::WeakPtr(ptr) = ty {
1385 if let Type::Ident(inner) = &ptr.inner {
1386 if Atom::from(&inner.rust).is_none()
1387 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnay4c6052d2020-12-31 15:01:04 -08001388 || out.types.explicit_impls.contains_key(&impl_key))
David Tolnay215e77f2020-12-28 17:09:48 -08001389 {
1390 out.next_section();
1391 write_weak_ptr(out, inner);
1392 }
1393 }
David Tolnayf33bc242020-12-04 18:27:02 -08001394 } else if let Type::CxxVector(vector) = ty {
1395 if let Type::Ident(inner) = &vector.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001396 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001397 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnay4c6052d2020-12-31 15:01:04 -08001398 || out.types.explicit_impls.contains_key(&impl_key))
David Tolnay7e69f892020-10-03 22:20:22 -07001399 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001400 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001401 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001402 }
1403 }
1404 }
1405 }
David Tolnay0c033e32020-11-01 15:15:48 -08001406 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001407
David Tolnay0c033e32020-11-01 15:15:48 -08001408 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001409 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001410 for ty in out.types {
David Tolnay4c6052d2020-12-31 15:01:04 -08001411 let impl_key = match ty.impl_key() {
1412 Some(impl_key) => impl_key,
1413 None => continue,
1414 };
David Tolnayf33bc242020-12-04 18:27:02 -08001415 if let Type::RustBox(ptr) = ty {
1416 if let Type::Ident(inner) = &ptr.inner {
1417 if Atom::from(&inner.rust).is_none()
1418 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnay4c6052d2020-12-31 15:01:04 -08001419 || out.types.explicit_impls.contains_key(&impl_key))
David Tolnayf33bc242020-12-04 18:27:02 -08001420 {
1421 write_rust_box_impl(out, &out.types.resolve(&inner));
1422 }
David Tolnay7db73692019-10-20 14:51:12 -04001423 }
David Tolnayf33bc242020-12-04 18:27:02 -08001424 } else if let Type::RustVec(vec) = ty {
1425 if let Type::Ident(inner) = &vec.inner {
1426 if Atom::from(&inner.rust).is_none()
1427 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnay4c6052d2020-12-31 15:01:04 -08001428 || out.types.explicit_impls.contains_key(&impl_key))
David Tolnayf33bc242020-12-04 18:27:02 -08001429 {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001430 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001431 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001432 }
David Tolnay7db73692019-10-20 14:51:12 -04001433 }
1434 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001435 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001436 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001437}
1438
David Tolnay8faec772020-11-02 00:18:19 -08001439fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001440 let inner = ident.to_fully_qualified();
1441 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001442
David Tolnay7db73692019-10-20 14:51:12 -04001443 writeln!(
1444 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001445 "{} *cxxbridge1$box${}$alloc() noexcept;",
1446 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001447 );
1448 writeln!(
1449 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001450 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1451 instance, inner,
1452 );
1453 writeln!(
1454 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001455 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001456 instance, inner,
1457 );
David Tolnay7db73692019-10-20 14:51:12 -04001458}
1459
David Tolnay75ea17c2020-12-06 21:08:34 -08001460fn write_rust_vec_extern(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001461 let inner = element.to_typename(out.types);
1462 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001463
David Tolnay12320e12020-12-09 23:09:36 -08001464 out.include.cstddef = true;
1465
Myron Ahneba35cf2020-02-05 19:41:51 +07001466 writeln!(
1467 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001468 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001469 instance, inner,
1470 );
1471 writeln!(
1472 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001473 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001474 instance, inner,
1475 );
1476 writeln!(
1477 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001478 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001479 instance, inner,
1480 );
David Tolnay219c0792020-04-24 20:31:37 -07001481 writeln!(
1482 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001483 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001484 instance, inner,
1485 );
1486 writeln!(
1487 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001488 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001489 inner, instance,
1490 );
David Tolnay503d0192020-04-24 22:18:56 -07001491 writeln!(
1492 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001493 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001494 instance, inner,
1495 );
1496 writeln!(
1497 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001498 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001499 instance, inner,
1500 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001501}
1502
David Tolnay8faec772020-11-02 00:18:19 -08001503fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001504 let inner = ident.to_fully_qualified();
1505 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001506
1507 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001508 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001509 writeln!(
1510 out,
1511 "{} *Box<{}>::allocation::alloc() noexcept {{",
1512 inner, inner,
1513 );
David Tolnayc4b34222020-12-12 13:06:26 -08001514 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001515 writeln!(out, "}}");
1516
1517 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001518 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001519 writeln!(
1520 out,
David Tolnaye5703162020-12-12 16:26:35 -08001521 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001522 inner, inner,
1523 );
1524 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1525 writeln!(out, "}}");
1526
1527 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001528 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001529 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001530 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001531 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001532}
1533
David Tolnay75ea17c2020-12-06 21:08:34 -08001534fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001535 let inner = element.to_typename(out.types);
1536 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001537
David Tolnay12320e12020-12-09 23:09:36 -08001538 out.include.cstddef = true;
1539
Myron Ahneba35cf2020-02-05 19:41:51 +07001540 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001541 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001542 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001543 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001544 writeln!(out, "}}");
1545
1546 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001547 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001548 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001549 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001550 writeln!(out, "}}");
1551
1552 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001553 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001554 writeln!(
1555 out,
1556 "::std::size_t Vec<{}>::size() const noexcept {{",
1557 inner,
1558 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001559 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001560 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001561
1562 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001563 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001564 writeln!(
1565 out,
1566 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1567 inner,
1568 );
David Tolnay381d9532020-12-11 13:59:45 -08001569 writeln!(
1570 out,
1571 " return cxxbridge1$rust_vec${}$capacity(this);",
1572 instance,
1573 );
David Tolnaydc62d712020-12-11 13:51:53 -08001574 writeln!(out, "}}");
1575
1576 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001577 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001578 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001579 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001580 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001581
1582 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001583 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001584 writeln!(
1585 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001586 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001587 inner,
1588 );
1589 writeln!(
1590 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001591 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001592 instance,
1593 );
1594 writeln!(out, "}}");
1595
1596 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001597 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001598 writeln!(
1599 out,
1600 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1601 inner,
1602 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001603 writeln!(
1604 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001605 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001606 instance,
1607 );
1608 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001609}
1610
David Tolnay75ea17c2020-12-06 21:08:34 -08001611fn write_unique_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001612 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001613 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001614}
1615
1616// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001617fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001618 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001619 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001620 let inner = ty.to_typename(out.types);
1621 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001622
David Tolnay63da4d32020-04-25 09:41:12 -07001623 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001624 // Some aliases are to opaque types; some are to trivial types. We can't
1625 // know at code generation time, so we generate both C++ and Rust side
1626 // bindings for a "new" method anyway. But the Rust code can't be called
1627 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001628 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001629 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001630 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001631 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001632 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001633 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001634 };
1635
David Tolnay53462762020-11-25 07:08:10 -08001636 let conditional_delete = match ty {
1637 UniquePtr::Ident(ident) => {
1638 !out.types.structs.contains_key(&ident.rust)
1639 && !out.types.enums.contains_key(&ident.rust)
1640 }
1641 UniquePtr::CxxVector(_) => false,
1642 };
1643
1644 if conditional_delete {
1645 out.builtin.is_complete = true;
1646 let definition = match ty {
1647 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1648 UniquePtr::CxxVector(_) => unreachable!(),
1649 };
1650 writeln!(
1651 out,
David Tolnay75068632020-12-26 22:15:17 -08001652 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001653 inner, definition,
1654 );
1655 }
David Tolnay7db73692019-10-20 14:51:12 -04001656 writeln!(
1657 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001658 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001659 inner,
1660 );
1661 writeln!(
1662 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001663 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001664 inner,
1665 );
1666 writeln!(
1667 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001668 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001669 instance, inner,
1670 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001671 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001672 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001673 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001674 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001675 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001676 out,
David Tolnay0b881402020-12-01 21:05:08 -08001677 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1678 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001679 );
David Tolnay63da4d32020-04-25 09:41:12 -07001680 writeln!(
1681 out,
David Tolnay0b881402020-12-01 21:05:08 -08001682 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1683 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001684 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001685 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001686 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001687 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001688 }
David Tolnay7db73692019-10-20 14:51:12 -04001689 writeln!(
1690 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001691 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001692 instance, inner, inner,
1693 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001694 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001695 writeln!(out, "}}");
1696 writeln!(
1697 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001698 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001699 inner, instance, inner,
1700 );
1701 writeln!(out, " return ptr.get();");
1702 writeln!(out, "}}");
1703 writeln!(
1704 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001705 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001706 inner, instance, inner,
1707 );
1708 writeln!(out, " return ptr.release();");
1709 writeln!(out, "}}");
1710 writeln!(
1711 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001712 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001713 instance, inner,
1714 );
David Tolnay53462762020-11-25 07:08:10 -08001715 if conditional_delete {
1716 out.builtin.deleter_if = true;
1717 writeln!(
1718 out,
David Tolnay75068632020-12-26 22:15:17 -08001719 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001720 inner,
1721 );
1722 } else {
1723 writeln!(out, " ptr->~unique_ptr();");
1724 }
David Tolnay7db73692019-10-20 14:51:12 -04001725 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001726}
Myron Ahneba35cf2020-02-05 19:41:51 +07001727
David Tolnay75ea17c2020-12-06 21:08:34 -08001728fn write_shared_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnayb3b24a12020-12-01 15:27:43 -08001729 let resolved = out.types.resolve(ident);
1730 let inner = resolved.to_fully_qualified();
1731 let instance = ident.to_symbol(out.types);
1732
David Tolnayb3b24a12020-12-01 15:27:43 -08001733 out.include.new = true;
1734 out.include.utility = true;
1735
1736 // Some aliases are to opaque types; some are to trivial types. We can't
1737 // know at code generation time, so we generate both C++ and Rust side
1738 // bindings for a "new" method anyway. But the Rust code can't be called for
1739 // Opaque types because the 'new' method is not implemented.
1740 let can_construct_from_value = out.types.structs.contains_key(&ident.rust)
1741 || out.types.enums.contains_key(&ident.rust)
1742 || out.types.aliases.contains_key(&ident.rust);
1743
David Tolnayb3b24a12020-12-01 15:27:43 -08001744 writeln!(
1745 out,
1746 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1747 inner,
1748 );
1749 writeln!(
1750 out,
1751 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1752 inner,
1753 );
1754 writeln!(
1755 out,
1756 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1757 instance, inner,
1758 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001759 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001760 writeln!(out, "}}");
1761 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001762 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001763 writeln!(
1764 out,
David Tolnay0b881402020-12-01 21:05:08 -08001765 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1766 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001767 );
1768 writeln!(
1769 out,
David Tolnay0b881402020-12-01 21:05:08 -08001770 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1771 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001772 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001773 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001774 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001775 writeln!(out, "}}");
1776 }
1777 writeln!(
1778 out,
1779 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1780 instance, inner, inner,
1781 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001782 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001783 writeln!(out, "}}");
1784 writeln!(
1785 out,
1786 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1787 inner, instance, inner,
1788 );
1789 writeln!(out, " return self.get();");
1790 writeln!(out, "}}");
1791 writeln!(
1792 out,
1793 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1794 instance, inner,
1795 );
1796 writeln!(out, " self->~shared_ptr();");
1797 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001798}
1799
David Tolnay215e77f2020-12-28 17:09:48 -08001800fn write_weak_ptr(out: &mut OutFile, ident: &RustName) {
1801 let resolved = out.types.resolve(ident);
1802 let inner = resolved.to_fully_qualified();
1803 let instance = ident.to_symbol(out.types);
1804
1805 out.include.new = true;
1806 out.include.utility = true;
1807
1808 writeln!(
1809 out,
1810 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1811 inner,
1812 );
1813 writeln!(
1814 out,
1815 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1816 inner,
1817 );
1818 writeln!(
1819 out,
1820 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1821 instance, inner,
1822 );
1823 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1824 writeln!(out, "}}");
1825 writeln!(
1826 out,
1827 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1828 instance, inner, inner,
1829 );
1830 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1831 writeln!(out, "}}");
1832 writeln!(
1833 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001834 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1835 instance, inner, inner,
1836 );
1837 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1838 writeln!(out, "}}");
1839 writeln!(
1840 out,
David Tolnay7a487852020-12-28 18:02:25 -08001841 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1842 instance, inner, inner,
1843 );
1844 writeln!(
1845 out,
1846 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1847 inner,
1848 );
1849 writeln!(out, "}}");
1850 writeln!(
1851 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001852 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1853 instance, inner,
1854 );
1855 writeln!(out, " self->~weak_ptr();");
1856 writeln!(out, "}}");
1857}
1858
David Tolnay75ea17c2020-12-06 21:08:34 -08001859fn write_cxx_vector(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001860 let inner = element.to_typename(out.types);
1861 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001862
David Tolnay12320e12020-12-09 23:09:36 -08001863 out.include.cstddef = true;
1864
Myron Ahneba35cf2020-02-05 19:41:51 +07001865 writeln!(
1866 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001867 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001868 instance, inner,
1869 );
1870 writeln!(out, " return s.size();");
1871 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001872 writeln!(
1873 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001874 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001875 inner, instance, inner,
1876 );
David Tolnay767e00d2020-12-21 17:12:27 -08001877 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001878 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001879
David Tolnay70820672020-12-07 11:15:14 -08001880 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001881 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001882}