blob: 370dac4566a50d74822407de8784117dd947a7c3 [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 Tolnaye05de872021-01-02 18:18:35 -0800124 if !out.header {
125 // MSVC workaround for "C linkage function cannot return C++ class" error.
126 // Apparently the compiler fails to perform implicit instantiations as part
127 // of an extern declaration return type. Instead we instantiate explicitly.
128 // See https://stackoverflow.com/a/57429504/6086311.
129 out.next_section();
130 let mut slice_in_return_position = OrderedSet::new();
131 for api in apis {
132 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
133 if let Some(ty @ Type::SliceRef(_)) = &efn.ret {
134 slice_in_return_position.insert(ty);
135 }
David Tolnay14dc7922021-01-02 01:07:16 -0800136 }
David Tolnay440e24a2021-01-01 23:25:34 -0800137 }
David Tolnaye05de872021-01-02 18:18:35 -0800138 for ty in &slice_in_return_position {
139 write!(out, "template class ");
140 write_type(out, ty);
141 writeln!(out, ";");
142 }
David Tolnay440e24a2021-01-01 23:25:34 -0800143
David Tolnay36614d62021-01-02 18:30:15 -0800144 out.next_section();
145 for api in apis {
146 if let Api::TypeAlias(ety) = api {
147 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
148 check_trivial_extern_type(out, ety, reasons)
149 }
David Tolnayfabca772020-10-03 21:25:41 -0700150 }
151 }
152 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800153}
154
David Tolnaye1109d92020-11-01 20:51:56 -0800155fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700156 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400157 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800158 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800159 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800160 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800161 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
162 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
163 _ => {}
164 }
David Tolnay7db73692019-10-20 14:51:12 -0400165 }
David Tolnay7da38202020-11-27 17:36:16 -0800166
167 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400168 }
169
170 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800171 match api {
172 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800173 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800174 Api::RustFunction(efn) => {
175 out.next_section();
176 write_rust_function_shim(out, efn);
177 }
178 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400179 }
180 }
David Tolnay7db73692019-10-20 14:51:12 -0400181}
182
David Tolnay7da38202020-11-27 17:36:16 -0800183fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
184 out.set_namespace(Default::default());
185 out.begin_block(Block::Namespace("std"));
186
187 for api in apis {
188 if let Api::Struct(strct) = api {
189 if derive::contains(&strct.derives, Trait::Hash) {
190 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800191 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800192 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800193 let qualified = strct.name.to_fully_qualified();
194 writeln!(out, "template <> struct hash<{}> {{", qualified);
195 writeln!(
196 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800197 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800198 qualified,
199 );
200 let link_name = mangle::operator(&strct.name, "hash");
201 write!(out, " return ::");
202 for name in &strct.name.namespace {
203 write!(out, "{}::", name);
204 }
205 writeln!(out, "{}(self);", link_name);
206 writeln!(out, " }}");
207 writeln!(out, "}};");
208 }
209 }
210 }
211
212 out.end_block(Block::Namespace("std"));
213}
214
David Tolnaydfb82d72020-11-02 00:10:10 -0800215fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
216 for api in apis {
217 if let Api::Include(include) = api {
218 out.include.insert(include);
219 }
220 }
221
David Tolnaya7c2ea12020-10-30 21:32:53 -0700222 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400223 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700224 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700225 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
226 | Some(I64) => out.include.cstdint = true,
227 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800228 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700229 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800230 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800231 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700232 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800233 Type::RustBox(_) => out.builtin.rust_box = true,
234 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700235 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay215e77f2020-12-28 17:09:48 -0800236 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800237 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700238 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800239 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800240 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800241 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800242 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400243 }
244 }
David Tolnayec66d112020-10-31 21:00:26 -0700245}
David Tolnayf51447e2020-03-06 14:14:27 -0800246
David Tolnay102c7ea2020-11-08 18:58:09 -0800247fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800248 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800249 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800250
David Tolnay17a934c2020-11-02 00:40:04 -0800251 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800252 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700253 writeln!(out, "#ifndef {}", guard);
254 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400255 for line in strct.doc.to_string().lines() {
256 writeln!(out, "//{}", line);
257 }
David Tolnay17a934c2020-11-02 00:40:04 -0800258 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800259
David Tolnay7db73692019-10-20 14:51:12 -0400260 for field in &strct.fields {
David Tolnay5573e522020-12-31 11:07:38 -0800261 for line in field.doc.to_string().lines() {
262 writeln!(out, " //{}", line);
263 }
David Tolnay7db73692019-10-20 14:51:12 -0400264 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700265 write_type_space(out, &field.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800266 writeln!(out, "{};", field.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400267 }
David Tolnay84389352020-11-27 17:12:10 -0800268
David Tolnay5554ebd2020-12-10 11:34:41 -0800269 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800270
David Tolnay102c7ea2020-11-08 18:58:09 -0800271 for method in methods {
272 write!(out, " ");
273 let sig = &method.sig;
274 let local_name = method.name.cxx.to_string();
275 write_rust_function_shim_decl(out, &local_name, sig, false);
276 writeln!(out, ";");
277 }
David Tolnay84389352020-11-27 17:12:10 -0800278
David Tolnayb960ed22020-11-27 14:34:30 -0800279 if operator_eq {
280 writeln!(
281 out,
282 " bool operator==(const {} &) const noexcept;",
283 strct.name.cxx,
284 );
285 writeln!(
286 out,
287 " bool operator!=(const {} &) const noexcept;",
288 strct.name.cxx,
289 );
290 }
David Tolnay84389352020-11-27 17:12:10 -0800291
292 if operator_ord {
293 writeln!(
294 out,
295 " bool operator<(const {} &) const noexcept;",
296 strct.name.cxx,
297 );
298 writeln!(
299 out,
300 " bool operator<=(const {} &) const noexcept;",
301 strct.name.cxx,
302 );
303 writeln!(
304 out,
305 " bool operator>(const {} &) const noexcept;",
306 strct.name.cxx,
307 );
308 writeln!(
309 out,
310 " bool operator>=(const {} &) const noexcept;",
311 strct.name.cxx,
312 );
313 }
314
David Tolnay5554ebd2020-12-10 11:34:41 -0800315 out.include.type_traits = true;
316 writeln!(out, " using IsRelocatable = ::std::true_type;");
317
David Tolnay7db73692019-10-20 14:51:12 -0400318 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700319 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400320}
321
David Tolnayed6ba4a2021-01-01 14:59:40 -0800322fn write_struct_decl(out: &mut OutFile, ident: &Pair) {
323 writeln!(out, "struct {};", ident.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400324}
325
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800326fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800327 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800328 write_atom(out, enm.repr);
329 writeln!(out, ";");
330}
331
David Tolnay8faec772020-11-02 00:18:19 -0800332fn write_struct_using(out: &mut OutFile, ident: &Pair) {
333 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800334}
335
David Tolnay8d067de2020-12-26 16:18:23 -0800336fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800337 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800338 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700339 writeln!(out, "#ifndef {}", guard);
340 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700341 for line in ety.doc.to_string().lines() {
342 writeln!(out, "//{}", line);
343 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800344
David Tolnay365fc7c2020-11-25 16:08:13 -0800345 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800346 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800347 out,
348 "struct {} final : public ::rust::Opaque {{",
349 ety.name.cxx,
350 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800351
Joel Galenson968738f2020-04-15 14:19:33 -0700352 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800353 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700354 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800355 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700356 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800357 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800358 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800359
David Tolnay8d067de2020-12-26 16:18:23 -0800360 if !methods.is_empty() {
361 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700362 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800363
David Tolnayee6ecfc2020-12-26 21:54:37 -0800364 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800365 out.include.cstddef = true;
366 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800367 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800368 writeln!(out, " struct layout {{");
369 writeln!(out, " static ::std::size_t size() noexcept;");
370 writeln!(out, " static ::std::size_t align() noexcept;");
371 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700372 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700373 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700374}
375
David Tolnay0b9b9f82020-11-01 20:41:00 -0800376fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800377 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800378 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700379 writeln!(out, "#ifndef {}", guard);
380 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700381 for line in enm.doc.to_string().lines() {
382 writeln!(out, "//{}", line);
383 }
David Tolnay17a934c2020-11-02 00:40:04 -0800384 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700385 write_atom(out, enm.repr);
386 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700387 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800388 for line in variant.doc.to_string().lines() {
389 writeln!(out, " //{}", line);
390 }
David Tolnaye6f62142020-12-21 16:00:41 -0800391 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700392 }
393 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700394 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700395}
396
David Tolnay0b9b9f82020-11-01 20:41:00 -0800397fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800398 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800399 out.include.type_traits = true;
400 writeln!(
401 out,
402 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
403 enm.name.cxx,
404 );
David Tolnay17a934c2020-11-02 00:40:04 -0800405 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700406 write_atom(out, enm.repr);
407 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700408 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700409 write!(out, "static_assert(static_cast<");
410 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700411 writeln!(
412 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700413 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800414 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700415 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700416 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700417}
418
David Tolnay5b1d8632020-12-20 22:05:11 -0800419fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800420 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700421 // necessary for soundness. That's because triviality is always declared by
422 // the user in the form of an unsafe impl of cxx::ExternType:
423 //
424 // unsafe impl ExternType for MyType {
425 // type Id = cxx::type_id!("...");
426 // type Kind = cxx::kind::Trivial;
427 // }
428 //
429 // Since the user went on the record with their unsafe impl to unsafely
430 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800431 // them if that were wrong. However, in practice correctly reasoning about
432 // the relocatability of C++ types is challenging, particularly if the type
433 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700434 //
David Tolnay174bd952020-11-02 09:23:12 -0800435 // There may be legitimate reasons to opt out of this assertion for support
436 // of types that the programmer knows are soundly Rust-movable despite not
437 // being recognized as such by the C++ type system due to a move constructor
438 // or destructor. To opt out of the relocatability check, they need to do
439 // one of the following things in any header used by `include!` in their
440 // bridge.
441 //
442 // --- if they define the type:
443 // struct MyType {
444 // ...
445 // + using IsRelocatable = std::true_type;
446 // };
447 //
448 // --- otherwise:
449 // + template <>
450 // + struct rust::IsRelocatable<MyType> : std::true_type {};
451 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700452
David Tolnay5b1d8632020-12-20 22:05:11 -0800453 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800454 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800455 writeln!(out, "static_assert(");
456 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
457 writeln!(
458 out,
459 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
460 id.trim_start_matches("::"),
461 trivial::as_what(&alias.name, reasons),
462 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700463}
464
David Tolnayb960ed22020-11-27 14:34:30 -0800465fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
466 out.set_namespace(&strct.name.namespace);
467 out.begin_block(Block::ExternC);
468
469 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800470 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800471 writeln!(
472 out,
473 "bool {}(const {1} &, const {1} &) noexcept;",
474 link_name, strct.name.cxx,
475 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800476
477 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800478 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800479 writeln!(
480 out,
481 "bool {}(const {1} &, const {1} &) noexcept;",
482 link_name, strct.name.cxx,
483 );
484 }
David Tolnayb960ed22020-11-27 14:34:30 -0800485 }
486
David Tolnay84389352020-11-27 17:12:10 -0800487 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800488 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800489 writeln!(
490 out,
491 "bool {}(const {1} &, const {1} &) noexcept;",
492 link_name, strct.name.cxx,
493 );
494
David Tolnaya05f9402020-11-27 17:44:05 -0800495 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800496 writeln!(
497 out,
498 "bool {}(const {1} &, const {1} &) noexcept;",
499 link_name, strct.name.cxx,
500 );
501
502 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800503 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800504 writeln!(
505 out,
506 "bool {}(const {1} &, const {1} &) noexcept;",
507 link_name, strct.name.cxx,
508 );
509
David Tolnaya05f9402020-11-27 17:44:05 -0800510 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800511 writeln!(
512 out,
513 "bool {}(const {1} &, const {1} &) noexcept;",
514 link_name, strct.name.cxx,
515 );
516 }
517 }
518
David Tolnay7da38202020-11-27 17:36:16 -0800519 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800520 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800521 let link_name = mangle::operator(&strct.name, "hash");
522 writeln!(
523 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800524 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800525 link_name, strct.name.cxx,
526 );
527 }
528
David Tolnayb960ed22020-11-27 14:34:30 -0800529 out.end_block(Block::ExternC);
530}
531
532fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
533 if out.header {
534 return;
535 }
536
537 out.set_namespace(&strct.name.namespace);
538
539 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800540 out.next_section();
541 writeln!(
542 out,
543 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
544 strct.name.cxx,
545 );
David Tolnaya05f9402020-11-27 17:44:05 -0800546 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800547 writeln!(out, " return {}(*this, rhs);", link_name);
548 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800549
David Tolnayb960ed22020-11-27 14:34:30 -0800550 out.next_section();
551 writeln!(
552 out,
553 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
554 strct.name.cxx,
555 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800556 if derive::contains(&strct.derives, Trait::Eq) {
557 writeln!(out, " return !(*this == rhs);");
558 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800559 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800560 writeln!(out, " return {}(*this, rhs);", link_name);
561 }
David Tolnayb960ed22020-11-27 14:34:30 -0800562 writeln!(out, "}}");
563 }
David Tolnay84389352020-11-27 17:12:10 -0800564
565 if derive::contains(&strct.derives, Trait::PartialOrd) {
566 out.next_section();
567 writeln!(
568 out,
569 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
570 strct.name.cxx,
571 );
David Tolnaya05f9402020-11-27 17:44:05 -0800572 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800573 writeln!(out, " return {}(*this, rhs);", link_name);
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 );
David Tolnaya05f9402020-11-27 17:44:05 -0800582 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800583 writeln!(out, " return {}(*this, rhs);", link_name);
584 writeln!(out, "}}");
585
586 out.next_section();
587 writeln!(
588 out,
589 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
590 strct.name.cxx,
591 );
592 if derive::contains(&strct.derives, Trait::Ord) {
593 writeln!(out, " return !(*this <= rhs);");
594 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800595 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800596 writeln!(out, " return {}(*this, rhs);", link_name);
597 }
598 writeln!(out, "}}");
599
600 out.next_section();
601 writeln!(
602 out,
603 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
604 strct.name.cxx,
605 );
606 if derive::contains(&strct.derives, Trait::Ord) {
607 writeln!(out, " return !(*this < rhs);");
608 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800609 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800610 writeln!(out, " return {}(*this, rhs);", link_name);
611 }
612 writeln!(out, "}}");
613 }
David Tolnayb960ed22020-11-27 14:34:30 -0800614}
615
David Tolnay358bc4b2020-12-26 22:37:57 -0800616fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
617 out.set_namespace(&ety.name.namespace);
618 out.begin_block(Block::ExternC);
619
620 let link_name = mangle::operator(&ety.name, "sizeof");
621 writeln!(out, "::std::size_t {}() noexcept;", link_name);
622
623 let link_name = mangle::operator(&ety.name, "alignof");
624 writeln!(out, "::std::size_t {}() noexcept;", link_name);
625
626 out.end_block(Block::ExternC);
627}
628
629fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
630 if out.header {
631 return;
632 }
633
634 out.set_namespace(&ety.name.namespace);
635
636 out.next_section();
637 let link_name = mangle::operator(&ety.name, "sizeof");
638 writeln!(
639 out,
640 "::std::size_t {}::layout::size() noexcept {{",
641 ety.name.cxx,
642 );
643 writeln!(out, " return {}();", link_name);
644 writeln!(out, "}}");
645
646 out.next_section();
647 let link_name = mangle::operator(&ety.name, "alignof");
648 writeln!(
649 out,
650 "::std::size_t {}::layout::align() noexcept {{",
651 ety.name.cxx,
652 );
653 writeln!(out, " return {}();", link_name);
654 writeln!(out, "}}");
655}
656
David Tolnay1eadd262020-12-29 16:42:08 -0800657fn begin_function_definition(out: &mut OutFile) {
658 if let Some(annotation) = &out.opt.cxx_impl_annotations {
659 write!(out, "{} ", annotation);
660 }
661}
662
David Tolnay0b9b9f82020-11-01 20:41:00 -0800663fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800664 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800665 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800666 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800667 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700668 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700669 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700670 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700671 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700672 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700673 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700674 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700675 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700676 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800677 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700678 write!(out, "const ");
679 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700680 write!(
681 out,
682 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800683 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700684 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700685 }
David Tolnay7db73692019-10-20 14:51:12 -0400686 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700687 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400688 write!(out, ", ");
689 }
David Tolnaya46a2372020-03-06 10:03:48 -0800690 if arg.ty == RustString {
691 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700692 } else if let Type::RustVec(_) = arg.ty {
693 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800694 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700695 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400696 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700697 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400698 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700699 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400700 write!(out, ", ");
701 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700702 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400703 write!(out, "*return$");
704 }
705 writeln!(out, ") noexcept {{");
706 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700707 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700708 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800709 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700710 Some(receiver) => write!(
711 out,
712 "({}::*{}$)(",
David Tolnay1e5fe232021-01-01 18:11:40 -0800713 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800714 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700715 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700716 }
David Tolnay7db73692019-10-20 14:51:12 -0400717 for (i, arg) in efn.args.iter().enumerate() {
718 if i > 0 {
719 write!(out, ", ");
720 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700721 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400722 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700723 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700724 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800725 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700726 write!(out, " const");
727 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700728 }
729 write!(out, " = ");
730 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800731 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700732 Some(receiver) => write!(
733 out,
734 "&{}::{}",
David Tolnay1e5fe232021-01-01 18:11:40 -0800735 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800736 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700737 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700738 }
739 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400740 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700741 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700742 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700743 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700744 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700745 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700746 writeln!(out, " [&] {{");
747 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700748 }
David Tolnay7db73692019-10-20 14:51:12 -0400749 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700750 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400751 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700752 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400753 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700754 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400755 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700756 }
David Tolnay681f5c82021-01-01 22:56:27 -0800757 if let Some(Type::Ref(_)) = efn.ret {
758 write!(out, "&");
David Tolnay7db73692019-10-20 14:51:12 -0400759 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700760 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800761 None => write!(out, "{}$(", efn.name.rust),
762 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700763 }
David Tolnay7db73692019-10-20 14:51:12 -0400764 for (i, arg) in efn.args.iter().enumerate() {
765 if i > 0 {
766 write!(out, ", ");
767 }
768 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700769 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800770 write!(out, "::from_raw({})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400771 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700772 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800773 write!(out, "({})", arg.name.cxx);
David Tolnaya46a2372020-03-06 10:03:48 -0800774 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700775 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800776 write!(
777 out,
778 "::rust::String(::rust::unsafe_bitcopy, *{})",
David Tolnay84ed6ad2021-01-01 15:30:14 -0800779 arg.name.cxx,
David Tolnaycc3767f2020-03-06 10:41:51 -0800780 );
David Tolnay313b10e2020-04-25 16:30:51 -0700781 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700782 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700783 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800784 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.name.cxx);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700785 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700786 out.include.utility = true;
David Tolnay84ed6ad2021-01-01 15:30:14 -0800787 write!(out, "::std::move(*{})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400788 } else {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800789 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400790 }
791 }
792 write!(out, ")");
793 match &efn.ret {
794 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
795 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
796 _ => {}
797 }
798 if indirect_return {
799 write!(out, ")");
800 }
801 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700802 if efn.throws {
803 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800804 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700805 writeln!(out, " throw$.ptr = nullptr;");
806 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700807 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700808 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700809 writeln!(
810 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800811 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700812 );
David Tolnay5d121442020-03-17 22:14:40 -0700813 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700814 writeln!(out, " return throw$;");
815 }
David Tolnay7db73692019-10-20 14:51:12 -0400816 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700817 for arg in &efn.args {
818 if let Type::Fn(f) = &arg.ty {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800819 let var = &arg.name;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700820 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700821 }
822 }
David Tolnayca563ee2020-11-01 20:12:27 -0800823 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700824}
825
David Tolnay84ed6ad2021-01-01 15:30:14 -0800826fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700827 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700828 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700829 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700830
831 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700832 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
833 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400834}
835
David Tolnay0b9b9f82020-11-01 20:41:00 -0800836fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800837 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800838 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700839 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700840 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700841 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800842 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700843}
844
845fn write_rust_function_decl_impl(
846 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700847 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700848 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700849 indirect_call: bool,
850) {
David Tolnay04770742020-11-01 13:50:50 -0800851 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700852 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700853 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700854 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700855 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700856 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700857 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700858 write!(out, "{}(", link_name);
859 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700860 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800861 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700862 write!(out, "const ");
863 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700864 write!(
865 out,
866 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800867 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700868 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700869 needs_comma = true;
870 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700871 for arg in &sig.args {
872 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400873 write!(out, ", ");
874 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700875 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700876 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400877 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700878 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700879 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400880 write!(out, ", ");
881 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700882 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400883 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700884 needs_comma = true;
885 }
886 if indirect_call {
887 if needs_comma {
888 write!(out, ", ");
889 }
890 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400891 }
892 writeln!(out, ") noexcept;");
893}
894
David Tolnay0b9b9f82020-11-01 20:41:00 -0800895fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800896 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400897 for line in efn.doc.to_string().lines() {
898 writeln!(out, "//{}", line);
899 }
David Tolnaya73853b2020-04-20 01:19:56 -0700900 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800901 None => efn.name.cxx.to_string(),
David Tolnay1e5fe232021-01-01 18:11:40 -0800902 Some(receiver) => format!(
903 "{}::{}",
904 out.types.resolve(&receiver.ty).name.cxx,
905 efn.name.cxx,
906 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700907 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700908 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700909 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700910 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700911}
912
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700913fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700914 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700915 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700916 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700917 indirect_call: bool,
918) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700919 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700920 write!(out, "{}(", local_name);
921 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400922 if i > 0 {
923 write!(out, ", ");
924 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700925 write_type_space(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800926 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400927 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700928 if indirect_call {
929 if !sig.args.is_empty() {
930 write!(out, ", ");
931 }
932 write!(out, "void *extern$");
933 }
David Tolnay1e548172020-03-16 13:37:09 -0700934 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700935 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800936 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700937 write!(out, " const");
938 }
939 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700940 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700941 write!(out, " noexcept");
942 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700943}
944
945fn write_rust_function_shim_impl(
946 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700947 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700948 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700949 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700950 indirect_call: bool,
951) {
952 if out.header && sig.receiver.is_some() {
953 // We've already defined this inside the struct.
954 return;
955 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800956 if !out.header {
957 begin_function_definition(out);
958 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700959 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400960 if out.header {
961 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700962 return;
David Tolnay7db73692019-10-20 14:51:12 -0400963 }
David Tolnay439cde22020-04-20 00:46:25 -0700964 writeln!(out, " {{");
965 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700966 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700967 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700968 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700969 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700970 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800971 writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -0700972 }
973 }
974 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700975 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700976 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700977 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700978 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700979 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700980 writeln!(out, "> return$;");
981 write!(out, " ");
982 } else if let Some(ret) = &sig.ret {
983 write!(out, "return ");
984 match ret {
985 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700986 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700987 write!(out, "::from_raw(");
988 }
989 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700990 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700991 write!(out, "(");
992 }
993 Type::Ref(_) => write!(out, "*"),
994 _ => {}
995 }
996 }
997 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700998 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700999 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001000 }
1001 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001002 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001003 if sig.receiver.is_some() {
1004 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001005 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001006 }
David Tolnay9d840362020-11-10 08:50:40 -08001007 for arg in &sig.args {
1008 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001009 write!(out, ", ");
1010 }
David Tolnay681f5c82021-01-01 22:56:27 -08001011 if out.types.needs_indirect_abi(&arg.ty) {
1012 write!(out, "&");
David Tolnay439cde22020-04-20 00:46:25 -07001013 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001014 write!(out, "{}", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -07001015 match &arg.ty {
1016 Type::RustBox(_) => write!(out, ".into_raw()"),
1017 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001018 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001019 _ => {}
1020 }
David Tolnay9d840362020-11-10 08:50:40 -08001021 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001022 }
1023 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001024 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001025 write!(out, ", ");
1026 }
1027 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001028 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001029 }
1030 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001031 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001032 write!(out, ", ");
1033 }
1034 write!(out, "extern$");
1035 }
1036 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001037 if !indirect_return {
1038 if let Some(ret) = &sig.ret {
David Tolnay681f5c82021-01-01 22:56:27 -08001039 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001040 write!(out, ")");
1041 }
David Tolnay439cde22020-04-20 00:46:25 -07001042 }
1043 }
1044 writeln!(out, ";");
1045 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001046 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001047 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001048 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001049 writeln!(out, " }}");
1050 }
1051 if indirect_return {
1052 out.include.utility = true;
1053 writeln!(out, " return ::std::move(return$.value);");
1054 }
1055 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001056}
1057
David Tolnaya7c2ea12020-10-30 21:32:53 -07001058fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001059 match ty {
1060 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001061 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001062 }
1063}
1064
David Tolnay75dca2e2020-03-25 20:17:52 -07001065fn indirect_return(sig: &Signature, types: &Types) -> bool {
1066 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001067 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001068 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001069}
1070
David Tolnaya7c2ea12020-10-30 21:32:53 -07001071fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001072 match ty {
1073 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001074 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001075 write!(out, "*");
1076 }
1077 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001078 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001079 write!(out, "const ");
1080 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001081 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001082 write!(out, " *");
1083 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001084 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001085 }
1086}
1087
David Tolnaya7c2ea12020-10-30 21:32:53 -07001088fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1089 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001090 match ty {
1091 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001092 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001093 _ => write_space_after_type(out, ty),
1094 }
1095}
1096
David Tolnaya7c2ea12020-10-30 21:32:53 -07001097fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001098 match ty {
1099 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001100 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001101 write!(out, "*");
1102 }
David Tolnay4a441222020-01-25 16:24:27 -08001103 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001104 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001105 write!(out, "const ");
1106 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001107 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001108 write!(out, " *");
1109 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001110 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1111 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001112 }
1113}
1114
David Tolnaya7c2ea12020-10-30 21:32:53 -07001115fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001116 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001117 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001118 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001119 write!(out, "*");
1120 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001121 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001122 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001123 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001124 write!(out, "*");
1125 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001126 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -04001127}
1128
David Tolnaya7c2ea12020-10-30 21:32:53 -07001129fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001130 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001131 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001132 Some(atom) => write_atom(out, atom),
David Tolnay1e5fe232021-01-01 18:11:40 -08001133 None => write!(
1134 out,
1135 "{}",
1136 out.types.resolve(ident).name.to_fully_qualified(),
1137 ),
David Tolnay7db73692019-10-20 14:51:12 -04001138 },
1139 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001140 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001141 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001142 write!(out, ">");
1143 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001144 Type::RustVec(ty) => {
1145 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001146 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001147 write!(out, ">");
1148 }
David Tolnay7db73692019-10-20 14:51:12 -04001149 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001150 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001151 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001152 write!(out, ">");
1153 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001154 Type::SharedPtr(ptr) => {
1155 write!(out, "::std::shared_ptr<");
1156 write_type(out, &ptr.inner);
1157 write!(out, ">");
1158 }
David Tolnay215e77f2020-12-28 17:09:48 -08001159 Type::WeakPtr(ptr) => {
1160 write!(out, "::std::weak_ptr<");
1161 write_type(out, &ptr.inner);
1162 write!(out, ">");
1163 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001164 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001165 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001166 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001167 write!(out, ">");
1168 }
David Tolnay7db73692019-10-20 14:51:12 -04001169 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001170 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001171 write!(out, "const ");
1172 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001173 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001174 write!(out, " &");
1175 }
1176 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001177 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001178 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001179 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001180 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001181 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001182 write!(out, "const ");
1183 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001184 write_type(out, &slice.inner);
1185 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001186 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001187 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001188 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001189 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001190 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001191 None => write!(out, "void"),
1192 }
1193 write!(out, "(");
1194 for (i, arg) in f.args.iter().enumerate() {
1195 if i > 0 {
1196 write!(out, ", ");
1197 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001198 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001199 }
1200 write!(out, ")>");
1201 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001202 Type::Array(a) => {
1203 write!(out, "::std::array<");
1204 write_type(out, &a.inner);
1205 write!(out, ", {}>", &a.len);
1206 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001207 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001208 }
1209}
1210
David Tolnayf6a89f22020-05-10 23:39:27 -07001211fn write_atom(out: &mut OutFile, atom: Atom) {
1212 match atom {
1213 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001214 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001215 U8 => write!(out, "::std::uint8_t"),
1216 U16 => write!(out, "::std::uint16_t"),
1217 U32 => write!(out, "::std::uint32_t"),
1218 U64 => write!(out, "::std::uint64_t"),
1219 Usize => write!(out, "::std::size_t"),
1220 I8 => write!(out, "::std::int8_t"),
1221 I16 => write!(out, "::std::int16_t"),
1222 I32 => write!(out, "::std::int32_t"),
1223 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001224 Isize => write!(out, "::rust::isize"),
1225 F32 => write!(out, "float"),
1226 F64 => write!(out, "double"),
1227 CxxString => write!(out, "::std::string"),
1228 RustString => write!(out, "::rust::String"),
1229 }
1230}
1231
David Tolnaya7c2ea12020-10-30 21:32:53 -07001232fn write_type_space(out: &mut OutFile, ty: &Type) {
1233 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001234 write_space_after_type(out, ty);
1235}
1236
1237fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001238 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001239 Type::Ident(_)
1240 | Type::RustBox(_)
1241 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001242 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001243 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001244 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001245 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001246 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001247 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001248 | Type::Fn(_)
1249 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001250 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001251 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001252 }
1253}
1254
David Tolnay1bdb4712020-11-25 07:27:54 -08001255#[derive(Copy, Clone)]
1256enum UniquePtr<'a> {
David Tolnay3abed472020-12-31 23:34:53 -08001257 Ident(&'a Ident),
1258 CxxVector(&'a Ident),
David Tolnay1bdb4712020-11-25 07:27:54 -08001259}
1260
1261trait ToTypename {
1262 fn to_typename(&self, types: &Types) -> String;
1263}
1264
David Tolnay3abed472020-12-31 23:34:53 -08001265impl ToTypename for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001266 fn to_typename(&self, types: &Types) -> String {
David Tolnay1e5fe232021-01-01 18:11:40 -08001267 types.resolve(self).name.to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001268 }
1269}
1270
David Tolnay1bdb4712020-11-25 07:27:54 -08001271impl<'a> ToTypename for UniquePtr<'a> {
1272 fn to_typename(&self, types: &Types) -> String {
1273 match self {
1274 UniquePtr::Ident(ident) => ident.to_typename(types),
1275 UniquePtr::CxxVector(element) => {
1276 format!("::std::vector<{}>", element.to_typename(types))
1277 }
1278 }
1279 }
1280}
1281
1282trait ToMangled {
1283 fn to_mangled(&self, types: &Types) -> Symbol;
1284}
1285
David Tolnay3abed472020-12-31 23:34:53 -08001286impl ToMangled for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001287 fn to_mangled(&self, types: &Types) -> Symbol {
David Tolnay1e5fe232021-01-01 18:11:40 -08001288 types.resolve(self).name.to_symbol()
David Tolnay1bdb4712020-11-25 07:27:54 -08001289 }
1290}
1291
1292impl<'a> ToMangled for UniquePtr<'a> {
1293 fn to_mangled(&self, types: &Types) -> Symbol {
1294 match self {
1295 UniquePtr::Ident(ident) => ident.to_mangled(types),
1296 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1297 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001298 }
1299}
1300
David Tolnaya7c2ea12020-10-30 21:32:53 -07001301fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001302 if out.header {
1303 return;
1304 }
1305
1306 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001307 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001308 out.begin_block(Block::ExternC);
David Tolnay3abed472020-12-31 23:34:53 -08001309 for impl_key in out.types.impls.keys() {
1310 out.next_section();
1311 match impl_key {
1312 ImplKey::RustBox(ident) => write_rust_box_extern(out, ident),
1313 ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident),
1314 ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident),
1315 ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident),
1316 ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident),
1317 ImplKey::CxxVector(ident) => write_cxx_vector(out, ident),
David Tolnay7db73692019-10-20 14:51:12 -04001318 }
1319 }
David Tolnay0c033e32020-11-01 15:15:48 -08001320 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001321
David Tolnay0c033e32020-11-01 15:15:48 -08001322 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001323 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3abed472020-12-31 23:34:53 -08001324 for impl_key in out.types.impls.keys() {
1325 match impl_key {
1326 ImplKey::RustBox(ident) => write_rust_box_impl(out, ident),
1327 ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident),
1328 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -04001329 }
1330 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001331 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001332 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001333}
1334
David Tolnay3abed472020-12-31 23:34:53 -08001335fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1336 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001337 let inner = resolve.name.to_fully_qualified();
1338 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001339
David Tolnay7db73692019-10-20 14:51:12 -04001340 writeln!(
1341 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001342 "{} *cxxbridge1$box${}$alloc() noexcept;",
1343 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001344 );
1345 writeln!(
1346 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001347 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1348 instance, inner,
1349 );
1350 writeln!(
1351 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001352 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001353 instance, inner,
1354 );
David Tolnay7db73692019-10-20 14:51:12 -04001355}
1356
David Tolnay3abed472020-12-31 23:34:53 -08001357fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001358 let inner = element.to_typename(out.types);
1359 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001360
David Tolnay12320e12020-12-09 23:09:36 -08001361 out.include.cstddef = true;
1362
Myron Ahneba35cf2020-02-05 19:41:51 +07001363 writeln!(
1364 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001365 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001366 instance, inner,
1367 );
1368 writeln!(
1369 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001370 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001371 instance, inner,
1372 );
1373 writeln!(
1374 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001375 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001376 instance, inner,
1377 );
David Tolnay219c0792020-04-24 20:31:37 -07001378 writeln!(
1379 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001380 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001381 instance, inner,
1382 );
1383 writeln!(
1384 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001385 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001386 inner, instance,
1387 );
David Tolnay503d0192020-04-24 22:18:56 -07001388 writeln!(
1389 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001390 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001391 instance, inner,
1392 );
1393 writeln!(
1394 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001395 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001396 instance, inner,
1397 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001398}
1399
David Tolnay3abed472020-12-31 23:34:53 -08001400fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1401 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001402 let inner = resolve.name.to_fully_qualified();
1403 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001404
1405 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001406 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001407 writeln!(
1408 out,
1409 "{} *Box<{}>::allocation::alloc() noexcept {{",
1410 inner, inner,
1411 );
David Tolnayc4b34222020-12-12 13:06:26 -08001412 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001413 writeln!(out, "}}");
1414
1415 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001416 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001417 writeln!(
1418 out,
David Tolnaye5703162020-12-12 16:26:35 -08001419 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001420 inner, inner,
1421 );
1422 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1423 writeln!(out, "}}");
1424
1425 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001426 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001427 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001428 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001429 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001430}
1431
David Tolnay3abed472020-12-31 23:34:53 -08001432fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001433 let inner = element.to_typename(out.types);
1434 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001435
David Tolnay12320e12020-12-09 23:09:36 -08001436 out.include.cstddef = true;
1437
Myron Ahneba35cf2020-02-05 19:41:51 +07001438 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001439 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001440 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001441 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001442 writeln!(out, "}}");
1443
1444 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001445 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001446 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001447 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001448 writeln!(out, "}}");
1449
1450 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001451 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001452 writeln!(
1453 out,
1454 "::std::size_t Vec<{}>::size() const noexcept {{",
1455 inner,
1456 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001457 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001458 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001459
1460 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001461 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001462 writeln!(
1463 out,
1464 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1465 inner,
1466 );
David Tolnay381d9532020-12-11 13:59:45 -08001467 writeln!(
1468 out,
1469 " return cxxbridge1$rust_vec${}$capacity(this);",
1470 instance,
1471 );
David Tolnaydc62d712020-12-11 13:51:53 -08001472 writeln!(out, "}}");
1473
1474 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001475 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001476 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001477 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001478 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001479
1480 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001481 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001482 writeln!(
1483 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001484 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001485 inner,
1486 );
1487 writeln!(
1488 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001489 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001490 instance,
1491 );
1492 writeln!(out, "}}");
1493
1494 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001495 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001496 writeln!(
1497 out,
1498 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1499 inner,
1500 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001501 writeln!(
1502 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001503 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001504 instance,
1505 );
1506 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001507}
1508
David Tolnay3abed472020-12-31 23:34:53 -08001509fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001510 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001511 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001512}
1513
1514// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001515fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001516 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001517 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001518 let inner = ty.to_typename(out.types);
1519 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001520
David Tolnay63da4d32020-04-25 09:41:12 -07001521 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001522 // Some aliases are to opaque types; some are to trivial types. We can't
1523 // know at code generation time, so we generate both C++ and Rust side
1524 // bindings for a "new" method anyway. But the Rust code can't be called
1525 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001526 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001527 out.types.structs.contains_key(ident)
1528 || out.types.enums.contains_key(ident)
1529 || out.types.aliases.contains_key(ident)
David Tolnayca0f9da2020-10-16 13:16:17 -07001530 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001531 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001532 };
1533
David Tolnay53462762020-11-25 07:08:10 -08001534 let conditional_delete = match ty {
1535 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001536 !out.types.structs.contains_key(ident) && !out.types.enums.contains_key(ident)
David Tolnay53462762020-11-25 07:08:10 -08001537 }
1538 UniquePtr::CxxVector(_) => false,
1539 };
1540
1541 if conditional_delete {
1542 out.builtin.is_complete = true;
1543 let definition = match ty {
David Tolnay1e5fe232021-01-01 18:11:40 -08001544 UniquePtr::Ident(ty) => &out.types.resolve(ty).name.cxx,
David Tolnay53462762020-11-25 07:08:10 -08001545 UniquePtr::CxxVector(_) => unreachable!(),
1546 };
1547 writeln!(
1548 out,
David Tolnay75068632020-12-26 22:15:17 -08001549 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001550 inner, definition,
1551 );
1552 }
David Tolnay7db73692019-10-20 14:51:12 -04001553 writeln!(
1554 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001555 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001556 inner,
1557 );
1558 writeln!(
1559 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001560 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001561 inner,
1562 );
1563 writeln!(
1564 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001565 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001566 instance, inner,
1567 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001568 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001569 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001570 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001571 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001572 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001573 out,
David Tolnay0b881402020-12-01 21:05:08 -08001574 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1575 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001576 );
David Tolnay63da4d32020-04-25 09:41:12 -07001577 writeln!(
1578 out,
David Tolnay0b881402020-12-01 21:05:08 -08001579 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1580 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001581 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001582 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001583 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001584 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001585 }
David Tolnay7db73692019-10-20 14:51:12 -04001586 writeln!(
1587 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001588 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001589 instance, inner, inner,
1590 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001591 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001592 writeln!(out, "}}");
1593 writeln!(
1594 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001595 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001596 inner, instance, inner,
1597 );
1598 writeln!(out, " return ptr.get();");
1599 writeln!(out, "}}");
1600 writeln!(
1601 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001602 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001603 inner, instance, inner,
1604 );
1605 writeln!(out, " return ptr.release();");
1606 writeln!(out, "}}");
1607 writeln!(
1608 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001609 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001610 instance, inner,
1611 );
David Tolnay53462762020-11-25 07:08:10 -08001612 if conditional_delete {
1613 out.builtin.deleter_if = true;
1614 writeln!(
1615 out,
David Tolnay75068632020-12-26 22:15:17 -08001616 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001617 inner,
1618 );
1619 } else {
1620 writeln!(out, " ptr->~unique_ptr();");
1621 }
David Tolnay7db73692019-10-20 14:51:12 -04001622 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001623}
Myron Ahneba35cf2020-02-05 19:41:51 +07001624
David Tolnay3abed472020-12-31 23:34:53 -08001625fn write_shared_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001626 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001627 let inner = resolve.name.to_fully_qualified();
1628 let instance = resolve.name.to_symbol();
David Tolnayb3b24a12020-12-01 15:27:43 -08001629
David Tolnayb3b24a12020-12-01 15:27:43 -08001630 out.include.new = true;
1631 out.include.utility = true;
1632
1633 // Some aliases are to opaque types; some are to trivial types. We can't
1634 // know at code generation time, so we generate both C++ and Rust side
1635 // bindings for a "new" method anyway. But the Rust code can't be called for
1636 // Opaque types because the 'new' method is not implemented.
David Tolnay3abed472020-12-31 23:34:53 -08001637 let can_construct_from_value = out.types.structs.contains_key(ident)
1638 || out.types.enums.contains_key(ident)
1639 || out.types.aliases.contains_key(ident);
David Tolnayb3b24a12020-12-01 15:27:43 -08001640
David Tolnayb3b24a12020-12-01 15:27:43 -08001641 writeln!(
1642 out,
1643 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1644 inner,
1645 );
1646 writeln!(
1647 out,
1648 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1649 inner,
1650 );
1651 writeln!(
1652 out,
1653 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1654 instance, inner,
1655 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001656 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001657 writeln!(out, "}}");
1658 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001659 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001660 writeln!(
1661 out,
David Tolnay0b881402020-12-01 21:05:08 -08001662 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1663 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001664 );
1665 writeln!(
1666 out,
David Tolnay0b881402020-12-01 21:05:08 -08001667 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1668 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001669 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001670 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001671 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001672 writeln!(out, "}}");
1673 }
1674 writeln!(
1675 out,
1676 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1677 instance, inner, inner,
1678 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001679 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001680 writeln!(out, "}}");
1681 writeln!(
1682 out,
1683 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1684 inner, instance, inner,
1685 );
1686 writeln!(out, " return self.get();");
1687 writeln!(out, "}}");
1688 writeln!(
1689 out,
1690 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1691 instance, inner,
1692 );
1693 writeln!(out, " self->~shared_ptr();");
1694 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001695}
1696
David Tolnay3abed472020-12-31 23:34:53 -08001697fn write_weak_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001698 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001699 let inner = resolve.name.to_fully_qualified();
1700 let instance = resolve.name.to_symbol();
David Tolnay215e77f2020-12-28 17:09:48 -08001701
1702 out.include.new = true;
1703 out.include.utility = true;
1704
1705 writeln!(
1706 out,
1707 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1708 inner,
1709 );
1710 writeln!(
1711 out,
1712 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1713 inner,
1714 );
1715 writeln!(
1716 out,
1717 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1718 instance, inner,
1719 );
1720 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1721 writeln!(out, "}}");
1722 writeln!(
1723 out,
1724 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1725 instance, inner, inner,
1726 );
1727 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1728 writeln!(out, "}}");
1729 writeln!(
1730 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001731 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1732 instance, inner, inner,
1733 );
1734 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1735 writeln!(out, "}}");
1736 writeln!(
1737 out,
David Tolnay7a487852020-12-28 18:02:25 -08001738 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1739 instance, inner, inner,
1740 );
1741 writeln!(
1742 out,
1743 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1744 inner,
1745 );
1746 writeln!(out, "}}");
1747 writeln!(
1748 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001749 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1750 instance, inner,
1751 );
1752 writeln!(out, " self->~weak_ptr();");
1753 writeln!(out, "}}");
1754}
1755
David Tolnay3abed472020-12-31 23:34:53 -08001756fn write_cxx_vector(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001757 let inner = element.to_typename(out.types);
1758 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001759
David Tolnay12320e12020-12-09 23:09:36 -08001760 out.include.cstddef = true;
1761
Myron Ahneba35cf2020-02-05 19:41:51 +07001762 writeln!(
1763 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001764 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001765 instance, inner,
1766 );
1767 writeln!(out, " return s.size();");
1768 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001769 writeln!(
1770 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001771 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001772 inner, instance, inner,
1773 );
David Tolnay767e00d2020-12-21 17:12:27 -08001774 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001775 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001776
David Tolnay70820672020-12-07 11:15:14 -08001777 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001778 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001779}