blob: 5b67e09b4c214858adcb8a314f721d1458de8bbe [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;
8use crate::syntax::set::UnorderedSet;
David Tolnay891061b2020-04-19 22:42:33 -07009use crate::syntax::symbol::Symbol;
David Tolnay5b1d8632020-12-20 22:05:11 -080010use crate::syntax::trivial::{self, TrivialReason};
Adrian Taylorc8713432020-10-21 18:20:55 -070011use crate::syntax::{
David Tolnay3abed472020-12-31 23:34:53 -080012 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, Signature, Struct, Trait, Type,
13 TypeAlias, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070014};
David Tolnay7db73692019-10-20 14:51:12 -040015use proc_macro2::Ident;
16
David Tolnayac7188c2020-11-01 16:58:16 -080017pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080018 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040019 let out = &mut out_file;
20
David Tolnaydfb82d72020-11-02 00:10:10 -080021 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070022 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023
David Tolnay7b0e5102020-11-01 23:22:12 -080024 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080025 write_data_structures(out, apis);
26 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080027 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040028
David Tolnay3374d8d2020-10-31 22:18:45 -070029 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070030 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070031
David Tolnayac7188c2020-11-01 16:58:16 -080032 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070033}
34
David Tolnay7b0e5102020-11-01 23:22:12 -080035fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
36 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080037 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080038 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080039 _ => false,
40 };
Adrian Taylorc8713432020-10-21 18:20:55 -070041
David Tolnay7b0e5102020-11-01 23:22:12 -080042 let apis_by_namespace =
43 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
44
David Tolnayd920be52020-11-01 23:34:30 -080045 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080046
David Tolnayd920be52020-11-01 23:34:30 -080047 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080048 let apis = ns_entries.direct_content();
49
50 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080051 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080052 match api {
David Tolnayed6ba4a2021-01-01 14:59:40 -080053 Api::Struct(strct) => write_struct_decl(out, &strct.name),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080054 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080055 Api::CxxType(ety) => write_struct_using(out, &ety.name),
David Tolnayed6ba4a2021-01-01 14:59:40 -080056 Api::RustType(ety) => write_struct_decl(out, &ety.name),
David Tolnay7b0e5102020-11-01 23:22:12 -080057 _ => unreachable!(),
58 }
David Tolnay7db73692019-10-20 14:51:12 -040059 }
David Tolnay7db73692019-10-20 14:51:12 -040060
David Tolnay7b0e5102020-11-01 23:22:12 -080061 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080062 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
63 write(out, nested_ns_entries, indent + 2);
64 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080065 }
Adrian Taylorf9213622020-10-31 22:25:42 -070066 }
67}
68
David Tolnaye1109d92020-11-01 20:51:56 -080069fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnaye352c1e2020-12-31 16:41:05 -080070 let mut methods_for_type = Map::new();
David Tolnay630af882020-10-31 22:03:47 -070071 for api in apis {
David Tolnay102c7ea2020-11-08 18:58:09 -080072 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayf94bef12020-04-17 14:46:42 -070073 if let Some(receiver) = &efn.sig.receiver {
74 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070075 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070076 .or_insert_with(Vec::new)
77 .push(efn);
78 }
79 }
80 }
Joel Galenson968738f2020-04-15 14:19:33 -070081
David Tolnaye352c1e2020-12-31 16:41:05 -080082 let mut structs_written = UnorderedSet::new();
David Tolnay5439fa12020-11-03 18:45:01 -080083 let mut toposorted_structs = out.types.toposorted_structs.iter();
David Tolnay9622b462020-11-02 21:34:42 -080084 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070085 match api {
David Tolnay5439fa12020-11-03 18:45:01 -080086 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
87 for next in &mut toposorted_structs {
88 if !out.types.cxx.contains(&strct.name.rust) {
89 out.next_section();
David Tolnay102c7ea2020-11-08 18:58:09 -080090 let methods = methods_for_type
91 .get(&strct.name.rust)
92 .map(Vec::as_slice)
93 .unwrap_or_default();
94 write_struct(out, next, methods);
David Tolnay5439fa12020-11-03 18:45:01 -080095 }
96 structs_written.insert(&next.name.rust);
97 if next.name.rust == strct.name.rust {
98 break;
99 }
100 }
101 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700102 Api::Enum(enm) => {
103 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800104 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700105 check_enum(out, enm);
106 } else {
107 write_enum(out, enm);
108 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700109 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700110 Api::RustType(ety) => {
David Tolnay8d067de2020-12-26 16:18:23 -0800111 out.next_section();
112 let methods = methods_for_type
113 .get(&ety.name.rust)
114 .map(Vec::as_slice)
115 .unwrap_or_default();
116 write_opaque_type(out, ety, methods);
David Tolnayc1fe0052020-04-17 15:15:06 -0700117 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700118 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400119 }
120 }
121
David Tolnay440e24a2021-01-01 23:25:34 -0800122 out.set_namespace(Default::default());
123
124 out.next_section();
125 for ty in out.types {
126 // MSVC workaround for "C linkage function cannot return C++ class"
127 // error. Apparently the compiler fails to perform implicit
128 // instantiations as part of an extern declaration. Instead we
129 // instantiate explicitly.
130 // See https://stackoverflow.com/a/57429504/6086311.
131 if let Type::SliceRef(_) = ty {
132 write!(out, "template struct ");
133 write_type(out, ty);
134 writeln!(out, ";");
135 }
136 }
137
David Tolnayfabca772020-10-03 21:25:41 -0700138 out.next_section();
139 for api in apis {
140 if let Api::TypeAlias(ety) = api {
Adrian Taylorf831a5a2020-12-07 16:49:19 -0800141 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
David Tolnay5b1d8632020-12-20 22:05:11 -0800142 check_trivial_extern_type(out, ety, reasons)
David Tolnayfabca772020-10-03 21:25:41 -0700143 }
144 }
145 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800146}
147
David Tolnaye1109d92020-11-01 20:51:56 -0800148fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700149 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400150 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800151 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800152 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800153 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800154 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
155 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
156 _ => {}
157 }
David Tolnay7db73692019-10-20 14:51:12 -0400158 }
David Tolnay7da38202020-11-27 17:36:16 -0800159
160 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400161 }
162
163 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800164 match api {
165 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800166 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800167 Api::RustFunction(efn) => {
168 out.next_section();
169 write_rust_function_shim(out, efn);
170 }
171 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400172 }
173 }
David Tolnay7db73692019-10-20 14:51:12 -0400174}
175
David Tolnay7da38202020-11-27 17:36:16 -0800176fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
177 out.set_namespace(Default::default());
178 out.begin_block(Block::Namespace("std"));
179
180 for api in apis {
181 if let Api::Struct(strct) = api {
182 if derive::contains(&strct.derives, Trait::Hash) {
183 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800184 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800185 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800186 let qualified = strct.name.to_fully_qualified();
187 writeln!(out, "template <> struct hash<{}> {{", qualified);
188 writeln!(
189 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800190 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800191 qualified,
192 );
193 let link_name = mangle::operator(&strct.name, "hash");
194 write!(out, " return ::");
195 for name in &strct.name.namespace {
196 write!(out, "{}::", name);
197 }
198 writeln!(out, "{}(self);", link_name);
199 writeln!(out, " }}");
200 writeln!(out, "}};");
201 }
202 }
203 }
204
205 out.end_block(Block::Namespace("std"));
206}
207
David Tolnaydfb82d72020-11-02 00:10:10 -0800208fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
209 for api in apis {
210 if let Api::Include(include) = api {
211 out.include.insert(include);
212 }
213 }
214
David Tolnaya7c2ea12020-10-30 21:32:53 -0700215 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400216 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700217 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700218 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
219 | Some(I64) => out.include.cstdint = true,
220 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800221 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700222 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800223 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800224 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700225 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800226 Type::RustBox(_) => out.builtin.rust_box = true,
227 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700228 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay215e77f2020-12-28 17:09:48 -0800229 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800230 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700231 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800232 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800233 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800234 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800235 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400236 }
237 }
David Tolnayec66d112020-10-31 21:00:26 -0700238}
David Tolnayf51447e2020-03-06 14:14:27 -0800239
David Tolnay102c7ea2020-11-08 18:58:09 -0800240fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800241 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800242 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800243
David Tolnay17a934c2020-11-02 00:40:04 -0800244 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800245 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700246 writeln!(out, "#ifndef {}", guard);
247 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400248 for line in strct.doc.to_string().lines() {
249 writeln!(out, "//{}", line);
250 }
David Tolnay17a934c2020-11-02 00:40:04 -0800251 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800252
David Tolnay7db73692019-10-20 14:51:12 -0400253 for field in &strct.fields {
David Tolnay5573e522020-12-31 11:07:38 -0800254 for line in field.doc.to_string().lines() {
255 writeln!(out, " //{}", line);
256 }
David Tolnay7db73692019-10-20 14:51:12 -0400257 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700258 write_type_space(out, &field.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800259 writeln!(out, "{};", field.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400260 }
David Tolnay84389352020-11-27 17:12:10 -0800261
David Tolnay5554ebd2020-12-10 11:34:41 -0800262 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800263
David Tolnay102c7ea2020-11-08 18:58:09 -0800264 for method in methods {
265 write!(out, " ");
266 let sig = &method.sig;
267 let local_name = method.name.cxx.to_string();
268 write_rust_function_shim_decl(out, &local_name, sig, false);
269 writeln!(out, ";");
270 }
David Tolnay84389352020-11-27 17:12:10 -0800271
David Tolnayb960ed22020-11-27 14:34:30 -0800272 if operator_eq {
273 writeln!(
274 out,
275 " bool operator==(const {} &) const noexcept;",
276 strct.name.cxx,
277 );
278 writeln!(
279 out,
280 " bool operator!=(const {} &) const noexcept;",
281 strct.name.cxx,
282 );
283 }
David Tolnay84389352020-11-27 17:12:10 -0800284
285 if operator_ord {
286 writeln!(
287 out,
288 " bool operator<(const {} &) const noexcept;",
289 strct.name.cxx,
290 );
291 writeln!(
292 out,
293 " bool operator<=(const {} &) const noexcept;",
294 strct.name.cxx,
295 );
296 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 }
307
David Tolnay5554ebd2020-12-10 11:34:41 -0800308 out.include.type_traits = true;
309 writeln!(out, " using IsRelocatable = ::std::true_type;");
310
David Tolnay7db73692019-10-20 14:51:12 -0400311 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700312 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400313}
314
David Tolnayed6ba4a2021-01-01 14:59:40 -0800315fn write_struct_decl(out: &mut OutFile, ident: &Pair) {
316 writeln!(out, "struct {};", ident.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400317}
318
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800319fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800320 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800321 write_atom(out, enm.repr);
322 writeln!(out, ";");
323}
324
David Tolnay8faec772020-11-02 00:18:19 -0800325fn write_struct_using(out: &mut OutFile, ident: &Pair) {
326 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800327}
328
David Tolnay8d067de2020-12-26 16:18:23 -0800329fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800330 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800331 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700332 writeln!(out, "#ifndef {}", guard);
333 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700334 for line in ety.doc.to_string().lines() {
335 writeln!(out, "//{}", line);
336 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800337
David Tolnay365fc7c2020-11-25 16:08:13 -0800338 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800339 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800340 out,
341 "struct {} final : public ::rust::Opaque {{",
342 ety.name.cxx,
343 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800344
Joel Galenson968738f2020-04-15 14:19:33 -0700345 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800346 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700347 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800348 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700349 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800350 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800351 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800352
David Tolnay8d067de2020-12-26 16:18:23 -0800353 if !methods.is_empty() {
354 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700355 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800356
David Tolnayee6ecfc2020-12-26 21:54:37 -0800357 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800358 out.include.cstddef = true;
359 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800360 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800361 writeln!(out, " struct layout {{");
362 writeln!(out, " static ::std::size_t size() noexcept;");
363 writeln!(out, " static ::std::size_t align() noexcept;");
364 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700365 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700366 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700367}
368
David Tolnay0b9b9f82020-11-01 20:41:00 -0800369fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800370 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800371 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700372 writeln!(out, "#ifndef {}", guard);
373 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700374 for line in enm.doc.to_string().lines() {
375 writeln!(out, "//{}", line);
376 }
David Tolnay17a934c2020-11-02 00:40:04 -0800377 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700378 write_atom(out, enm.repr);
379 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700380 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800381 for line in variant.doc.to_string().lines() {
382 writeln!(out, " //{}", line);
383 }
David Tolnaye6f62142020-12-21 16:00:41 -0800384 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700385 }
386 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700387 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700388}
389
David Tolnay0b9b9f82020-11-01 20:41:00 -0800390fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800391 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800392 out.include.type_traits = true;
393 writeln!(
394 out,
395 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
396 enm.name.cxx,
397 );
David Tolnay17a934c2020-11-02 00:40:04 -0800398 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700399 write_atom(out, enm.repr);
400 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700401 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700402 write!(out, "static_assert(static_cast<");
403 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700404 writeln!(
405 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700406 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800407 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700408 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700409 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700410}
411
David Tolnay5b1d8632020-12-20 22:05:11 -0800412fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800413 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700414 // necessary for soundness. That's because triviality is always declared by
415 // the user in the form of an unsafe impl of cxx::ExternType:
416 //
417 // unsafe impl ExternType for MyType {
418 // type Id = cxx::type_id!("...");
419 // type Kind = cxx::kind::Trivial;
420 // }
421 //
422 // Since the user went on the record with their unsafe impl to unsafely
423 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800424 // them if that were wrong. However, in practice correctly reasoning about
425 // the relocatability of C++ types is challenging, particularly if the type
426 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700427 //
David Tolnay174bd952020-11-02 09:23:12 -0800428 // There may be legitimate reasons to opt out of this assertion for support
429 // of types that the programmer knows are soundly Rust-movable despite not
430 // being recognized as such by the C++ type system due to a move constructor
431 // or destructor. To opt out of the relocatability check, they need to do
432 // one of the following things in any header used by `include!` in their
433 // bridge.
434 //
435 // --- if they define the type:
436 // struct MyType {
437 // ...
438 // + using IsRelocatable = std::true_type;
439 // };
440 //
441 // --- otherwise:
442 // + template <>
443 // + struct rust::IsRelocatable<MyType> : std::true_type {};
444 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700445
David Tolnay5b1d8632020-12-20 22:05:11 -0800446 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800447 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800448 writeln!(out, "static_assert(");
449 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
450 writeln!(
451 out,
452 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
453 id.trim_start_matches("::"),
454 trivial::as_what(&alias.name, reasons),
455 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700456}
457
David Tolnayb960ed22020-11-27 14:34:30 -0800458fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
459 out.set_namespace(&strct.name.namespace);
460 out.begin_block(Block::ExternC);
461
462 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800463 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800464 writeln!(
465 out,
466 "bool {}(const {1} &, const {1} &) noexcept;",
467 link_name, strct.name.cxx,
468 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800469
470 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800471 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800472 writeln!(
473 out,
474 "bool {}(const {1} &, const {1} &) noexcept;",
475 link_name, strct.name.cxx,
476 );
477 }
David Tolnayb960ed22020-11-27 14:34:30 -0800478 }
479
David Tolnay84389352020-11-27 17:12:10 -0800480 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800481 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800482 writeln!(
483 out,
484 "bool {}(const {1} &, const {1} &) noexcept;",
485 link_name, strct.name.cxx,
486 );
487
David Tolnaya05f9402020-11-27 17:44:05 -0800488 let link_name = mangle::operator(&strct.name, "le");
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
495 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800496 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800497 writeln!(
498 out,
499 "bool {}(const {1} &, const {1} &) noexcept;",
500 link_name, strct.name.cxx,
501 );
502
David Tolnaya05f9402020-11-27 17:44:05 -0800503 let link_name = mangle::operator(&strct.name, "ge");
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 }
510 }
511
David Tolnay7da38202020-11-27 17:36:16 -0800512 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800513 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800514 let link_name = mangle::operator(&strct.name, "hash");
515 writeln!(
516 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800517 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800518 link_name, strct.name.cxx,
519 );
520 }
521
David Tolnayb960ed22020-11-27 14:34:30 -0800522 out.end_block(Block::ExternC);
523}
524
525fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
526 if out.header {
527 return;
528 }
529
530 out.set_namespace(&strct.name.namespace);
531
532 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800533 out.next_section();
534 writeln!(
535 out,
536 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
537 strct.name.cxx,
538 );
David Tolnaya05f9402020-11-27 17:44:05 -0800539 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800540 writeln!(out, " return {}(*this, rhs);", link_name);
541 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800542
David Tolnayb960ed22020-11-27 14:34:30 -0800543 out.next_section();
544 writeln!(
545 out,
546 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
547 strct.name.cxx,
548 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800549 if derive::contains(&strct.derives, Trait::Eq) {
550 writeln!(out, " return !(*this == rhs);");
551 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800552 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800553 writeln!(out, " return {}(*this, rhs);", link_name);
554 }
David Tolnayb960ed22020-11-27 14:34:30 -0800555 writeln!(out, "}}");
556 }
David Tolnay84389352020-11-27 17:12:10 -0800557
558 if derive::contains(&strct.derives, Trait::PartialOrd) {
559 out.next_section();
560 writeln!(
561 out,
562 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
563 strct.name.cxx,
564 );
David Tolnaya05f9402020-11-27 17:44:05 -0800565 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800566 writeln!(out, " return {}(*this, rhs);", link_name);
567 writeln!(out, "}}");
568
569 out.next_section();
570 writeln!(
571 out,
572 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
573 strct.name.cxx,
574 );
David Tolnaya05f9402020-11-27 17:44:05 -0800575 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800576 writeln!(out, " return {}(*this, rhs);", link_name);
577 writeln!(out, "}}");
578
579 out.next_section();
580 writeln!(
581 out,
582 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
583 strct.name.cxx,
584 );
585 if derive::contains(&strct.derives, Trait::Ord) {
586 writeln!(out, " return !(*this <= rhs);");
587 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800588 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800589 writeln!(out, " return {}(*this, rhs);", link_name);
590 }
591 writeln!(out, "}}");
592
593 out.next_section();
594 writeln!(
595 out,
596 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
597 strct.name.cxx,
598 );
599 if derive::contains(&strct.derives, Trait::Ord) {
600 writeln!(out, " return !(*this < rhs);");
601 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800602 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800603 writeln!(out, " return {}(*this, rhs);", link_name);
604 }
605 writeln!(out, "}}");
606 }
David Tolnayb960ed22020-11-27 14:34:30 -0800607}
608
David Tolnay358bc4b2020-12-26 22:37:57 -0800609fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
610 out.set_namespace(&ety.name.namespace);
611 out.begin_block(Block::ExternC);
612
613 let link_name = mangle::operator(&ety.name, "sizeof");
614 writeln!(out, "::std::size_t {}() noexcept;", link_name);
615
616 let link_name = mangle::operator(&ety.name, "alignof");
617 writeln!(out, "::std::size_t {}() noexcept;", link_name);
618
619 out.end_block(Block::ExternC);
620}
621
622fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
623 if out.header {
624 return;
625 }
626
627 out.set_namespace(&ety.name.namespace);
628
629 out.next_section();
630 let link_name = mangle::operator(&ety.name, "sizeof");
631 writeln!(
632 out,
633 "::std::size_t {}::layout::size() noexcept {{",
634 ety.name.cxx,
635 );
636 writeln!(out, " return {}();", link_name);
637 writeln!(out, "}}");
638
639 out.next_section();
640 let link_name = mangle::operator(&ety.name, "alignof");
641 writeln!(
642 out,
643 "::std::size_t {}::layout::align() noexcept {{",
644 ety.name.cxx,
645 );
646 writeln!(out, " return {}();", link_name);
647 writeln!(out, "}}");
648}
649
David Tolnay1eadd262020-12-29 16:42:08 -0800650fn begin_function_definition(out: &mut OutFile) {
651 if let Some(annotation) = &out.opt.cxx_impl_annotations {
652 write!(out, "{} ", annotation);
653 }
654}
655
David Tolnay0b9b9f82020-11-01 20:41:00 -0800656fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800657 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800658 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800659 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800660 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700661 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700662 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700663 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700664 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700665 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700666 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700667 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700668 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700669 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800670 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700671 write!(out, "const ");
672 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700673 write!(
674 out,
675 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800676 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700677 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700678 }
David Tolnay7db73692019-10-20 14:51:12 -0400679 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700680 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400681 write!(out, ", ");
682 }
David Tolnaya46a2372020-03-06 10:03:48 -0800683 if arg.ty == RustString {
684 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700685 } else if let Type::RustVec(_) = arg.ty {
686 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800687 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700688 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400689 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700690 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400691 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700692 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400693 write!(out, ", ");
694 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700695 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400696 write!(out, "*return$");
697 }
698 writeln!(out, ") noexcept {{");
699 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700700 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700701 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800702 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700703 Some(receiver) => write!(
704 out,
705 "({}::*{}$)(",
David Tolnay1e5fe232021-01-01 18:11:40 -0800706 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800707 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700708 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700709 }
David Tolnay7db73692019-10-20 14:51:12 -0400710 for (i, arg) in efn.args.iter().enumerate() {
711 if i > 0 {
712 write!(out, ", ");
713 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700714 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400715 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700716 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700717 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800718 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700719 write!(out, " const");
720 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700721 }
722 write!(out, " = ");
723 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800724 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700725 Some(receiver) => write!(
726 out,
727 "&{}::{}",
David Tolnay1e5fe232021-01-01 18:11:40 -0800728 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800729 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700730 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700731 }
732 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400733 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700734 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700735 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700736 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700737 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700738 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700739 writeln!(out, " [&] {{");
740 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700741 }
David Tolnay7db73692019-10-20 14:51:12 -0400742 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700743 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400744 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700745 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400746 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700747 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400748 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700749 }
750 match &efn.ret {
751 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay73b72642020-11-25 17:44:05 -0800752 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700753 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800754 write!(out, "::rust::impl<");
755 write_type(out, ty);
756 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700757 }
David Tolnay99642622020-03-25 13:07:35 -0700758 _ => {}
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 Tolnay73b72642020-11-25 17:44:05 -0800785 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800786 write_type(out, &arg.ty);
787 write!(out, "(static_cast<");
788 if slice.mutability.is_none() {
789 write!(out, "const ");
790 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800791 write_type_space(out, &slice.inner);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800792 write!(out, "*>({0}.ptr), {0}.len)", arg.name.cxx);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700793 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700794 out.include.utility = true;
David Tolnay84ed6ad2021-01-01 15:30:14 -0800795 write!(out, "::std::move(*{})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400796 } else {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800797 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400798 }
799 }
800 write!(out, ")");
801 match &efn.ret {
802 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
803 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay7f65e7c2021-01-01 22:50:50 -0800804 Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400805 _ => {}
806 }
807 if indirect_return {
808 write!(out, ")");
809 }
810 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700811 if efn.throws {
812 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800813 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700814 writeln!(out, " throw$.ptr = nullptr;");
815 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700816 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700817 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700818 writeln!(
819 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800820 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700821 );
David Tolnay5d121442020-03-17 22:14:40 -0700822 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700823 writeln!(out, " return throw$;");
824 }
David Tolnay7db73692019-10-20 14:51:12 -0400825 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700826 for arg in &efn.args {
827 if let Type::Fn(f) = &arg.ty {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800828 let var = &arg.name;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700829 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700830 }
831 }
David Tolnayca563ee2020-11-01 20:12:27 -0800832 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700833}
834
David Tolnay84ed6ad2021-01-01 15:30:14 -0800835fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700836 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700837 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700838 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700839
840 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700841 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
842 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400843}
844
David Tolnay0b9b9f82020-11-01 20:41:00 -0800845fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800846 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800847 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700848 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700849 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700850 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800851 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700852}
853
854fn write_rust_function_decl_impl(
855 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700856 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700857 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700858 indirect_call: bool,
859) {
David Tolnay04770742020-11-01 13:50:50 -0800860 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700861 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700862 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700863 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700864 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700865 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700866 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700867 write!(out, "{}(", link_name);
868 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700869 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800870 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700871 write!(out, "const ");
872 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700873 write!(
874 out,
875 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800876 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700877 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700878 needs_comma = true;
879 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700880 for arg in &sig.args {
881 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400882 write!(out, ", ");
883 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700884 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700885 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400886 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700887 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700888 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400889 write!(out, ", ");
890 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700891 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400892 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700893 needs_comma = true;
894 }
895 if indirect_call {
896 if needs_comma {
897 write!(out, ", ");
898 }
899 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400900 }
901 writeln!(out, ") noexcept;");
902}
903
David Tolnay0b9b9f82020-11-01 20:41:00 -0800904fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800905 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400906 for line in efn.doc.to_string().lines() {
907 writeln!(out, "//{}", line);
908 }
David Tolnaya73853b2020-04-20 01:19:56 -0700909 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800910 None => efn.name.cxx.to_string(),
David Tolnay1e5fe232021-01-01 18:11:40 -0800911 Some(receiver) => format!(
912 "{}::{}",
913 out.types.resolve(&receiver.ty).name.cxx,
914 efn.name.cxx,
915 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700916 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700917 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700918 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700919 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700920}
921
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700922fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700923 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700924 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700925 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700926 indirect_call: bool,
927) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700928 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700929 write!(out, "{}(", local_name);
930 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400931 if i > 0 {
932 write!(out, ", ");
933 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700934 write_type_space(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800935 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400936 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700937 if indirect_call {
938 if !sig.args.is_empty() {
939 write!(out, ", ");
940 }
941 write!(out, "void *extern$");
942 }
David Tolnay1e548172020-03-16 13:37:09 -0700943 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700944 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800945 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700946 write!(out, " const");
947 }
948 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700949 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700950 write!(out, " noexcept");
951 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700952}
953
954fn write_rust_function_shim_impl(
955 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700956 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700957 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700958 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700959 indirect_call: bool,
960) {
961 if out.header && sig.receiver.is_some() {
962 // We've already defined this inside the struct.
963 return;
964 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800965 if !out.header {
966 begin_function_definition(out);
967 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700968 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400969 if out.header {
970 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700971 return;
David Tolnay7db73692019-10-20 14:51:12 -0400972 }
David Tolnay439cde22020-04-20 00:46:25 -0700973 writeln!(out, " {{");
974 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700975 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700976 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700977 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700978 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700979 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800980 writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -0700981 }
982 }
983 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700984 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700985 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700986 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700987 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700988 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700989 writeln!(out, "> return$;");
990 write!(out, " ");
991 } else if let Some(ret) = &sig.ret {
992 write!(out, "return ");
993 match ret {
994 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700995 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700996 write!(out, "::from_raw(");
997 }
998 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700999 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -07001000 write!(out, "(");
1001 }
1002 Type::Ref(_) => write!(out, "*"),
David Tolnay73b72642020-11-25 17:44:05 -08001003 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001004 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001005 write!(out, "::rust::impl<");
1006 write_type(out, ret);
1007 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001008 }
David Tolnay439cde22020-04-20 00:46:25 -07001009 _ => {}
1010 }
1011 }
1012 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -07001013 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001014 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001015 }
1016 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001017 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001018 if sig.receiver.is_some() {
1019 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001020 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001021 }
David Tolnay9d840362020-11-10 08:50:40 -08001022 for arg in &sig.args {
1023 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001024 write!(out, ", ");
1025 }
1026 match &arg.ty {
David Tolnay73b72642020-11-25 17:44:05 -08001027 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001028 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001029 write!(out, "::rust::impl<");
1030 write_type(out, &arg.ty);
1031 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001032 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001033 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -07001034 _ => {}
1035 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001036 write!(out, "{}", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -07001037 match &arg.ty {
1038 Type::RustBox(_) => write!(out, ".into_raw()"),
1039 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay7f65e7c2021-01-01 22:50:50 -08001040 Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001041 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001042 _ => {}
1043 }
David Tolnay9d840362020-11-10 08:50:40 -08001044 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001045 }
1046 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001047 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001048 write!(out, ", ");
1049 }
1050 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001051 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001052 }
1053 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001054 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001055 write!(out, ", ");
1056 }
1057 write!(out, "extern$");
1058 }
1059 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001060 if !indirect_return {
1061 if let Some(ret) = &sig.ret {
David Tolnay7f65e7c2021-01-01 22:50:50 -08001062 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001063 write!(out, ")");
1064 }
David Tolnay439cde22020-04-20 00:46:25 -07001065 }
1066 }
1067 writeln!(out, ";");
1068 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001069 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001070 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001071 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001072 writeln!(out, " }}");
1073 }
1074 if indirect_return {
1075 out.include.utility = true;
1076 writeln!(out, " return ::std::move(return$.value);");
1077 }
1078 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001079}
1080
David Tolnaya7c2ea12020-10-30 21:32:53 -07001081fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001082 match ty {
1083 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001084 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001085 }
1086}
1087
David Tolnay75dca2e2020-03-25 20:17:52 -07001088fn indirect_return(sig: &Signature, types: &Types) -> bool {
1089 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001090 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001091 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001092}
1093
David Tolnaya7c2ea12020-10-30 21:32:53 -07001094fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001095 match ty {
1096 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001097 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001098 write!(out, "*");
1099 }
1100 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001101 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001102 write!(out, "const ");
1103 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001104 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001105 write!(out, " *");
1106 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001107 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001108 }
1109}
1110
David Tolnaya7c2ea12020-10-30 21:32:53 -07001111fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1112 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001113 match ty {
1114 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001115 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001116 _ => write_space_after_type(out, ty),
1117 }
1118}
1119
David Tolnaya7c2ea12020-10-30 21:32:53 -07001120fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001121 match ty {
1122 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001123 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001124 write!(out, "*");
1125 }
David Tolnay4a441222020-01-25 16:24:27 -08001126 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001127 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001128 write!(out, "const ");
1129 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001130 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001131 write!(out, " *");
1132 }
David Tolnay7f65e7c2021-01-01 22:50:50 -08001133 Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001134 out.builtin.ptr_len = true;
1135 write!(out, "::rust::repr::PtrLen ");
1136 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001137 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1138 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001139 }
1140}
1141
David Tolnaya7c2ea12020-10-30 21:32:53 -07001142fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001143 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001144 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001145 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001146 write!(out, "*");
1147 }
David Tolnay7f65e7c2021-01-01 22:50:50 -08001148 Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001149 out.builtin.ptr_len = true;
1150 write!(out, "::rust::repr::PtrLen ");
1151 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001152 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001153 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001154 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001155 write!(out, "*");
1156 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001157 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -04001158}
1159
David Tolnaya7c2ea12020-10-30 21:32:53 -07001160fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001161 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001162 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001163 Some(atom) => write_atom(out, atom),
David Tolnay1e5fe232021-01-01 18:11:40 -08001164 None => write!(
1165 out,
1166 "{}",
1167 out.types.resolve(ident).name.to_fully_qualified(),
1168 ),
David Tolnay7db73692019-10-20 14:51:12 -04001169 },
1170 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001171 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001172 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001173 write!(out, ">");
1174 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001175 Type::RustVec(ty) => {
1176 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001177 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001178 write!(out, ">");
1179 }
David Tolnay7db73692019-10-20 14:51:12 -04001180 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001181 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001182 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001183 write!(out, ">");
1184 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001185 Type::SharedPtr(ptr) => {
1186 write!(out, "::std::shared_ptr<");
1187 write_type(out, &ptr.inner);
1188 write!(out, ">");
1189 }
David Tolnay215e77f2020-12-28 17:09:48 -08001190 Type::WeakPtr(ptr) => {
1191 write!(out, "::std::weak_ptr<");
1192 write_type(out, &ptr.inner);
1193 write!(out, ">");
1194 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001195 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001196 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001197 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001198 write!(out, ">");
1199 }
David Tolnay7db73692019-10-20 14:51:12 -04001200 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001201 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001202 write!(out, "const ");
1203 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001204 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001205 write!(out, " &");
1206 }
1207 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001208 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001209 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001210 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001211 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001212 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001213 write!(out, "const ");
1214 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001215 write_type(out, &slice.inner);
1216 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001217 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001218 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001219 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001220 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001221 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001222 None => write!(out, "void"),
1223 }
1224 write!(out, "(");
1225 for (i, arg) in f.args.iter().enumerate() {
1226 if i > 0 {
1227 write!(out, ", ");
1228 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001229 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001230 }
1231 write!(out, ")>");
1232 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001233 Type::Array(a) => {
1234 write!(out, "::std::array<");
1235 write_type(out, &a.inner);
1236 write!(out, ", {}>", &a.len);
1237 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001238 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001239 }
1240}
1241
David Tolnayf6a89f22020-05-10 23:39:27 -07001242fn write_atom(out: &mut OutFile, atom: Atom) {
1243 match atom {
1244 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001245 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001246 U8 => write!(out, "::std::uint8_t"),
1247 U16 => write!(out, "::std::uint16_t"),
1248 U32 => write!(out, "::std::uint32_t"),
1249 U64 => write!(out, "::std::uint64_t"),
1250 Usize => write!(out, "::std::size_t"),
1251 I8 => write!(out, "::std::int8_t"),
1252 I16 => write!(out, "::std::int16_t"),
1253 I32 => write!(out, "::std::int32_t"),
1254 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001255 Isize => write!(out, "::rust::isize"),
1256 F32 => write!(out, "float"),
1257 F64 => write!(out, "double"),
1258 CxxString => write!(out, "::std::string"),
1259 RustString => write!(out, "::rust::String"),
1260 }
1261}
1262
David Tolnaya7c2ea12020-10-30 21:32:53 -07001263fn write_type_space(out: &mut OutFile, ty: &Type) {
1264 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001265 write_space_after_type(out, ty);
1266}
1267
1268fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001269 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001270 Type::Ident(_)
1271 | Type::RustBox(_)
1272 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001273 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001274 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001275 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001276 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001277 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001278 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001279 | Type::Fn(_)
1280 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001281 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001282 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001283 }
1284}
1285
David Tolnay1bdb4712020-11-25 07:27:54 -08001286#[derive(Copy, Clone)]
1287enum UniquePtr<'a> {
David Tolnay3abed472020-12-31 23:34:53 -08001288 Ident(&'a Ident),
1289 CxxVector(&'a Ident),
David Tolnay1bdb4712020-11-25 07:27:54 -08001290}
1291
1292trait ToTypename {
1293 fn to_typename(&self, types: &Types) -> String;
1294}
1295
David Tolnay3abed472020-12-31 23:34:53 -08001296impl ToTypename for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001297 fn to_typename(&self, types: &Types) -> String {
David Tolnay1e5fe232021-01-01 18:11:40 -08001298 types.resolve(self).name.to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001299 }
1300}
1301
David Tolnay1bdb4712020-11-25 07:27:54 -08001302impl<'a> ToTypename for UniquePtr<'a> {
1303 fn to_typename(&self, types: &Types) -> String {
1304 match self {
1305 UniquePtr::Ident(ident) => ident.to_typename(types),
1306 UniquePtr::CxxVector(element) => {
1307 format!("::std::vector<{}>", element.to_typename(types))
1308 }
1309 }
1310 }
1311}
1312
1313trait ToMangled {
1314 fn to_mangled(&self, types: &Types) -> Symbol;
1315}
1316
David Tolnay3abed472020-12-31 23:34:53 -08001317impl ToMangled for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001318 fn to_mangled(&self, types: &Types) -> Symbol {
David Tolnay1e5fe232021-01-01 18:11:40 -08001319 types.resolve(self).name.to_symbol()
David Tolnay1bdb4712020-11-25 07:27:54 -08001320 }
1321}
1322
1323impl<'a> ToMangled for UniquePtr<'a> {
1324 fn to_mangled(&self, types: &Types) -> Symbol {
1325 match self {
1326 UniquePtr::Ident(ident) => ident.to_mangled(types),
1327 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1328 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001329 }
1330}
1331
David Tolnaya7c2ea12020-10-30 21:32:53 -07001332fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001333 if out.header {
1334 return;
1335 }
1336
1337 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001338 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001339 out.begin_block(Block::ExternC);
David Tolnay3abed472020-12-31 23:34:53 -08001340 for impl_key in out.types.impls.keys() {
1341 out.next_section();
1342 match impl_key {
1343 ImplKey::RustBox(ident) => write_rust_box_extern(out, ident),
1344 ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident),
1345 ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident),
1346 ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident),
1347 ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident),
1348 ImplKey::CxxVector(ident) => write_cxx_vector(out, ident),
David Tolnay7db73692019-10-20 14:51:12 -04001349 }
1350 }
David Tolnay0c033e32020-11-01 15:15:48 -08001351 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001352
David Tolnay0c033e32020-11-01 15:15:48 -08001353 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001354 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3abed472020-12-31 23:34:53 -08001355 for impl_key in out.types.impls.keys() {
1356 match impl_key {
1357 ImplKey::RustBox(ident) => write_rust_box_impl(out, ident),
1358 ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident),
1359 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -04001360 }
1361 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001362 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001363 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001364}
1365
David Tolnay3abed472020-12-31 23:34:53 -08001366fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1367 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001368 let inner = resolve.name.to_fully_qualified();
1369 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001370
David Tolnay7db73692019-10-20 14:51:12 -04001371 writeln!(
1372 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001373 "{} *cxxbridge1$box${}$alloc() noexcept;",
1374 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001375 );
1376 writeln!(
1377 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001378 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1379 instance, inner,
1380 );
1381 writeln!(
1382 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001383 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001384 instance, inner,
1385 );
David Tolnay7db73692019-10-20 14:51:12 -04001386}
1387
David Tolnay3abed472020-12-31 23:34:53 -08001388fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001389 let inner = element.to_typename(out.types);
1390 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001391
David Tolnay12320e12020-12-09 23:09:36 -08001392 out.include.cstddef = true;
1393
Myron Ahneba35cf2020-02-05 19:41:51 +07001394 writeln!(
1395 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001396 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001397 instance, inner,
1398 );
1399 writeln!(
1400 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001401 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001402 instance, inner,
1403 );
1404 writeln!(
1405 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001406 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001407 instance, inner,
1408 );
David Tolnay219c0792020-04-24 20:31:37 -07001409 writeln!(
1410 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001411 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001412 instance, inner,
1413 );
1414 writeln!(
1415 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001416 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001417 inner, instance,
1418 );
David Tolnay503d0192020-04-24 22:18:56 -07001419 writeln!(
1420 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001421 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001422 instance, inner,
1423 );
1424 writeln!(
1425 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001426 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001427 instance, inner,
1428 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001429}
1430
David Tolnay3abed472020-12-31 23:34:53 -08001431fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1432 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001433 let inner = resolve.name.to_fully_qualified();
1434 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001435
1436 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001437 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001438 writeln!(
1439 out,
1440 "{} *Box<{}>::allocation::alloc() noexcept {{",
1441 inner, inner,
1442 );
David Tolnayc4b34222020-12-12 13:06:26 -08001443 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001444 writeln!(out, "}}");
1445
1446 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001447 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001448 writeln!(
1449 out,
David Tolnaye5703162020-12-12 16:26:35 -08001450 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001451 inner, inner,
1452 );
1453 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1454 writeln!(out, "}}");
1455
1456 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001457 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001458 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001459 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001460 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001461}
1462
David Tolnay3abed472020-12-31 23:34:53 -08001463fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001464 let inner = element.to_typename(out.types);
1465 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001466
David Tolnay12320e12020-12-09 23:09:36 -08001467 out.include.cstddef = true;
1468
Myron Ahneba35cf2020-02-05 19:41:51 +07001469 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001470 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001471 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001472 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001473 writeln!(out, "}}");
1474
1475 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001476 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001477 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001478 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001479 writeln!(out, "}}");
1480
1481 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001482 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001483 writeln!(
1484 out,
1485 "::std::size_t Vec<{}>::size() const noexcept {{",
1486 inner,
1487 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001488 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001489 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001490
1491 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001492 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001493 writeln!(
1494 out,
1495 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1496 inner,
1497 );
David Tolnay381d9532020-12-11 13:59:45 -08001498 writeln!(
1499 out,
1500 " return cxxbridge1$rust_vec${}$capacity(this);",
1501 instance,
1502 );
David Tolnaydc62d712020-12-11 13:51:53 -08001503 writeln!(out, "}}");
1504
1505 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001506 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001507 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001508 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001509 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001510
1511 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001512 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001513 writeln!(
1514 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001515 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001516 inner,
1517 );
1518 writeln!(
1519 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001520 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001521 instance,
1522 );
1523 writeln!(out, "}}");
1524
1525 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001526 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001527 writeln!(
1528 out,
1529 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1530 inner,
1531 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001532 writeln!(
1533 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001534 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001535 instance,
1536 );
1537 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001538}
1539
David Tolnay3abed472020-12-31 23:34:53 -08001540fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001541 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001542 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001543}
1544
1545// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001546fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001547 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001548 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001549 let inner = ty.to_typename(out.types);
1550 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001551
David Tolnay63da4d32020-04-25 09:41:12 -07001552 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001553 // Some aliases are to opaque types; some are to trivial types. We can't
1554 // know at code generation time, so we generate both C++ and Rust side
1555 // bindings for a "new" method anyway. But the Rust code can't be called
1556 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001557 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001558 out.types.structs.contains_key(ident)
1559 || out.types.enums.contains_key(ident)
1560 || out.types.aliases.contains_key(ident)
David Tolnayca0f9da2020-10-16 13:16:17 -07001561 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001562 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001563 };
1564
David Tolnay53462762020-11-25 07:08:10 -08001565 let conditional_delete = match ty {
1566 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001567 !out.types.structs.contains_key(ident) && !out.types.enums.contains_key(ident)
David Tolnay53462762020-11-25 07:08:10 -08001568 }
1569 UniquePtr::CxxVector(_) => false,
1570 };
1571
1572 if conditional_delete {
1573 out.builtin.is_complete = true;
1574 let definition = match ty {
David Tolnay1e5fe232021-01-01 18:11:40 -08001575 UniquePtr::Ident(ty) => &out.types.resolve(ty).name.cxx,
David Tolnay53462762020-11-25 07:08:10 -08001576 UniquePtr::CxxVector(_) => unreachable!(),
1577 };
1578 writeln!(
1579 out,
David Tolnay75068632020-12-26 22:15:17 -08001580 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001581 inner, definition,
1582 );
1583 }
David Tolnay7db73692019-10-20 14:51:12 -04001584 writeln!(
1585 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001586 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001587 inner,
1588 );
1589 writeln!(
1590 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001591 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001592 inner,
1593 );
1594 writeln!(
1595 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001596 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001597 instance, inner,
1598 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001599 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001600 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001601 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001602 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001603 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001604 out,
David Tolnay0b881402020-12-01 21:05:08 -08001605 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1606 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001607 );
David Tolnay63da4d32020-04-25 09:41:12 -07001608 writeln!(
1609 out,
David Tolnay0b881402020-12-01 21:05:08 -08001610 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1611 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001612 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001613 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001614 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001615 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001616 }
David Tolnay7db73692019-10-20 14:51:12 -04001617 writeln!(
1618 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001619 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001620 instance, inner, inner,
1621 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001622 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001623 writeln!(out, "}}");
1624 writeln!(
1625 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001626 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001627 inner, instance, inner,
1628 );
1629 writeln!(out, " return ptr.get();");
1630 writeln!(out, "}}");
1631 writeln!(
1632 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001633 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001634 inner, instance, inner,
1635 );
1636 writeln!(out, " return ptr.release();");
1637 writeln!(out, "}}");
1638 writeln!(
1639 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001640 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001641 instance, inner,
1642 );
David Tolnay53462762020-11-25 07:08:10 -08001643 if conditional_delete {
1644 out.builtin.deleter_if = true;
1645 writeln!(
1646 out,
David Tolnay75068632020-12-26 22:15:17 -08001647 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001648 inner,
1649 );
1650 } else {
1651 writeln!(out, " ptr->~unique_ptr();");
1652 }
David Tolnay7db73692019-10-20 14:51:12 -04001653 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001654}
Myron Ahneba35cf2020-02-05 19:41:51 +07001655
David Tolnay3abed472020-12-31 23:34:53 -08001656fn write_shared_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001657 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001658 let inner = resolve.name.to_fully_qualified();
1659 let instance = resolve.name.to_symbol();
David Tolnayb3b24a12020-12-01 15:27:43 -08001660
David Tolnayb3b24a12020-12-01 15:27:43 -08001661 out.include.new = true;
1662 out.include.utility = true;
1663
1664 // Some aliases are to opaque types; some are to trivial types. We can't
1665 // know at code generation time, so we generate both C++ and Rust side
1666 // bindings for a "new" method anyway. But the Rust code can't be called for
1667 // Opaque types because the 'new' method is not implemented.
David Tolnay3abed472020-12-31 23:34:53 -08001668 let can_construct_from_value = out.types.structs.contains_key(ident)
1669 || out.types.enums.contains_key(ident)
1670 || out.types.aliases.contains_key(ident);
David Tolnayb3b24a12020-12-01 15:27:43 -08001671
David Tolnayb3b24a12020-12-01 15:27:43 -08001672 writeln!(
1673 out,
1674 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1675 inner,
1676 );
1677 writeln!(
1678 out,
1679 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1680 inner,
1681 );
1682 writeln!(
1683 out,
1684 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1685 instance, inner,
1686 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001687 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001688 writeln!(out, "}}");
1689 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001690 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001691 writeln!(
1692 out,
David Tolnay0b881402020-12-01 21:05:08 -08001693 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1694 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001695 );
1696 writeln!(
1697 out,
David Tolnay0b881402020-12-01 21:05:08 -08001698 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1699 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001700 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001701 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001702 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001703 writeln!(out, "}}");
1704 }
1705 writeln!(
1706 out,
1707 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1708 instance, inner, inner,
1709 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001710 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001711 writeln!(out, "}}");
1712 writeln!(
1713 out,
1714 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1715 inner, instance, inner,
1716 );
1717 writeln!(out, " return self.get();");
1718 writeln!(out, "}}");
1719 writeln!(
1720 out,
1721 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1722 instance, inner,
1723 );
1724 writeln!(out, " self->~shared_ptr();");
1725 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001726}
1727
David Tolnay3abed472020-12-31 23:34:53 -08001728fn write_weak_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001729 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001730 let inner = resolve.name.to_fully_qualified();
1731 let instance = resolve.name.to_symbol();
David Tolnay215e77f2020-12-28 17:09:48 -08001732
1733 out.include.new = true;
1734 out.include.utility = true;
1735
1736 writeln!(
1737 out,
1738 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1739 inner,
1740 );
1741 writeln!(
1742 out,
1743 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1744 inner,
1745 );
1746 writeln!(
1747 out,
1748 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1749 instance, inner,
1750 );
1751 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1752 writeln!(out, "}}");
1753 writeln!(
1754 out,
1755 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1756 instance, inner, inner,
1757 );
1758 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1759 writeln!(out, "}}");
1760 writeln!(
1761 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001762 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1763 instance, inner, inner,
1764 );
1765 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1766 writeln!(out, "}}");
1767 writeln!(
1768 out,
David Tolnay7a487852020-12-28 18:02:25 -08001769 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1770 instance, inner, inner,
1771 );
1772 writeln!(
1773 out,
1774 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1775 inner,
1776 );
1777 writeln!(out, "}}");
1778 writeln!(
1779 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001780 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1781 instance, inner,
1782 );
1783 writeln!(out, " self->~weak_ptr();");
1784 writeln!(out, "}}");
1785}
1786
David Tolnay3abed472020-12-31 23:34:53 -08001787fn write_cxx_vector(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001788 let inner = element.to_typename(out.types);
1789 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001790
David Tolnay12320e12020-12-09 23:09:36 -08001791 out.include.cstddef = true;
1792
Myron Ahneba35cf2020-02-05 19:41:51 +07001793 writeln!(
1794 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001795 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001796 instance, inner,
1797 );
1798 writeln!(out, " return s.size();");
1799 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001800 writeln!(
1801 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001802 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001803 inner, instance, inner,
1804 );
David Tolnay767e00d2020-12-21 17:12:27 -08001805 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001806 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001807
David Tolnay70820672020-12-07 11:15:14 -08001808 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001809 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001810}