blob: 97610a4f95706f0441dd09e5946f909afa2ca43f [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 Tolnay3abed472020-12-31 23:34:53 -08006use crate::syntax::instantiate::ImplKey;
David Tolnaye352c1e2020-12-31 16:41:05 -08007use crate::syntax::map::UnorderedMap as Map;
David Tolnay14dc7922021-01-02 01:07:16 -08008use crate::syntax::set::{OrderedSet, UnorderedSet};
David Tolnay891061b2020-04-19 22:42:33 -07009use crate::syntax::symbol::Symbol;
David Tolnay5b1d8632020-12-20 22:05:11 -080010use crate::syntax::trivial::{self, TrivialReason};
Adrian Taylorc8713432020-10-21 18:20:55 -070011use crate::syntax::{
David Tolnay3abed472020-12-31 23:34:53 -080012 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, Signature, Struct, Trait, Type,
13 TypeAlias, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070014};
David Tolnay7db73692019-10-20 14:51:12 -040015use proc_macro2::Ident;
16
David Tolnayac7188c2020-11-01 16:58:16 -080017pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080018 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040019 let out = &mut out_file;
20
David Tolnaydfb82d72020-11-02 00:10:10 -080021 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070022 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023
David Tolnay7b0e5102020-11-01 23:22:12 -080024 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080025 write_data_structures(out, apis);
26 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080027 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040028
David Tolnay3374d8d2020-10-31 22:18:45 -070029 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070030 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070031
David Tolnayac7188c2020-11-01 16:58:16 -080032 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070033}
34
David Tolnay7b0e5102020-11-01 23:22:12 -080035fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
36 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080037 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080038 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080039 _ => false,
40 };
Adrian Taylorc8713432020-10-21 18:20:55 -070041
David Tolnay7b0e5102020-11-01 23:22:12 -080042 let apis_by_namespace =
43 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
44
David Tolnayd920be52020-11-01 23:34:30 -080045 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080046
David Tolnayd920be52020-11-01 23:34:30 -080047 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080048 let apis = ns_entries.direct_content();
49
50 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080051 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080052 match api {
David Tolnayed6ba4a2021-01-01 14:59:40 -080053 Api::Struct(strct) => write_struct_decl(out, &strct.name),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080054 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080055 Api::CxxType(ety) => write_struct_using(out, &ety.name),
David Tolnayed6ba4a2021-01-01 14:59:40 -080056 Api::RustType(ety) => write_struct_decl(out, &ety.name),
David Tolnay7b0e5102020-11-01 23:22:12 -080057 _ => unreachable!(),
58 }
David Tolnay7db73692019-10-20 14:51:12 -040059 }
David Tolnay7db73692019-10-20 14:51:12 -040060
David Tolnay7b0e5102020-11-01 23:22:12 -080061 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080062 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
63 write(out, nested_ns_entries, indent + 2);
64 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080065 }
Adrian Taylorf9213622020-10-31 22:25:42 -070066 }
67}
68
David Tolnaye1109d92020-11-01 20:51:56 -080069fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnaye352c1e2020-12-31 16:41:05 -080070 let mut methods_for_type = Map::new();
David Tolnay630af882020-10-31 22:03:47 -070071 for api in apis {
David Tolnay102c7ea2020-11-08 18:58:09 -080072 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayf94bef12020-04-17 14:46:42 -070073 if let Some(receiver) = &efn.sig.receiver {
74 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070075 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070076 .or_insert_with(Vec::new)
77 .push(efn);
78 }
79 }
80 }
Joel Galenson968738f2020-04-15 14:19:33 -070081
David Tolnaye352c1e2020-12-31 16:41:05 -080082 let mut structs_written = UnorderedSet::new();
David Tolnay5439fa12020-11-03 18:45:01 -080083 let mut toposorted_structs = out.types.toposorted_structs.iter();
David Tolnay9622b462020-11-02 21:34:42 -080084 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070085 match api {
David Tolnay5439fa12020-11-03 18:45:01 -080086 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
87 for next in &mut toposorted_structs {
88 if !out.types.cxx.contains(&strct.name.rust) {
89 out.next_section();
David Tolnay102c7ea2020-11-08 18:58:09 -080090 let methods = methods_for_type
91 .get(&strct.name.rust)
92 .map(Vec::as_slice)
93 .unwrap_or_default();
94 write_struct(out, next, methods);
David Tolnay5439fa12020-11-03 18:45:01 -080095 }
96 structs_written.insert(&next.name.rust);
97 if next.name.rust == strct.name.rust {
98 break;
99 }
100 }
101 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700102 Api::Enum(enm) => {
103 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800104 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700105 check_enum(out, enm);
106 } else {
107 write_enum(out, enm);
108 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700109 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700110 Api::RustType(ety) => {
David Tolnay8d067de2020-12-26 16:18:23 -0800111 out.next_section();
112 let methods = methods_for_type
113 .get(&ety.name.rust)
114 .map(Vec::as_slice)
115 .unwrap_or_default();
116 write_opaque_type(out, ety, methods);
David Tolnayc1fe0052020-04-17 15:15:06 -0700117 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700118 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400119 }
120 }
121
David Tolnay440e24a2021-01-01 23:25:34 -0800122 out.set_namespace(Default::default());
123
David Tolnay14dc7922021-01-02 01:07:16 -0800124 // MSVC workaround for "C linkage function cannot return C++ class" error.
125 // Apparently the compiler fails to perform implicit instantiations as part
126 // of an extern declaration. Instead we instantiate explicitly.
127 // See https://stackoverflow.com/a/57429504/6086311.
David Tolnay440e24a2021-01-01 23:25:34 -0800128 out.next_section();
David Tolnay14dc7922021-01-02 01:07:16 -0800129 let mut slice_in_return_position = OrderedSet::new();
130 for api in apis {
131 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
132 if let Some(ty @ Type::SliceRef(_)) = &efn.ret {
133 slice_in_return_position.insert(ty);
134 }
David Tolnay440e24a2021-01-01 23:25:34 -0800135 }
136 }
David Tolnay14dc7922021-01-02 01:07:16 -0800137 for ty in &slice_in_return_position {
138 write!(out, "template struct ");
139 write_type(out, ty);
140 writeln!(out, ";");
141 }
David Tolnay440e24a2021-01-01 23:25:34 -0800142
David Tolnayfabca772020-10-03 21:25:41 -0700143 out.next_section();
144 for api in apis {
145 if let Api::TypeAlias(ety) = api {
Adrian Taylorf831a5a2020-12-07 16:49:19 -0800146 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
David Tolnay5b1d8632020-12-20 22:05:11 -0800147 check_trivial_extern_type(out, ety, reasons)
David Tolnayfabca772020-10-03 21:25:41 -0700148 }
149 }
150 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800151}
152
David Tolnaye1109d92020-11-01 20:51:56 -0800153fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700154 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400155 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800156 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800157 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800158 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800159 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
160 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
161 _ => {}
162 }
David Tolnay7db73692019-10-20 14:51:12 -0400163 }
David Tolnay7da38202020-11-27 17:36:16 -0800164
165 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400166 }
167
168 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800169 match api {
170 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800171 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800172 Api::RustFunction(efn) => {
173 out.next_section();
174 write_rust_function_shim(out, efn);
175 }
176 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400177 }
178 }
David Tolnay7db73692019-10-20 14:51:12 -0400179}
180
David Tolnay7da38202020-11-27 17:36:16 -0800181fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
182 out.set_namespace(Default::default());
183 out.begin_block(Block::Namespace("std"));
184
185 for api in apis {
186 if let Api::Struct(strct) = api {
187 if derive::contains(&strct.derives, Trait::Hash) {
188 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800189 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800190 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800191 let qualified = strct.name.to_fully_qualified();
192 writeln!(out, "template <> struct hash<{}> {{", qualified);
193 writeln!(
194 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800195 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800196 qualified,
197 );
198 let link_name = mangle::operator(&strct.name, "hash");
199 write!(out, " return ::");
200 for name in &strct.name.namespace {
201 write!(out, "{}::", name);
202 }
203 writeln!(out, "{}(self);", link_name);
204 writeln!(out, " }}");
205 writeln!(out, "}};");
206 }
207 }
208 }
209
210 out.end_block(Block::Namespace("std"));
211}
212
David Tolnaydfb82d72020-11-02 00:10:10 -0800213fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
214 for api in apis {
215 if let Api::Include(include) = api {
216 out.include.insert(include);
217 }
218 }
219
David Tolnaya7c2ea12020-10-30 21:32:53 -0700220 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400221 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700222 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700223 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
224 | Some(I64) => out.include.cstdint = true,
225 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800226 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700227 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800228 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800229 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700230 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800231 Type::RustBox(_) => out.builtin.rust_box = true,
232 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700233 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay215e77f2020-12-28 17:09:48 -0800234 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800235 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700236 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800237 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800238 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800239 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800240 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400241 }
242 }
David Tolnayec66d112020-10-31 21:00:26 -0700243}
David Tolnayf51447e2020-03-06 14:14:27 -0800244
David Tolnay102c7ea2020-11-08 18:58:09 -0800245fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800246 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800247 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800248
David Tolnay17a934c2020-11-02 00:40:04 -0800249 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800250 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700251 writeln!(out, "#ifndef {}", guard);
252 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400253 for line in strct.doc.to_string().lines() {
254 writeln!(out, "//{}", line);
255 }
David Tolnay17a934c2020-11-02 00:40:04 -0800256 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800257
David Tolnay7db73692019-10-20 14:51:12 -0400258 for field in &strct.fields {
David Tolnay5573e522020-12-31 11:07:38 -0800259 for line in field.doc.to_string().lines() {
260 writeln!(out, " //{}", line);
261 }
David Tolnay7db73692019-10-20 14:51:12 -0400262 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700263 write_type_space(out, &field.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800264 writeln!(out, "{};", field.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400265 }
David Tolnay84389352020-11-27 17:12:10 -0800266
David Tolnay5554ebd2020-12-10 11:34:41 -0800267 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800268
David Tolnay102c7ea2020-11-08 18:58:09 -0800269 for method in methods {
270 write!(out, " ");
271 let sig = &method.sig;
272 let local_name = method.name.cxx.to_string();
273 write_rust_function_shim_decl(out, &local_name, sig, false);
274 writeln!(out, ";");
275 }
David Tolnay84389352020-11-27 17:12:10 -0800276
David Tolnayb960ed22020-11-27 14:34:30 -0800277 if operator_eq {
278 writeln!(
279 out,
280 " bool operator==(const {} &) const noexcept;",
281 strct.name.cxx,
282 );
283 writeln!(
284 out,
285 " bool operator!=(const {} &) const noexcept;",
286 strct.name.cxx,
287 );
288 }
David Tolnay84389352020-11-27 17:12:10 -0800289
290 if operator_ord {
291 writeln!(
292 out,
293 " bool operator<(const {} &) const noexcept;",
294 strct.name.cxx,
295 );
296 writeln!(
297 out,
298 " bool operator<=(const {} &) const noexcept;",
299 strct.name.cxx,
300 );
301 writeln!(
302 out,
303 " bool operator>(const {} &) const noexcept;",
304 strct.name.cxx,
305 );
306 writeln!(
307 out,
308 " bool operator>=(const {} &) const noexcept;",
309 strct.name.cxx,
310 );
311 }
312
David Tolnay5554ebd2020-12-10 11:34:41 -0800313 out.include.type_traits = true;
314 writeln!(out, " using IsRelocatable = ::std::true_type;");
315
David Tolnay7db73692019-10-20 14:51:12 -0400316 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700317 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400318}
319
David Tolnayed6ba4a2021-01-01 14:59:40 -0800320fn write_struct_decl(out: &mut OutFile, ident: &Pair) {
321 writeln!(out, "struct {};", ident.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400322}
323
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800324fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800325 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800326 write_atom(out, enm.repr);
327 writeln!(out, ";");
328}
329
David Tolnay8faec772020-11-02 00:18:19 -0800330fn write_struct_using(out: &mut OutFile, ident: &Pair) {
331 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800332}
333
David Tolnay8d067de2020-12-26 16:18:23 -0800334fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800335 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800336 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700337 writeln!(out, "#ifndef {}", guard);
338 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700339 for line in ety.doc.to_string().lines() {
340 writeln!(out, "//{}", line);
341 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800342
David Tolnay365fc7c2020-11-25 16:08:13 -0800343 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800344 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800345 out,
346 "struct {} final : public ::rust::Opaque {{",
347 ety.name.cxx,
348 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800349
Joel Galenson968738f2020-04-15 14:19:33 -0700350 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800351 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700352 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800353 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700354 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800355 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800356 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800357
David Tolnay8d067de2020-12-26 16:18:23 -0800358 if !methods.is_empty() {
359 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700360 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800361
David Tolnayee6ecfc2020-12-26 21:54:37 -0800362 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800363 out.include.cstddef = true;
364 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800365 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800366 writeln!(out, " struct layout {{");
367 writeln!(out, " static ::std::size_t size() noexcept;");
368 writeln!(out, " static ::std::size_t align() noexcept;");
369 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700370 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700371 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700372}
373
David Tolnay0b9b9f82020-11-01 20:41:00 -0800374fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800375 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800376 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700377 writeln!(out, "#ifndef {}", guard);
378 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700379 for line in enm.doc.to_string().lines() {
380 writeln!(out, "//{}", line);
381 }
David Tolnay17a934c2020-11-02 00:40:04 -0800382 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700383 write_atom(out, enm.repr);
384 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700385 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800386 for line in variant.doc.to_string().lines() {
387 writeln!(out, " //{}", line);
388 }
David Tolnaye6f62142020-12-21 16:00:41 -0800389 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700390 }
391 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700392 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700393}
394
David Tolnay0b9b9f82020-11-01 20:41:00 -0800395fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800396 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800397 out.include.type_traits = true;
398 writeln!(
399 out,
400 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
401 enm.name.cxx,
402 );
David Tolnay17a934c2020-11-02 00:40:04 -0800403 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700404 write_atom(out, enm.repr);
405 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700406 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700407 write!(out, "static_assert(static_cast<");
408 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700409 writeln!(
410 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700411 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800412 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700413 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700414 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700415}
416
David Tolnay5b1d8632020-12-20 22:05:11 -0800417fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800418 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700419 // necessary for soundness. That's because triviality is always declared by
420 // the user in the form of an unsafe impl of cxx::ExternType:
421 //
422 // unsafe impl ExternType for MyType {
423 // type Id = cxx::type_id!("...");
424 // type Kind = cxx::kind::Trivial;
425 // }
426 //
427 // Since the user went on the record with their unsafe impl to unsafely
428 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800429 // them if that were wrong. However, in practice correctly reasoning about
430 // the relocatability of C++ types is challenging, particularly if the type
431 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700432 //
David Tolnay174bd952020-11-02 09:23:12 -0800433 // There may be legitimate reasons to opt out of this assertion for support
434 // of types that the programmer knows are soundly Rust-movable despite not
435 // being recognized as such by the C++ type system due to a move constructor
436 // or destructor. To opt out of the relocatability check, they need to do
437 // one of the following things in any header used by `include!` in their
438 // bridge.
439 //
440 // --- if they define the type:
441 // struct MyType {
442 // ...
443 // + using IsRelocatable = std::true_type;
444 // };
445 //
446 // --- otherwise:
447 // + template <>
448 // + struct rust::IsRelocatable<MyType> : std::true_type {};
449 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700450
David Tolnay5b1d8632020-12-20 22:05:11 -0800451 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800452 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800453 writeln!(out, "static_assert(");
454 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
455 writeln!(
456 out,
457 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
458 id.trim_start_matches("::"),
459 trivial::as_what(&alias.name, reasons),
460 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700461}
462
David Tolnayb960ed22020-11-27 14:34:30 -0800463fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
464 out.set_namespace(&strct.name.namespace);
465 out.begin_block(Block::ExternC);
466
467 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800468 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800469 writeln!(
470 out,
471 "bool {}(const {1} &, const {1} &) noexcept;",
472 link_name, strct.name.cxx,
473 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800474
475 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800476 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800477 writeln!(
478 out,
479 "bool {}(const {1} &, const {1} &) noexcept;",
480 link_name, strct.name.cxx,
481 );
482 }
David Tolnayb960ed22020-11-27 14:34:30 -0800483 }
484
David Tolnay84389352020-11-27 17:12:10 -0800485 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800486 let link_name = mangle::operator(&strct.name, "lt");
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
David Tolnaya05f9402020-11-27 17:44:05 -0800493 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800494 writeln!(
495 out,
496 "bool {}(const {1} &, const {1} &) noexcept;",
497 link_name, strct.name.cxx,
498 );
499
500 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800501 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800502 writeln!(
503 out,
504 "bool {}(const {1} &, const {1} &) noexcept;",
505 link_name, strct.name.cxx,
506 );
507
David Tolnaya05f9402020-11-27 17:44:05 -0800508 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800509 writeln!(
510 out,
511 "bool {}(const {1} &, const {1} &) noexcept;",
512 link_name, strct.name.cxx,
513 );
514 }
515 }
516
David Tolnay7da38202020-11-27 17:36:16 -0800517 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800518 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800519 let link_name = mangle::operator(&strct.name, "hash");
520 writeln!(
521 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800522 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800523 link_name, strct.name.cxx,
524 );
525 }
526
David Tolnayb960ed22020-11-27 14:34:30 -0800527 out.end_block(Block::ExternC);
528}
529
530fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
531 if out.header {
532 return;
533 }
534
535 out.set_namespace(&strct.name.namespace);
536
537 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800538 out.next_section();
539 writeln!(
540 out,
541 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
542 strct.name.cxx,
543 );
David Tolnaya05f9402020-11-27 17:44:05 -0800544 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800545 writeln!(out, " return {}(*this, rhs);", link_name);
546 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800547
David Tolnayb960ed22020-11-27 14:34:30 -0800548 out.next_section();
549 writeln!(
550 out,
551 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
552 strct.name.cxx,
553 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800554 if derive::contains(&strct.derives, Trait::Eq) {
555 writeln!(out, " return !(*this == rhs);");
556 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800557 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800558 writeln!(out, " return {}(*this, rhs);", link_name);
559 }
David Tolnayb960ed22020-11-27 14:34:30 -0800560 writeln!(out, "}}");
561 }
David Tolnay84389352020-11-27 17:12:10 -0800562
563 if derive::contains(&strct.derives, Trait::PartialOrd) {
564 out.next_section();
565 writeln!(
566 out,
567 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
568 strct.name.cxx,
569 );
David Tolnaya05f9402020-11-27 17:44:05 -0800570 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800571 writeln!(out, " return {}(*this, rhs);", link_name);
572 writeln!(out, "}}");
573
574 out.next_section();
575 writeln!(
576 out,
577 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
578 strct.name.cxx,
579 );
David Tolnaya05f9402020-11-27 17:44:05 -0800580 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800581 writeln!(out, " return {}(*this, rhs);", link_name);
582 writeln!(out, "}}");
583
584 out.next_section();
585 writeln!(
586 out,
587 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
588 strct.name.cxx,
589 );
590 if derive::contains(&strct.derives, Trait::Ord) {
591 writeln!(out, " return !(*this <= rhs);");
592 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800593 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800594 writeln!(out, " return {}(*this, rhs);", link_name);
595 }
596 writeln!(out, "}}");
597
598 out.next_section();
599 writeln!(
600 out,
601 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
602 strct.name.cxx,
603 );
604 if derive::contains(&strct.derives, Trait::Ord) {
605 writeln!(out, " return !(*this < rhs);");
606 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800607 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800608 writeln!(out, " return {}(*this, rhs);", link_name);
609 }
610 writeln!(out, "}}");
611 }
David Tolnayb960ed22020-11-27 14:34:30 -0800612}
613
David Tolnay358bc4b2020-12-26 22:37:57 -0800614fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
615 out.set_namespace(&ety.name.namespace);
616 out.begin_block(Block::ExternC);
617
618 let link_name = mangle::operator(&ety.name, "sizeof");
619 writeln!(out, "::std::size_t {}() noexcept;", link_name);
620
621 let link_name = mangle::operator(&ety.name, "alignof");
622 writeln!(out, "::std::size_t {}() noexcept;", link_name);
623
624 out.end_block(Block::ExternC);
625}
626
627fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
628 if out.header {
629 return;
630 }
631
632 out.set_namespace(&ety.name.namespace);
633
634 out.next_section();
635 let link_name = mangle::operator(&ety.name, "sizeof");
636 writeln!(
637 out,
638 "::std::size_t {}::layout::size() noexcept {{",
639 ety.name.cxx,
640 );
641 writeln!(out, " return {}();", link_name);
642 writeln!(out, "}}");
643
644 out.next_section();
645 let link_name = mangle::operator(&ety.name, "alignof");
646 writeln!(
647 out,
648 "::std::size_t {}::layout::align() noexcept {{",
649 ety.name.cxx,
650 );
651 writeln!(out, " return {}();", link_name);
652 writeln!(out, "}}");
653}
654
David Tolnay1eadd262020-12-29 16:42:08 -0800655fn begin_function_definition(out: &mut OutFile) {
656 if let Some(annotation) = &out.opt.cxx_impl_annotations {
657 write!(out, "{} ", annotation);
658 }
659}
660
David Tolnay0b9b9f82020-11-01 20:41:00 -0800661fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800662 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800663 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800664 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800665 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700666 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700667 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700668 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700669 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700670 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700671 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700672 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700673 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700674 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800675 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700676 write!(out, "const ");
677 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700678 write!(
679 out,
680 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800681 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700682 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700683 }
David Tolnay7db73692019-10-20 14:51:12 -0400684 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700685 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400686 write!(out, ", ");
687 }
David Tolnaya46a2372020-03-06 10:03:48 -0800688 if arg.ty == RustString {
689 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700690 } else if let Type::RustVec(_) = arg.ty {
691 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800692 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700693 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400694 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700695 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400696 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700697 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400698 write!(out, ", ");
699 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700700 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400701 write!(out, "*return$");
702 }
703 writeln!(out, ") noexcept {{");
704 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700705 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700706 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800707 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700708 Some(receiver) => write!(
709 out,
710 "({}::*{}$)(",
David Tolnay1e5fe232021-01-01 18:11:40 -0800711 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800712 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700713 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700714 }
David Tolnay7db73692019-10-20 14:51:12 -0400715 for (i, arg) in efn.args.iter().enumerate() {
716 if i > 0 {
717 write!(out, ", ");
718 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700719 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400720 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700721 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700722 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800723 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700724 write!(out, " const");
725 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700726 }
727 write!(out, " = ");
728 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800729 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700730 Some(receiver) => write!(
731 out,
732 "&{}::{}",
David Tolnay1e5fe232021-01-01 18:11:40 -0800733 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800734 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700735 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700736 }
737 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400738 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700739 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700740 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700741 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700742 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700743 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700744 writeln!(out, " [&] {{");
745 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700746 }
David Tolnay7db73692019-10-20 14:51:12 -0400747 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700748 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400749 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700750 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400751 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700752 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400753 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700754 }
David Tolnay681f5c82021-01-01 22:56:27 -0800755 if let Some(Type::Ref(_)) = efn.ret {
756 write!(out, "&");
David Tolnay7db73692019-10-20 14:51:12 -0400757 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700758 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800759 None => write!(out, "{}$(", efn.name.rust),
760 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700761 }
David Tolnay7db73692019-10-20 14:51:12 -0400762 for (i, arg) in efn.args.iter().enumerate() {
763 if i > 0 {
764 write!(out, ", ");
765 }
766 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700767 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800768 write!(out, "::from_raw({})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400769 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700770 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800771 write!(out, "({})", arg.name.cxx);
David Tolnaya46a2372020-03-06 10:03:48 -0800772 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700773 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800774 write!(
775 out,
776 "::rust::String(::rust::unsafe_bitcopy, *{})",
David Tolnay84ed6ad2021-01-01 15:30:14 -0800777 arg.name.cxx,
David Tolnaycc3767f2020-03-06 10:41:51 -0800778 );
David Tolnay313b10e2020-04-25 16:30:51 -0700779 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700780 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700781 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800782 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.name.cxx);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700783 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700784 out.include.utility = true;
David Tolnay84ed6ad2021-01-01 15:30:14 -0800785 write!(out, "::std::move(*{})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400786 } else {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800787 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400788 }
789 }
790 write!(out, ")");
791 match &efn.ret {
792 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
793 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
794 _ => {}
795 }
796 if indirect_return {
797 write!(out, ")");
798 }
799 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700800 if efn.throws {
801 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800802 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700803 writeln!(out, " throw$.ptr = nullptr;");
804 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700805 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700806 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700807 writeln!(
808 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800809 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700810 );
David Tolnay5d121442020-03-17 22:14:40 -0700811 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700812 writeln!(out, " return throw$;");
813 }
David Tolnay7db73692019-10-20 14:51:12 -0400814 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700815 for arg in &efn.args {
816 if let Type::Fn(f) = &arg.ty {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800817 let var = &arg.name;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700818 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700819 }
820 }
David Tolnayca563ee2020-11-01 20:12:27 -0800821 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700822}
823
David Tolnay84ed6ad2021-01-01 15:30:14 -0800824fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700825 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700826 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700827 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700828
829 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700830 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
831 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400832}
833
David Tolnay0b9b9f82020-11-01 20:41:00 -0800834fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800835 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800836 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700837 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700838 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700839 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800840 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700841}
842
843fn write_rust_function_decl_impl(
844 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700845 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700846 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700847 indirect_call: bool,
848) {
David Tolnay04770742020-11-01 13:50:50 -0800849 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700850 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700851 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700852 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700853 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700854 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700855 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700856 write!(out, "{}(", link_name);
857 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700858 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800859 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700860 write!(out, "const ");
861 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700862 write!(
863 out,
864 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800865 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700866 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700867 needs_comma = true;
868 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700869 for arg in &sig.args {
870 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400871 write!(out, ", ");
872 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700873 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700874 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400875 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700876 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700877 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400878 write!(out, ", ");
879 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700880 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400881 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700882 needs_comma = true;
883 }
884 if indirect_call {
885 if needs_comma {
886 write!(out, ", ");
887 }
888 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400889 }
890 writeln!(out, ") noexcept;");
891}
892
David Tolnay0b9b9f82020-11-01 20:41:00 -0800893fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800894 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400895 for line in efn.doc.to_string().lines() {
896 writeln!(out, "//{}", line);
897 }
David Tolnaya73853b2020-04-20 01:19:56 -0700898 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800899 None => efn.name.cxx.to_string(),
David Tolnay1e5fe232021-01-01 18:11:40 -0800900 Some(receiver) => format!(
901 "{}::{}",
902 out.types.resolve(&receiver.ty).name.cxx,
903 efn.name.cxx,
904 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700905 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700906 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700907 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700908 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700909}
910
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700911fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700912 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700913 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700914 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700915 indirect_call: bool,
916) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700917 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700918 write!(out, "{}(", local_name);
919 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400920 if i > 0 {
921 write!(out, ", ");
922 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700923 write_type_space(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800924 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400925 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700926 if indirect_call {
927 if !sig.args.is_empty() {
928 write!(out, ", ");
929 }
930 write!(out, "void *extern$");
931 }
David Tolnay1e548172020-03-16 13:37:09 -0700932 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700933 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800934 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700935 write!(out, " const");
936 }
937 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700938 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700939 write!(out, " noexcept");
940 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700941}
942
943fn write_rust_function_shim_impl(
944 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700945 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700946 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700947 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700948 indirect_call: bool,
949) {
950 if out.header && sig.receiver.is_some() {
951 // We've already defined this inside the struct.
952 return;
953 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800954 if !out.header {
955 begin_function_definition(out);
956 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700957 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400958 if out.header {
959 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700960 return;
David Tolnay7db73692019-10-20 14:51:12 -0400961 }
David Tolnay439cde22020-04-20 00:46:25 -0700962 writeln!(out, " {{");
963 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700964 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700965 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700966 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700967 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700968 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800969 writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -0700970 }
971 }
972 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700973 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700974 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700975 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700976 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700977 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700978 writeln!(out, "> return$;");
979 write!(out, " ");
980 } else if let Some(ret) = &sig.ret {
981 write!(out, "return ");
982 match ret {
983 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700984 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700985 write!(out, "::from_raw(");
986 }
987 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700988 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700989 write!(out, "(");
990 }
991 Type::Ref(_) => write!(out, "*"),
992 _ => {}
993 }
994 }
995 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700996 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700997 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700998 }
999 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001000 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001001 if sig.receiver.is_some() {
1002 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001003 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001004 }
David Tolnay9d840362020-11-10 08:50:40 -08001005 for arg in &sig.args {
1006 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001007 write!(out, ", ");
1008 }
David Tolnay681f5c82021-01-01 22:56:27 -08001009 if out.types.needs_indirect_abi(&arg.ty) {
1010 write!(out, "&");
David Tolnay439cde22020-04-20 00:46:25 -07001011 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001012 write!(out, "{}", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -07001013 match &arg.ty {
1014 Type::RustBox(_) => write!(out, ".into_raw()"),
1015 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001016 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001017 _ => {}
1018 }
David Tolnay9d840362020-11-10 08:50:40 -08001019 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001020 }
1021 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001022 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001023 write!(out, ", ");
1024 }
1025 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001026 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001027 }
1028 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001029 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001030 write!(out, ", ");
1031 }
1032 write!(out, "extern$");
1033 }
1034 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001035 if !indirect_return {
1036 if let Some(ret) = &sig.ret {
David Tolnay681f5c82021-01-01 22:56:27 -08001037 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001038 write!(out, ")");
1039 }
David Tolnay439cde22020-04-20 00:46:25 -07001040 }
1041 }
1042 writeln!(out, ";");
1043 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001044 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001045 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001046 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001047 writeln!(out, " }}");
1048 }
1049 if indirect_return {
1050 out.include.utility = true;
1051 writeln!(out, " return ::std::move(return$.value);");
1052 }
1053 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001054}
1055
David Tolnaya7c2ea12020-10-30 21:32:53 -07001056fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001057 match ty {
1058 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001059 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001060 }
1061}
1062
David Tolnay75dca2e2020-03-25 20:17:52 -07001063fn indirect_return(sig: &Signature, types: &Types) -> bool {
1064 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001065 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001066 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001067}
1068
David Tolnaya7c2ea12020-10-30 21:32:53 -07001069fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001070 match ty {
1071 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001072 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001073 write!(out, "*");
1074 }
1075 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001076 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001077 write!(out, "const ");
1078 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001079 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001080 write!(out, " *");
1081 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001082 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001083 }
1084}
1085
David Tolnaya7c2ea12020-10-30 21:32:53 -07001086fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1087 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001088 match ty {
1089 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001090 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001091 _ => write_space_after_type(out, ty),
1092 }
1093}
1094
David Tolnaya7c2ea12020-10-30 21:32:53 -07001095fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001096 match ty {
1097 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001098 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001099 write!(out, "*");
1100 }
David Tolnay4a441222020-01-25 16:24:27 -08001101 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001102 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001103 write!(out, "const ");
1104 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001105 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001106 write!(out, " *");
1107 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001108 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1109 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001110 }
1111}
1112
David Tolnaya7c2ea12020-10-30 21:32:53 -07001113fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001114 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001115 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001116 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001117 write!(out, "*");
1118 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001119 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001120 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001121 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001122 write!(out, "*");
1123 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001124 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -04001125}
1126
David Tolnaya7c2ea12020-10-30 21:32:53 -07001127fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001128 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001129 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001130 Some(atom) => write_atom(out, atom),
David Tolnay1e5fe232021-01-01 18:11:40 -08001131 None => write!(
1132 out,
1133 "{}",
1134 out.types.resolve(ident).name.to_fully_qualified(),
1135 ),
David Tolnay7db73692019-10-20 14:51:12 -04001136 },
1137 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001138 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001139 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001140 write!(out, ">");
1141 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001142 Type::RustVec(ty) => {
1143 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001144 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001145 write!(out, ">");
1146 }
David Tolnay7db73692019-10-20 14:51:12 -04001147 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001148 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001149 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001150 write!(out, ">");
1151 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001152 Type::SharedPtr(ptr) => {
1153 write!(out, "::std::shared_ptr<");
1154 write_type(out, &ptr.inner);
1155 write!(out, ">");
1156 }
David Tolnay215e77f2020-12-28 17:09:48 -08001157 Type::WeakPtr(ptr) => {
1158 write!(out, "::std::weak_ptr<");
1159 write_type(out, &ptr.inner);
1160 write!(out, ">");
1161 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001162 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001163 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001164 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001165 write!(out, ">");
1166 }
David Tolnay7db73692019-10-20 14:51:12 -04001167 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001168 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001169 write!(out, "const ");
1170 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001171 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001172 write!(out, " &");
1173 }
1174 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001175 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001176 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001177 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001178 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001179 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001180 write!(out, "const ");
1181 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001182 write_type(out, &slice.inner);
1183 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001184 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001185 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001186 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001187 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001188 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001189 None => write!(out, "void"),
1190 }
1191 write!(out, "(");
1192 for (i, arg) in f.args.iter().enumerate() {
1193 if i > 0 {
1194 write!(out, ", ");
1195 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001196 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001197 }
1198 write!(out, ")>");
1199 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001200 Type::Array(a) => {
1201 write!(out, "::std::array<");
1202 write_type(out, &a.inner);
1203 write!(out, ", {}>", &a.len);
1204 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001205 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001206 }
1207}
1208
David Tolnayf6a89f22020-05-10 23:39:27 -07001209fn write_atom(out: &mut OutFile, atom: Atom) {
1210 match atom {
1211 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001212 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001213 U8 => write!(out, "::std::uint8_t"),
1214 U16 => write!(out, "::std::uint16_t"),
1215 U32 => write!(out, "::std::uint32_t"),
1216 U64 => write!(out, "::std::uint64_t"),
1217 Usize => write!(out, "::std::size_t"),
1218 I8 => write!(out, "::std::int8_t"),
1219 I16 => write!(out, "::std::int16_t"),
1220 I32 => write!(out, "::std::int32_t"),
1221 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001222 Isize => write!(out, "::rust::isize"),
1223 F32 => write!(out, "float"),
1224 F64 => write!(out, "double"),
1225 CxxString => write!(out, "::std::string"),
1226 RustString => write!(out, "::rust::String"),
1227 }
1228}
1229
David Tolnaya7c2ea12020-10-30 21:32:53 -07001230fn write_type_space(out: &mut OutFile, ty: &Type) {
1231 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001232 write_space_after_type(out, ty);
1233}
1234
1235fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001236 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001237 Type::Ident(_)
1238 | Type::RustBox(_)
1239 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001240 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001241 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001242 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001243 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001244 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001245 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001246 | Type::Fn(_)
1247 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001248 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001249 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001250 }
1251}
1252
David Tolnay1bdb4712020-11-25 07:27:54 -08001253#[derive(Copy, Clone)]
1254enum UniquePtr<'a> {
David Tolnay3abed472020-12-31 23:34:53 -08001255 Ident(&'a Ident),
1256 CxxVector(&'a Ident),
David Tolnay1bdb4712020-11-25 07:27:54 -08001257}
1258
1259trait ToTypename {
1260 fn to_typename(&self, types: &Types) -> String;
1261}
1262
David Tolnay3abed472020-12-31 23:34:53 -08001263impl ToTypename for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001264 fn to_typename(&self, types: &Types) -> String {
David Tolnay1e5fe232021-01-01 18:11:40 -08001265 types.resolve(self).name.to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001266 }
1267}
1268
David Tolnay1bdb4712020-11-25 07:27:54 -08001269impl<'a> ToTypename for UniquePtr<'a> {
1270 fn to_typename(&self, types: &Types) -> String {
1271 match self {
1272 UniquePtr::Ident(ident) => ident.to_typename(types),
1273 UniquePtr::CxxVector(element) => {
1274 format!("::std::vector<{}>", element.to_typename(types))
1275 }
1276 }
1277 }
1278}
1279
1280trait ToMangled {
1281 fn to_mangled(&self, types: &Types) -> Symbol;
1282}
1283
David Tolnay3abed472020-12-31 23:34:53 -08001284impl ToMangled for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001285 fn to_mangled(&self, types: &Types) -> Symbol {
David Tolnay1e5fe232021-01-01 18:11:40 -08001286 types.resolve(self).name.to_symbol()
David Tolnay1bdb4712020-11-25 07:27:54 -08001287 }
1288}
1289
1290impl<'a> ToMangled for UniquePtr<'a> {
1291 fn to_mangled(&self, types: &Types) -> Symbol {
1292 match self {
1293 UniquePtr::Ident(ident) => ident.to_mangled(types),
1294 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1295 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001296 }
1297}
1298
David Tolnaya7c2ea12020-10-30 21:32:53 -07001299fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001300 if out.header {
1301 return;
1302 }
1303
1304 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001305 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001306 out.begin_block(Block::ExternC);
David Tolnay3abed472020-12-31 23:34:53 -08001307 for impl_key in out.types.impls.keys() {
1308 out.next_section();
1309 match impl_key {
1310 ImplKey::RustBox(ident) => write_rust_box_extern(out, ident),
1311 ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident),
1312 ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident),
1313 ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident),
1314 ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident),
1315 ImplKey::CxxVector(ident) => write_cxx_vector(out, ident),
David Tolnay7db73692019-10-20 14:51:12 -04001316 }
1317 }
David Tolnay0c033e32020-11-01 15:15:48 -08001318 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001319
David Tolnay0c033e32020-11-01 15:15:48 -08001320 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001321 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3abed472020-12-31 23:34:53 -08001322 for impl_key in out.types.impls.keys() {
1323 match impl_key {
1324 ImplKey::RustBox(ident) => write_rust_box_impl(out, ident),
1325 ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident),
1326 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -04001327 }
1328 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001329 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001330 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001331}
1332
David Tolnay3abed472020-12-31 23:34:53 -08001333fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1334 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001335 let inner = resolve.name.to_fully_qualified();
1336 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001337
David Tolnay7db73692019-10-20 14:51:12 -04001338 writeln!(
1339 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001340 "{} *cxxbridge1$box${}$alloc() noexcept;",
1341 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001342 );
1343 writeln!(
1344 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001345 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1346 instance, inner,
1347 );
1348 writeln!(
1349 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001350 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001351 instance, inner,
1352 );
David Tolnay7db73692019-10-20 14:51:12 -04001353}
1354
David Tolnay3abed472020-12-31 23:34:53 -08001355fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001356 let inner = element.to_typename(out.types);
1357 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001358
David Tolnay12320e12020-12-09 23:09:36 -08001359 out.include.cstddef = true;
1360
Myron Ahneba35cf2020-02-05 19:41:51 +07001361 writeln!(
1362 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001363 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001364 instance, inner,
1365 );
1366 writeln!(
1367 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001368 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001369 instance, inner,
1370 );
1371 writeln!(
1372 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001373 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001374 instance, inner,
1375 );
David Tolnay219c0792020-04-24 20:31:37 -07001376 writeln!(
1377 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001378 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001379 instance, inner,
1380 );
1381 writeln!(
1382 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001383 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001384 inner, instance,
1385 );
David Tolnay503d0192020-04-24 22:18:56 -07001386 writeln!(
1387 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001388 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001389 instance, inner,
1390 );
1391 writeln!(
1392 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001393 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001394 instance, inner,
1395 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001396}
1397
David Tolnay3abed472020-12-31 23:34:53 -08001398fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1399 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001400 let inner = resolve.name.to_fully_qualified();
1401 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001402
1403 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001404 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001405 writeln!(
1406 out,
1407 "{} *Box<{}>::allocation::alloc() noexcept {{",
1408 inner, inner,
1409 );
David Tolnayc4b34222020-12-12 13:06:26 -08001410 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001411 writeln!(out, "}}");
1412
1413 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001414 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001415 writeln!(
1416 out,
David Tolnaye5703162020-12-12 16:26:35 -08001417 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001418 inner, inner,
1419 );
1420 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1421 writeln!(out, "}}");
1422
1423 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001424 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001425 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001426 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001427 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001428}
1429
David Tolnay3abed472020-12-31 23:34:53 -08001430fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001431 let inner = element.to_typename(out.types);
1432 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001433
David Tolnay12320e12020-12-09 23:09:36 -08001434 out.include.cstddef = true;
1435
Myron Ahneba35cf2020-02-05 19:41:51 +07001436 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001437 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001438 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001439 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001440 writeln!(out, "}}");
1441
1442 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001443 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001444 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001445 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001446 writeln!(out, "}}");
1447
1448 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001449 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001450 writeln!(
1451 out,
1452 "::std::size_t Vec<{}>::size() const noexcept {{",
1453 inner,
1454 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001455 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001456 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001457
1458 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001459 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001460 writeln!(
1461 out,
1462 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1463 inner,
1464 );
David Tolnay381d9532020-12-11 13:59:45 -08001465 writeln!(
1466 out,
1467 " return cxxbridge1$rust_vec${}$capacity(this);",
1468 instance,
1469 );
David Tolnaydc62d712020-12-11 13:51:53 -08001470 writeln!(out, "}}");
1471
1472 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001473 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001474 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001475 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001476 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001477
1478 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001479 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001480 writeln!(
1481 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001482 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001483 inner,
1484 );
1485 writeln!(
1486 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001487 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001488 instance,
1489 );
1490 writeln!(out, "}}");
1491
1492 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001493 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001494 writeln!(
1495 out,
1496 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1497 inner,
1498 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001499 writeln!(
1500 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001501 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001502 instance,
1503 );
1504 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001505}
1506
David Tolnay3abed472020-12-31 23:34:53 -08001507fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001508 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001509 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001510}
1511
1512// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001513fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001514 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001515 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001516 let inner = ty.to_typename(out.types);
1517 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001518
David Tolnay63da4d32020-04-25 09:41:12 -07001519 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001520 // Some aliases are to opaque types; some are to trivial types. We can't
1521 // know at code generation time, so we generate both C++ and Rust side
1522 // bindings for a "new" method anyway. But the Rust code can't be called
1523 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001524 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001525 out.types.structs.contains_key(ident)
1526 || out.types.enums.contains_key(ident)
1527 || out.types.aliases.contains_key(ident)
David Tolnayca0f9da2020-10-16 13:16:17 -07001528 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001529 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001530 };
1531
David Tolnay53462762020-11-25 07:08:10 -08001532 let conditional_delete = match ty {
1533 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001534 !out.types.structs.contains_key(ident) && !out.types.enums.contains_key(ident)
David Tolnay53462762020-11-25 07:08:10 -08001535 }
1536 UniquePtr::CxxVector(_) => false,
1537 };
1538
1539 if conditional_delete {
1540 out.builtin.is_complete = true;
1541 let definition = match ty {
David Tolnay1e5fe232021-01-01 18:11:40 -08001542 UniquePtr::Ident(ty) => &out.types.resolve(ty).name.cxx,
David Tolnay53462762020-11-25 07:08:10 -08001543 UniquePtr::CxxVector(_) => unreachable!(),
1544 };
1545 writeln!(
1546 out,
David Tolnay75068632020-12-26 22:15:17 -08001547 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001548 inner, definition,
1549 );
1550 }
David Tolnay7db73692019-10-20 14:51:12 -04001551 writeln!(
1552 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001553 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001554 inner,
1555 );
1556 writeln!(
1557 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001558 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001559 inner,
1560 );
1561 writeln!(
1562 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001563 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001564 instance, inner,
1565 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001566 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001567 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001568 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001569 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001570 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001571 out,
David Tolnay0b881402020-12-01 21:05:08 -08001572 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1573 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001574 );
David Tolnay63da4d32020-04-25 09:41:12 -07001575 writeln!(
1576 out,
David Tolnay0b881402020-12-01 21:05:08 -08001577 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1578 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001579 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001580 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001581 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001582 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001583 }
David Tolnay7db73692019-10-20 14:51:12 -04001584 writeln!(
1585 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001586 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001587 instance, inner, inner,
1588 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001589 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001590 writeln!(out, "}}");
1591 writeln!(
1592 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001593 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001594 inner, instance, inner,
1595 );
1596 writeln!(out, " return ptr.get();");
1597 writeln!(out, "}}");
1598 writeln!(
1599 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001600 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001601 inner, instance, inner,
1602 );
1603 writeln!(out, " return ptr.release();");
1604 writeln!(out, "}}");
1605 writeln!(
1606 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001607 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001608 instance, inner,
1609 );
David Tolnay53462762020-11-25 07:08:10 -08001610 if conditional_delete {
1611 out.builtin.deleter_if = true;
1612 writeln!(
1613 out,
David Tolnay75068632020-12-26 22:15:17 -08001614 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001615 inner,
1616 );
1617 } else {
1618 writeln!(out, " ptr->~unique_ptr();");
1619 }
David Tolnay7db73692019-10-20 14:51:12 -04001620 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001621}
Myron Ahneba35cf2020-02-05 19:41:51 +07001622
David Tolnay3abed472020-12-31 23:34:53 -08001623fn write_shared_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001624 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001625 let inner = resolve.name.to_fully_qualified();
1626 let instance = resolve.name.to_symbol();
David Tolnayb3b24a12020-12-01 15:27:43 -08001627
David Tolnayb3b24a12020-12-01 15:27:43 -08001628 out.include.new = true;
1629 out.include.utility = true;
1630
1631 // Some aliases are to opaque types; some are to trivial types. We can't
1632 // know at code generation time, so we generate both C++ and Rust side
1633 // bindings for a "new" method anyway. But the Rust code can't be called for
1634 // Opaque types because the 'new' method is not implemented.
David Tolnay3abed472020-12-31 23:34:53 -08001635 let can_construct_from_value = out.types.structs.contains_key(ident)
1636 || out.types.enums.contains_key(ident)
1637 || out.types.aliases.contains_key(ident);
David Tolnayb3b24a12020-12-01 15:27:43 -08001638
David Tolnayb3b24a12020-12-01 15:27:43 -08001639 writeln!(
1640 out,
1641 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1642 inner,
1643 );
1644 writeln!(
1645 out,
1646 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1647 inner,
1648 );
1649 writeln!(
1650 out,
1651 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1652 instance, inner,
1653 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001654 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001655 writeln!(out, "}}");
1656 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001657 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001658 writeln!(
1659 out,
David Tolnay0b881402020-12-01 21:05:08 -08001660 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1661 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001662 );
1663 writeln!(
1664 out,
David Tolnay0b881402020-12-01 21:05:08 -08001665 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1666 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001667 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001668 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001669 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001670 writeln!(out, "}}");
1671 }
1672 writeln!(
1673 out,
1674 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1675 instance, inner, inner,
1676 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001677 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001678 writeln!(out, "}}");
1679 writeln!(
1680 out,
1681 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1682 inner, instance, inner,
1683 );
1684 writeln!(out, " return self.get();");
1685 writeln!(out, "}}");
1686 writeln!(
1687 out,
1688 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1689 instance, inner,
1690 );
1691 writeln!(out, " self->~shared_ptr();");
1692 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001693}
1694
David Tolnay3abed472020-12-31 23:34:53 -08001695fn write_weak_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001696 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001697 let inner = resolve.name.to_fully_qualified();
1698 let instance = resolve.name.to_symbol();
David Tolnay215e77f2020-12-28 17:09:48 -08001699
1700 out.include.new = true;
1701 out.include.utility = true;
1702
1703 writeln!(
1704 out,
1705 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1706 inner,
1707 );
1708 writeln!(
1709 out,
1710 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1711 inner,
1712 );
1713 writeln!(
1714 out,
1715 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1716 instance, inner,
1717 );
1718 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1719 writeln!(out, "}}");
1720 writeln!(
1721 out,
1722 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1723 instance, inner, inner,
1724 );
1725 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1726 writeln!(out, "}}");
1727 writeln!(
1728 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001729 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1730 instance, inner, inner,
1731 );
1732 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1733 writeln!(out, "}}");
1734 writeln!(
1735 out,
David Tolnay7a487852020-12-28 18:02:25 -08001736 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1737 instance, inner, inner,
1738 );
1739 writeln!(
1740 out,
1741 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1742 inner,
1743 );
1744 writeln!(out, "}}");
1745 writeln!(
1746 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001747 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1748 instance, inner,
1749 );
1750 writeln!(out, " self->~weak_ptr();");
1751 writeln!(out, "}}");
1752}
1753
David Tolnay3abed472020-12-31 23:34:53 -08001754fn write_cxx_vector(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001755 let inner = element.to_typename(out.types);
1756 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001757
David Tolnay12320e12020-12-09 23:09:36 -08001758 out.include.cstddef = true;
1759
Myron Ahneba35cf2020-02-05 19:41:51 +07001760 writeln!(
1761 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001762 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001763 instance, inner,
1764 );
1765 writeln!(out, " return s.size();");
1766 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001767 writeln!(
1768 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001769 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001770 inner, instance, inner,
1771 );
David Tolnay767e00d2020-12-21 17:12:27 -08001772 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001773 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001774
David Tolnay70820672020-12-07 11:15:14 -08001775 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001776 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001777}