blob: c6331f4dbde4898e7f80e221419ea34d5f76ee8e [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 Tolnay8b229532021-01-02 18:30:45 -0800122 if out.header {
123 return;
124 }
125
David Tolnay440e24a2021-01-01 23:25:34 -0800126 out.set_namespace(Default::default());
127
David Tolnay8b229532021-01-02 18:30:45 -0800128 // MSVC workaround for "C linkage function cannot return C++ class" error.
129 // Apparently the compiler fails to perform implicit instantiations as part
130 // of an extern declaration return type. Instead we instantiate explicitly.
131 // See https://stackoverflow.com/a/57429504/6086311.
132 out.next_section();
133 let mut slice_in_return_position = OrderedSet::new();
134 for api in apis {
135 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayfe77f332021-01-02 17:39:51 -0800136 if let Some(ty @ Type::Str(_)) | Some(ty @ Type::SliceRef(_)) = &efn.ret {
David Tolnay8b229532021-01-02 18:30:45 -0800137 slice_in_return_position.insert(ty);
David Tolnay14dc7922021-01-02 01:07:16 -0800138 }
David Tolnay440e24a2021-01-01 23:25:34 -0800139 }
David Tolnay8b229532021-01-02 18:30:45 -0800140 }
141 for ty in &slice_in_return_position {
David Tolnayfe77f332021-01-02 17:39:51 -0800142 write!(out, "template class ::rust::repr::Fat<");
David Tolnay8b229532021-01-02 18:30:45 -0800143 write_type(out, ty);
David Tolnayfe77f332021-01-02 17:39:51 -0800144 writeln!(out, ">;");
David Tolnay8b229532021-01-02 18:30:45 -0800145 }
David Tolnay440e24a2021-01-01 23:25:34 -0800146
David Tolnay8b229532021-01-02 18:30:45 -0800147 out.next_section();
148 for api in apis {
149 if let Api::TypeAlias(ety) = api {
150 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
151 check_trivial_extern_type(out, ety, reasons)
David Tolnayfabca772020-10-03 21:25:41 -0700152 }
153 }
154 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800155}
156
David Tolnaye1109d92020-11-01 20:51:56 -0800157fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700158 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400159 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800160 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800161 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800162 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800163 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
164 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
165 _ => {}
166 }
David Tolnay7db73692019-10-20 14:51:12 -0400167 }
David Tolnay7da38202020-11-27 17:36:16 -0800168
169 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400170 }
171
172 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800173 match api {
174 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800175 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800176 Api::RustFunction(efn) => {
177 out.next_section();
178 write_rust_function_shim(out, efn);
179 }
180 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400181 }
182 }
David Tolnay7db73692019-10-20 14:51:12 -0400183}
184
David Tolnay7da38202020-11-27 17:36:16 -0800185fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
186 out.set_namespace(Default::default());
187 out.begin_block(Block::Namespace("std"));
188
189 for api in apis {
190 if let Api::Struct(strct) = api {
191 if derive::contains(&strct.derives, Trait::Hash) {
192 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800193 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800194 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800195 let qualified = strct.name.to_fully_qualified();
196 writeln!(out, "template <> struct hash<{}> {{", qualified);
197 writeln!(
198 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800199 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800200 qualified,
201 );
202 let link_name = mangle::operator(&strct.name, "hash");
203 write!(out, " return ::");
204 for name in &strct.name.namespace {
205 write!(out, "{}::", name);
206 }
207 writeln!(out, "{}(self);", link_name);
208 writeln!(out, " }}");
209 writeln!(out, "}};");
210 }
211 }
212 }
213
214 out.end_block(Block::Namespace("std"));
215}
216
David Tolnaydfb82d72020-11-02 00:10:10 -0800217fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
218 for api in apis {
219 if let Api::Include(include) = api {
220 out.include.insert(include);
221 }
222 }
223
David Tolnaya7c2ea12020-10-30 21:32:53 -0700224 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400225 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700226 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700227 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
228 | Some(I64) => out.include.cstdint = true,
229 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800230 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700231 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800232 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800233 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700234 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800235 Type::RustBox(_) => out.builtin.rust_box = true,
236 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700237 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay215e77f2020-12-28 17:09:48 -0800238 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800239 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700240 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800241 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800242 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800243 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800244 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400245 }
246 }
David Tolnayec66d112020-10-31 21:00:26 -0700247}
David Tolnayf51447e2020-03-06 14:14:27 -0800248
David Tolnay102c7ea2020-11-08 18:58:09 -0800249fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800250 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800251 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800252
David Tolnay17a934c2020-11-02 00:40:04 -0800253 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800254 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700255 writeln!(out, "#ifndef {}", guard);
256 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400257 for line in strct.doc.to_string().lines() {
258 writeln!(out, "//{}", line);
259 }
David Tolnay17a934c2020-11-02 00:40:04 -0800260 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800261
David Tolnay7db73692019-10-20 14:51:12 -0400262 for field in &strct.fields {
David Tolnay5573e522020-12-31 11:07:38 -0800263 for line in field.doc.to_string().lines() {
264 writeln!(out, " //{}", line);
265 }
David Tolnay7db73692019-10-20 14:51:12 -0400266 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700267 write_type_space(out, &field.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800268 writeln!(out, "{};", field.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400269 }
David Tolnay84389352020-11-27 17:12:10 -0800270
David Tolnay5554ebd2020-12-10 11:34:41 -0800271 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800272
David Tolnay102c7ea2020-11-08 18:58:09 -0800273 for method in methods {
274 write!(out, " ");
275 let sig = &method.sig;
276 let local_name = method.name.cxx.to_string();
277 write_rust_function_shim_decl(out, &local_name, sig, false);
278 writeln!(out, ";");
279 }
David Tolnay84389352020-11-27 17:12:10 -0800280
David Tolnayb960ed22020-11-27 14:34:30 -0800281 if operator_eq {
282 writeln!(
283 out,
284 " bool operator==(const {} &) const noexcept;",
285 strct.name.cxx,
286 );
287 writeln!(
288 out,
289 " bool operator!=(const {} &) const noexcept;",
290 strct.name.cxx,
291 );
292 }
David Tolnay84389352020-11-27 17:12:10 -0800293
294 if operator_ord {
295 writeln!(
296 out,
297 " bool operator<(const {} &) const noexcept;",
298 strct.name.cxx,
299 );
300 writeln!(
301 out,
302 " bool operator<=(const {} &) const noexcept;",
303 strct.name.cxx,
304 );
305 writeln!(
306 out,
307 " bool operator>(const {} &) const noexcept;",
308 strct.name.cxx,
309 );
310 writeln!(
311 out,
312 " bool operator>=(const {} &) const noexcept;",
313 strct.name.cxx,
314 );
315 }
316
David Tolnay5554ebd2020-12-10 11:34:41 -0800317 out.include.type_traits = true;
318 writeln!(out, " using IsRelocatable = ::std::true_type;");
319
David Tolnay7db73692019-10-20 14:51:12 -0400320 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700321 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400322}
323
David Tolnayed6ba4a2021-01-01 14:59:40 -0800324fn write_struct_decl(out: &mut OutFile, ident: &Pair) {
325 writeln!(out, "struct {};", ident.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400326}
327
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800328fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800329 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800330 write_atom(out, enm.repr);
331 writeln!(out, ";");
332}
333
David Tolnay8faec772020-11-02 00:18:19 -0800334fn write_struct_using(out: &mut OutFile, ident: &Pair) {
335 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800336}
337
David Tolnay8d067de2020-12-26 16:18:23 -0800338fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800339 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800340 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700341 writeln!(out, "#ifndef {}", guard);
342 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700343 for line in ety.doc.to_string().lines() {
344 writeln!(out, "//{}", line);
345 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800346
David Tolnay365fc7c2020-11-25 16:08:13 -0800347 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800348 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800349 out,
350 "struct {} final : public ::rust::Opaque {{",
351 ety.name.cxx,
352 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800353
Joel Galenson968738f2020-04-15 14:19:33 -0700354 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800355 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700356 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800357 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700358 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800359 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800360 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800361
David Tolnay8f940cf2021-01-02 18:45:00 -0800362 writeln!(out, " ~{}() = delete;", ety.name.cxx);
363 writeln!(out);
David Tolnay6ba262f2020-12-26 22:23:50 -0800364
David Tolnayee6ecfc2020-12-26 21:54:37 -0800365 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800366 out.include.cstddef = true;
367 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800368 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800369 writeln!(out, " struct layout {{");
370 writeln!(out, " static ::std::size_t size() noexcept;");
371 writeln!(out, " static ::std::size_t align() noexcept;");
372 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700373 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700374 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700375}
376
David Tolnay0b9b9f82020-11-01 20:41:00 -0800377fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800378 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800379 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700380 writeln!(out, "#ifndef {}", guard);
381 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700382 for line in enm.doc.to_string().lines() {
383 writeln!(out, "//{}", line);
384 }
David Tolnay17a934c2020-11-02 00:40:04 -0800385 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700386 write_atom(out, enm.repr);
387 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700388 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800389 for line in variant.doc.to_string().lines() {
390 writeln!(out, " //{}", line);
391 }
David Tolnaye6f62142020-12-21 16:00:41 -0800392 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700393 }
394 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700395 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700396}
397
David Tolnay0b9b9f82020-11-01 20:41:00 -0800398fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800399 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800400 out.include.type_traits = true;
401 writeln!(
402 out,
403 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
404 enm.name.cxx,
405 );
David Tolnay17a934c2020-11-02 00:40:04 -0800406 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700407 write_atom(out, enm.repr);
408 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700409 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700410 write!(out, "static_assert(static_cast<");
411 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700412 writeln!(
413 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700414 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800415 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700416 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700417 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700418}
419
David Tolnay5b1d8632020-12-20 22:05:11 -0800420fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800421 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700422 // necessary for soundness. That's because triviality is always declared by
423 // the user in the form of an unsafe impl of cxx::ExternType:
424 //
425 // unsafe impl ExternType for MyType {
426 // type Id = cxx::type_id!("...");
427 // type Kind = cxx::kind::Trivial;
428 // }
429 //
430 // Since the user went on the record with their unsafe impl to unsafely
431 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800432 // them if that were wrong. However, in practice correctly reasoning about
433 // the relocatability of C++ types is challenging, particularly if the type
434 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700435 //
David Tolnay174bd952020-11-02 09:23:12 -0800436 // There may be legitimate reasons to opt out of this assertion for support
437 // of types that the programmer knows are soundly Rust-movable despite not
438 // being recognized as such by the C++ type system due to a move constructor
439 // or destructor. To opt out of the relocatability check, they need to do
440 // one of the following things in any header used by `include!` in their
441 // bridge.
442 //
443 // --- if they define the type:
444 // struct MyType {
445 // ...
446 // + using IsRelocatable = std::true_type;
447 // };
448 //
449 // --- otherwise:
450 // + template <>
451 // + struct rust::IsRelocatable<MyType> : std::true_type {};
452 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700453
David Tolnay5b1d8632020-12-20 22:05:11 -0800454 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800455 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800456 writeln!(out, "static_assert(");
457 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
458 writeln!(
459 out,
460 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
461 id.trim_start_matches("::"),
462 trivial::as_what(&alias.name, reasons),
463 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700464}
465
David Tolnayb960ed22020-11-27 14:34:30 -0800466fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
467 out.set_namespace(&strct.name.namespace);
468 out.begin_block(Block::ExternC);
469
470 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800471 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800472 writeln!(
473 out,
474 "bool {}(const {1} &, const {1} &) noexcept;",
475 link_name, strct.name.cxx,
476 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800477
478 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800479 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800480 writeln!(
481 out,
482 "bool {}(const {1} &, const {1} &) noexcept;",
483 link_name, strct.name.cxx,
484 );
485 }
David Tolnayb960ed22020-11-27 14:34:30 -0800486 }
487
David Tolnay84389352020-11-27 17:12:10 -0800488 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800489 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800490 writeln!(
491 out,
492 "bool {}(const {1} &, const {1} &) noexcept;",
493 link_name, strct.name.cxx,
494 );
495
David Tolnaya05f9402020-11-27 17:44:05 -0800496 let link_name = mangle::operator(&strct.name, "le");
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
503 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800504 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800505 writeln!(
506 out,
507 "bool {}(const {1} &, const {1} &) noexcept;",
508 link_name, strct.name.cxx,
509 );
510
David Tolnaya05f9402020-11-27 17:44:05 -0800511 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800512 writeln!(
513 out,
514 "bool {}(const {1} &, const {1} &) noexcept;",
515 link_name, strct.name.cxx,
516 );
517 }
518 }
519
David Tolnay7da38202020-11-27 17:36:16 -0800520 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800521 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800522 let link_name = mangle::operator(&strct.name, "hash");
523 writeln!(
524 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800525 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800526 link_name, strct.name.cxx,
527 );
528 }
529
David Tolnayb960ed22020-11-27 14:34:30 -0800530 out.end_block(Block::ExternC);
531}
532
533fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
534 if out.header {
535 return;
536 }
537
538 out.set_namespace(&strct.name.namespace);
539
540 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800541 out.next_section();
542 writeln!(
543 out,
544 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
545 strct.name.cxx,
546 );
David Tolnaya05f9402020-11-27 17:44:05 -0800547 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800548 writeln!(out, " return {}(*this, rhs);", link_name);
549 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800550
David Tolnayb960ed22020-11-27 14:34:30 -0800551 out.next_section();
552 writeln!(
553 out,
554 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
555 strct.name.cxx,
556 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800557 if derive::contains(&strct.derives, Trait::Eq) {
558 writeln!(out, " return !(*this == rhs);");
559 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800560 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800561 writeln!(out, " return {}(*this, rhs);", link_name);
562 }
David Tolnayb960ed22020-11-27 14:34:30 -0800563 writeln!(out, "}}");
564 }
David Tolnay84389352020-11-27 17:12:10 -0800565
566 if derive::contains(&strct.derives, Trait::PartialOrd) {
567 out.next_section();
568 writeln!(
569 out,
570 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
571 strct.name.cxx,
572 );
David Tolnaya05f9402020-11-27 17:44:05 -0800573 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800574 writeln!(out, " return {}(*this, rhs);", link_name);
575 writeln!(out, "}}");
576
577 out.next_section();
578 writeln!(
579 out,
580 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
581 strct.name.cxx,
582 );
David Tolnaya05f9402020-11-27 17:44:05 -0800583 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800584 writeln!(out, " return {}(*this, rhs);", link_name);
585 writeln!(out, "}}");
586
587 out.next_section();
588 writeln!(
589 out,
590 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
591 strct.name.cxx,
592 );
593 if derive::contains(&strct.derives, Trait::Ord) {
594 writeln!(out, " return !(*this <= rhs);");
595 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800596 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800597 writeln!(out, " return {}(*this, rhs);", link_name);
598 }
599 writeln!(out, "}}");
600
601 out.next_section();
602 writeln!(
603 out,
604 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
605 strct.name.cxx,
606 );
607 if derive::contains(&strct.derives, Trait::Ord) {
608 writeln!(out, " return !(*this < rhs);");
609 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800610 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800611 writeln!(out, " return {}(*this, rhs);", link_name);
612 }
613 writeln!(out, "}}");
614 }
David Tolnayb960ed22020-11-27 14:34:30 -0800615}
616
David Tolnay358bc4b2020-12-26 22:37:57 -0800617fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
618 out.set_namespace(&ety.name.namespace);
619 out.begin_block(Block::ExternC);
620
621 let link_name = mangle::operator(&ety.name, "sizeof");
622 writeln!(out, "::std::size_t {}() noexcept;", link_name);
623
624 let link_name = mangle::operator(&ety.name, "alignof");
625 writeln!(out, "::std::size_t {}() noexcept;", link_name);
626
627 out.end_block(Block::ExternC);
628}
629
630fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
631 if out.header {
632 return;
633 }
634
635 out.set_namespace(&ety.name.namespace);
636
637 out.next_section();
638 let link_name = mangle::operator(&ety.name, "sizeof");
639 writeln!(
640 out,
641 "::std::size_t {}::layout::size() noexcept {{",
642 ety.name.cxx,
643 );
644 writeln!(out, " return {}();", link_name);
645 writeln!(out, "}}");
646
647 out.next_section();
648 let link_name = mangle::operator(&ety.name, "alignof");
649 writeln!(
650 out,
651 "::std::size_t {}::layout::align() noexcept {{",
652 ety.name.cxx,
653 );
654 writeln!(out, " return {}();", link_name);
655 writeln!(out, "}}");
656}
657
David Tolnay1eadd262020-12-29 16:42:08 -0800658fn begin_function_definition(out: &mut OutFile) {
659 if let Some(annotation) = &out.opt.cxx_impl_annotations {
660 write!(out, "{} ", annotation);
661 }
662}
663
David Tolnay0b9b9f82020-11-01 20:41:00 -0800664fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800665 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800666 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800667 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800668 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700669 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700670 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700671 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700672 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700673 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700674 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700675 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700676 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700677 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800678 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700679 write!(out, "const ");
680 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700681 write!(
682 out,
683 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800684 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700685 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700686 }
David Tolnay7db73692019-10-20 14:51:12 -0400687 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700688 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400689 write!(out, ", ");
690 }
David Tolnaya46a2372020-03-06 10:03:48 -0800691 if arg.ty == RustString {
692 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700693 } else if let Type::RustVec(_) = arg.ty {
694 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800695 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700696 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400697 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700698 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400699 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700700 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400701 write!(out, ", ");
702 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700703 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400704 write!(out, "*return$");
705 }
706 writeln!(out, ") noexcept {{");
707 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700708 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700709 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800710 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700711 Some(receiver) => write!(
712 out,
713 "({}::*{}$)(",
David Tolnay1e5fe232021-01-01 18:11:40 -0800714 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800715 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700716 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700717 }
David Tolnay7db73692019-10-20 14:51:12 -0400718 for (i, arg) in efn.args.iter().enumerate() {
719 if i > 0 {
720 write!(out, ", ");
721 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700722 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400723 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700724 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700725 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800726 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700727 write!(out, " const");
728 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700729 }
730 write!(out, " = ");
731 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800732 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700733 Some(receiver) => write!(
734 out,
735 "&{}::{}",
David Tolnay1e5fe232021-01-01 18:11:40 -0800736 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800737 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700738 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700739 }
740 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400741 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700742 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700743 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700744 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700745 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700746 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700747 writeln!(out, " [&] {{");
748 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700749 }
David Tolnay7db73692019-10-20 14:51:12 -0400750 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700751 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400752 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700753 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400754 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700755 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400756 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700757 }
David Tolnayfe77f332021-01-02 17:39:51 -0800758 match &efn.ret {
759 Some(Type::Ref(_)) => write!(out, "&"),
760 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, "{{"),
761 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400762 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700763 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800764 None => write!(out, "{}$(", efn.name.rust),
765 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700766 }
David Tolnay7db73692019-10-20 14:51:12 -0400767 for (i, arg) in efn.args.iter().enumerate() {
768 if i > 0 {
769 write!(out, ", ");
770 }
771 if let Type::RustBox(_) = &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, "::from_raw({})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400774 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700775 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800776 write!(out, "({})", arg.name.cxx);
David Tolnaya46a2372020-03-06 10:03:48 -0800777 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700778 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800779 write!(
780 out,
781 "::rust::String(::rust::unsafe_bitcopy, *{})",
David Tolnay84ed6ad2021-01-01 15:30:14 -0800782 arg.name.cxx,
David Tolnaycc3767f2020-03-06 10:41:51 -0800783 );
David Tolnay313b10e2020-04-25 16:30:51 -0700784 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700785 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700786 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800787 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.name.cxx);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700788 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700789 out.include.utility = true;
David Tolnay84ed6ad2021-01-01 15:30:14 -0800790 write!(out, "::std::move(*{})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400791 } else {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800792 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400793 }
794 }
795 write!(out, ")");
796 match &efn.ret {
797 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
798 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnayfe77f332021-01-02 17:39:51 -0800799 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, "}}"),
David Tolnay7db73692019-10-20 14:51:12 -0400800 _ => {}
801 }
802 if indirect_return {
803 write!(out, ")");
804 }
805 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700806 if efn.throws {
807 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800808 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700809 writeln!(out, " throw$.ptr = nullptr;");
810 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700811 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700812 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700813 writeln!(
814 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800815 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700816 );
David Tolnay5d121442020-03-17 22:14:40 -0700817 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700818 writeln!(out, " return throw$;");
819 }
David Tolnay7db73692019-10-20 14:51:12 -0400820 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700821 for arg in &efn.args {
822 if let Type::Fn(f) = &arg.ty {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800823 let var = &arg.name;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700824 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700825 }
826 }
David Tolnayca563ee2020-11-01 20:12:27 -0800827 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700828}
829
David Tolnay84ed6ad2021-01-01 15:30:14 -0800830fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700831 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700832 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700833 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700834
835 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700836 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
837 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400838}
839
David Tolnay0b9b9f82020-11-01 20:41:00 -0800840fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800841 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800842 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700843 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700844 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700845 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800846 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700847}
848
849fn write_rust_function_decl_impl(
850 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700851 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700852 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 indirect_call: bool,
854) {
David Tolnay04770742020-11-01 13:50:50 -0800855 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700856 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700857 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700858 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700859 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700860 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700861 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700862 write!(out, "{}(", link_name);
863 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700864 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800865 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700866 write!(out, "const ");
867 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700868 write!(
869 out,
870 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800871 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700872 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700873 needs_comma = true;
874 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700875 for arg in &sig.args {
876 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400877 write!(out, ", ");
878 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700879 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700880 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400881 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700882 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700883 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400884 write!(out, ", ");
885 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700886 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400887 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700888 needs_comma = true;
889 }
890 if indirect_call {
891 if needs_comma {
892 write!(out, ", ");
893 }
894 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400895 }
896 writeln!(out, ") noexcept;");
897}
898
David Tolnay0b9b9f82020-11-01 20:41:00 -0800899fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800900 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400901 for line in efn.doc.to_string().lines() {
902 writeln!(out, "//{}", line);
903 }
David Tolnaya73853b2020-04-20 01:19:56 -0700904 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800905 None => efn.name.cxx.to_string(),
David Tolnay1e5fe232021-01-01 18:11:40 -0800906 Some(receiver) => format!(
907 "{}::{}",
908 out.types.resolve(&receiver.ty).name.cxx,
909 efn.name.cxx,
910 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700911 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700912 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700913 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700914 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700915}
916
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700917fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700918 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700919 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700920 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700921 indirect_call: bool,
922) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700923 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700924 write!(out, "{}(", local_name);
925 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400926 if i > 0 {
927 write!(out, ", ");
928 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700929 write_type_space(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800930 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400931 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700932 if indirect_call {
933 if !sig.args.is_empty() {
934 write!(out, ", ");
935 }
936 write!(out, "void *extern$");
937 }
David Tolnay1e548172020-03-16 13:37:09 -0700938 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700939 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800940 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700941 write!(out, " const");
942 }
943 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700944 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700945 write!(out, " noexcept");
946 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700947}
948
949fn write_rust_function_shim_impl(
950 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700951 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700952 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700953 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700954 indirect_call: bool,
955) {
956 if out.header && sig.receiver.is_some() {
957 // We've already defined this inside the struct.
958 return;
959 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800960 if !out.header {
961 begin_function_definition(out);
962 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700963 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400964 if out.header {
965 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700966 return;
David Tolnay7db73692019-10-20 14:51:12 -0400967 }
David Tolnay439cde22020-04-20 00:46:25 -0700968 writeln!(out, " {{");
969 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700970 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700971 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700972 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700973 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700974 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800975 writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -0700976 }
977 }
978 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700979 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700980 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700981 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700982 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700983 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700984 writeln!(out, "> return$;");
985 write!(out, " ");
986 } else if let Some(ret) = &sig.ret {
987 write!(out, "return ");
988 match ret {
989 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700990 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700991 write!(out, "::from_raw(");
992 }
993 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700994 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700995 write!(out, "(");
996 }
997 Type::Ref(_) => write!(out, "*"),
998 _ => {}
999 }
1000 }
1001 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -07001002 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001003 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001004 }
1005 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001006 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001007 if sig.receiver.is_some() {
1008 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001009 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001010 }
David Tolnay9d840362020-11-10 08:50:40 -08001011 for arg in &sig.args {
1012 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001013 write!(out, ", ");
1014 }
David Tolnay681f5c82021-01-01 22:56:27 -08001015 if out.types.needs_indirect_abi(&arg.ty) {
1016 write!(out, "&");
David Tolnay439cde22020-04-20 00:46:25 -07001017 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001018 write!(out, "{}", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -07001019 match &arg.ty {
1020 Type::RustBox(_) => write!(out, ".into_raw()"),
1021 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001022 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001023 _ => {}
1024 }
David Tolnay9d840362020-11-10 08:50:40 -08001025 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001026 }
1027 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001028 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001029 write!(out, ", ");
1030 }
1031 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001032 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001033 }
1034 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001035 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001036 write!(out, ", ");
1037 }
1038 write!(out, "extern$");
1039 }
1040 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001041 if !indirect_return {
1042 if let Some(ret) = &sig.ret {
David Tolnayfe77f332021-01-02 17:39:51 -08001043 match ret {
1044 Type::RustBox(_) | Type::UniquePtr(_) => write!(out, ")"),
1045 Type::Str(_) | Type::SliceRef(_) => write!(out, ".repr"),
1046 _ => {}
David Tolnay22602b42020-09-21 18:04:05 -04001047 }
David Tolnay439cde22020-04-20 00:46:25 -07001048 }
1049 }
1050 writeln!(out, ";");
1051 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001052 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001053 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001054 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001055 writeln!(out, " }}");
1056 }
1057 if indirect_return {
1058 out.include.utility = true;
1059 writeln!(out, " return ::std::move(return$.value);");
1060 }
1061 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001062}
1063
David Tolnaya7c2ea12020-10-30 21:32:53 -07001064fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001065 match ty {
1066 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001067 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001068 }
1069}
1070
David Tolnay75dca2e2020-03-25 20:17:52 -07001071fn indirect_return(sig: &Signature, types: &Types) -> bool {
1072 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001073 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001074 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001075}
1076
David Tolnaya7c2ea12020-10-30 21:32:53 -07001077fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001078 match ty {
1079 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001080 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001081 write!(out, "*");
1082 }
1083 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001084 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001085 write!(out, "const ");
1086 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001087 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001088 write!(out, " *");
1089 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001090 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001091 }
1092}
1093
David Tolnaya7c2ea12020-10-30 21:32:53 -07001094fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1095 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001096 match ty {
1097 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001098 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001099 _ => write_space_after_type(out, ty),
1100 }
1101}
1102
David Tolnaya7c2ea12020-10-30 21:32:53 -07001103fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001104 match ty {
1105 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001106 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001107 write!(out, "*");
1108 }
David Tolnay4a441222020-01-25 16:24:27 -08001109 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001110 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001111 write!(out, "const ");
1112 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001113 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001114 write!(out, " *");
1115 }
David Tolnayfe77f332021-01-02 17:39:51 -08001116 Some(ty @ Type::Str(_)) | Some(ty @ Type::SliceRef(_)) => {
1117 out.builtin.repr_fat = true;
1118 write!(out, "::rust::repr::Fat<");
1119 write_type(out, ty);
1120 write!(out, "> ");
1121 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001122 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1123 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001124 }
1125}
1126
David Tolnaya7c2ea12020-10-30 21:32:53 -07001127fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001128 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001129 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001130 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001131 write!(out, "*");
1132 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001133 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001134 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001135 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001136 write!(out, "*");
1137 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001138 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -04001139}
1140
David Tolnaya7c2ea12020-10-30 21:32:53 -07001141fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001142 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001143 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001144 Some(atom) => write_atom(out, atom),
David Tolnay1e5fe232021-01-01 18:11:40 -08001145 None => write!(
1146 out,
1147 "{}",
1148 out.types.resolve(ident).name.to_fully_qualified(),
1149 ),
David Tolnay7db73692019-10-20 14:51:12 -04001150 },
1151 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001152 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001153 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001154 write!(out, ">");
1155 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001156 Type::RustVec(ty) => {
1157 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001158 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001159 write!(out, ">");
1160 }
David Tolnay7db73692019-10-20 14:51:12 -04001161 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001162 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001163 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001164 write!(out, ">");
1165 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001166 Type::SharedPtr(ptr) => {
1167 write!(out, "::std::shared_ptr<");
1168 write_type(out, &ptr.inner);
1169 write!(out, ">");
1170 }
David Tolnay215e77f2020-12-28 17:09:48 -08001171 Type::WeakPtr(ptr) => {
1172 write!(out, "::std::weak_ptr<");
1173 write_type(out, &ptr.inner);
1174 write!(out, ">");
1175 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001176 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001177 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001178 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001179 write!(out, ">");
1180 }
David Tolnay7db73692019-10-20 14:51:12 -04001181 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001182 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001183 write!(out, "const ");
1184 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001185 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001186 write!(out, " &");
1187 }
1188 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001189 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001190 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001191 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001192 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001193 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001194 write!(out, "const ");
1195 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001196 write_type(out, &slice.inner);
1197 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001198 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001199 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001200 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001201 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001202 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001203 None => write!(out, "void"),
1204 }
1205 write!(out, "(");
1206 for (i, arg) in f.args.iter().enumerate() {
1207 if i > 0 {
1208 write!(out, ", ");
1209 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001210 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001211 }
1212 write!(out, ")>");
1213 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001214 Type::Array(a) => {
1215 write!(out, "::std::array<");
1216 write_type(out, &a.inner);
1217 write!(out, ", {}>", &a.len);
1218 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001219 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001220 }
1221}
1222
David Tolnayf6a89f22020-05-10 23:39:27 -07001223fn write_atom(out: &mut OutFile, atom: Atom) {
1224 match atom {
1225 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001226 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001227 U8 => write!(out, "::std::uint8_t"),
1228 U16 => write!(out, "::std::uint16_t"),
1229 U32 => write!(out, "::std::uint32_t"),
1230 U64 => write!(out, "::std::uint64_t"),
1231 Usize => write!(out, "::std::size_t"),
1232 I8 => write!(out, "::std::int8_t"),
1233 I16 => write!(out, "::std::int16_t"),
1234 I32 => write!(out, "::std::int32_t"),
1235 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001236 Isize => write!(out, "::rust::isize"),
1237 F32 => write!(out, "float"),
1238 F64 => write!(out, "double"),
1239 CxxString => write!(out, "::std::string"),
1240 RustString => write!(out, "::rust::String"),
1241 }
1242}
1243
David Tolnaya7c2ea12020-10-30 21:32:53 -07001244fn write_type_space(out: &mut OutFile, ty: &Type) {
1245 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001246 write_space_after_type(out, ty);
1247}
1248
1249fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001250 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001251 Type::Ident(_)
1252 | Type::RustBox(_)
1253 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001254 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001255 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001256 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001257 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001258 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001259 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001260 | Type::Fn(_)
1261 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001262 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001263 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001264 }
1265}
1266
David Tolnay1bdb4712020-11-25 07:27:54 -08001267#[derive(Copy, Clone)]
1268enum UniquePtr<'a> {
David Tolnay3abed472020-12-31 23:34:53 -08001269 Ident(&'a Ident),
1270 CxxVector(&'a Ident),
David Tolnay1bdb4712020-11-25 07:27:54 -08001271}
1272
1273trait ToTypename {
1274 fn to_typename(&self, types: &Types) -> String;
1275}
1276
David Tolnay3abed472020-12-31 23:34:53 -08001277impl ToTypename for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001278 fn to_typename(&self, types: &Types) -> String {
David Tolnay1e5fe232021-01-01 18:11:40 -08001279 types.resolve(self).name.to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001280 }
1281}
1282
David Tolnay1bdb4712020-11-25 07:27:54 -08001283impl<'a> ToTypename for UniquePtr<'a> {
1284 fn to_typename(&self, types: &Types) -> String {
1285 match self {
1286 UniquePtr::Ident(ident) => ident.to_typename(types),
1287 UniquePtr::CxxVector(element) => {
1288 format!("::std::vector<{}>", element.to_typename(types))
1289 }
1290 }
1291 }
1292}
1293
1294trait ToMangled {
1295 fn to_mangled(&self, types: &Types) -> Symbol;
1296}
1297
David Tolnay3abed472020-12-31 23:34:53 -08001298impl ToMangled for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001299 fn to_mangled(&self, types: &Types) -> Symbol {
David Tolnay1e5fe232021-01-01 18:11:40 -08001300 types.resolve(self).name.to_symbol()
David Tolnay1bdb4712020-11-25 07:27:54 -08001301 }
1302}
1303
1304impl<'a> ToMangled for UniquePtr<'a> {
1305 fn to_mangled(&self, types: &Types) -> Symbol {
1306 match self {
1307 UniquePtr::Ident(ident) => ident.to_mangled(types),
1308 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1309 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001310 }
1311}
1312
David Tolnaya7c2ea12020-10-30 21:32:53 -07001313fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001314 if out.header {
1315 return;
1316 }
1317
1318 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001319 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001320 out.begin_block(Block::ExternC);
David Tolnay3abed472020-12-31 23:34:53 -08001321 for impl_key in out.types.impls.keys() {
1322 out.next_section();
1323 match impl_key {
1324 ImplKey::RustBox(ident) => write_rust_box_extern(out, ident),
1325 ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident),
1326 ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident),
1327 ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident),
1328 ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident),
1329 ImplKey::CxxVector(ident) => write_cxx_vector(out, ident),
David Tolnay7db73692019-10-20 14:51:12 -04001330 }
1331 }
David Tolnay0c033e32020-11-01 15:15:48 -08001332 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001333
David Tolnay0c033e32020-11-01 15:15:48 -08001334 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001335 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3abed472020-12-31 23:34:53 -08001336 for impl_key in out.types.impls.keys() {
1337 match impl_key {
1338 ImplKey::RustBox(ident) => write_rust_box_impl(out, ident),
1339 ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident),
1340 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -04001341 }
1342 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001343 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001344 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001345}
1346
David Tolnay3abed472020-12-31 23:34:53 -08001347fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1348 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001349 let inner = resolve.name.to_fully_qualified();
1350 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001351
David Tolnay7db73692019-10-20 14:51:12 -04001352 writeln!(
1353 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001354 "{} *cxxbridge1$box${}$alloc() noexcept;",
1355 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001356 );
1357 writeln!(
1358 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001359 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1360 instance, inner,
1361 );
1362 writeln!(
1363 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001364 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001365 instance, inner,
1366 );
David Tolnay7db73692019-10-20 14:51:12 -04001367}
1368
David Tolnay3abed472020-12-31 23:34:53 -08001369fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001370 let inner = element.to_typename(out.types);
1371 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001372
David Tolnay12320e12020-12-09 23:09:36 -08001373 out.include.cstddef = true;
1374
Myron Ahneba35cf2020-02-05 19:41:51 +07001375 writeln!(
1376 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001377 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001378 instance, inner,
1379 );
1380 writeln!(
1381 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001382 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001383 instance, inner,
1384 );
1385 writeln!(
1386 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001387 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001388 instance, inner,
1389 );
David Tolnay219c0792020-04-24 20:31:37 -07001390 writeln!(
1391 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001392 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001393 instance, inner,
1394 );
1395 writeln!(
1396 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001397 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001398 inner, instance,
1399 );
David Tolnay503d0192020-04-24 22:18:56 -07001400 writeln!(
1401 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001402 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001403 instance, inner,
1404 );
1405 writeln!(
1406 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001407 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001408 instance, inner,
1409 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001410}
1411
David Tolnay3abed472020-12-31 23:34:53 -08001412fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1413 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001414 let inner = resolve.name.to_fully_qualified();
1415 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001416
1417 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001418 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001419 writeln!(
1420 out,
1421 "{} *Box<{}>::allocation::alloc() noexcept {{",
1422 inner, inner,
1423 );
David Tolnayc4b34222020-12-12 13:06:26 -08001424 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001425 writeln!(out, "}}");
1426
1427 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001428 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001429 writeln!(
1430 out,
David Tolnaye5703162020-12-12 16:26:35 -08001431 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001432 inner, inner,
1433 );
1434 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1435 writeln!(out, "}}");
1436
1437 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001438 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001439 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001440 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001441 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001442}
1443
David Tolnay3abed472020-12-31 23:34:53 -08001444fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001445 let inner = element.to_typename(out.types);
1446 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001447
David Tolnay12320e12020-12-09 23:09:36 -08001448 out.include.cstddef = true;
1449
Myron Ahneba35cf2020-02-05 19:41:51 +07001450 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001451 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001452 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001453 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001454 writeln!(out, "}}");
1455
1456 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001457 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001458 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001459 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001460 writeln!(out, "}}");
1461
1462 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001463 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001464 writeln!(
1465 out,
1466 "::std::size_t Vec<{}>::size() const noexcept {{",
1467 inner,
1468 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001469 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001470 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001471
1472 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001473 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001474 writeln!(
1475 out,
1476 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1477 inner,
1478 );
David Tolnay381d9532020-12-11 13:59:45 -08001479 writeln!(
1480 out,
1481 " return cxxbridge1$rust_vec${}$capacity(this);",
1482 instance,
1483 );
David Tolnaydc62d712020-12-11 13:51:53 -08001484 writeln!(out, "}}");
1485
1486 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001487 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001488 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001489 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001490 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001491
1492 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001493 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001494 writeln!(
1495 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001496 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001497 inner,
1498 );
1499 writeln!(
1500 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001501 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001502 instance,
1503 );
1504 writeln!(out, "}}");
1505
1506 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001507 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001508 writeln!(
1509 out,
1510 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1511 inner,
1512 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001513 writeln!(
1514 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001515 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001516 instance,
1517 );
1518 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001519}
1520
David Tolnay3abed472020-12-31 23:34:53 -08001521fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001522 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001523 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001524}
1525
1526// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001527fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001528 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001529 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001530 let inner = ty.to_typename(out.types);
1531 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001532
David Tolnay63da4d32020-04-25 09:41:12 -07001533 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001534 // Some aliases are to opaque types; some are to trivial types. We can't
1535 // know at code generation time, so we generate both C++ and Rust side
1536 // bindings for a "new" method anyway. But the Rust code can't be called
1537 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001538 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001539 out.types.structs.contains_key(ident)
1540 || out.types.enums.contains_key(ident)
1541 || out.types.aliases.contains_key(ident)
David Tolnayca0f9da2020-10-16 13:16:17 -07001542 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001543 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001544 };
1545
David Tolnay53462762020-11-25 07:08:10 -08001546 let conditional_delete = match ty {
1547 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001548 !out.types.structs.contains_key(ident) && !out.types.enums.contains_key(ident)
David Tolnay53462762020-11-25 07:08:10 -08001549 }
1550 UniquePtr::CxxVector(_) => false,
1551 };
1552
1553 if conditional_delete {
1554 out.builtin.is_complete = true;
1555 let definition = match ty {
David Tolnay1e5fe232021-01-01 18:11:40 -08001556 UniquePtr::Ident(ty) => &out.types.resolve(ty).name.cxx,
David Tolnay53462762020-11-25 07:08:10 -08001557 UniquePtr::CxxVector(_) => unreachable!(),
1558 };
1559 writeln!(
1560 out,
David Tolnay75068632020-12-26 22:15:17 -08001561 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001562 inner, definition,
1563 );
1564 }
David Tolnay7db73692019-10-20 14:51:12 -04001565 writeln!(
1566 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001567 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001568 inner,
1569 );
1570 writeln!(
1571 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001572 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001573 inner,
1574 );
1575 writeln!(
1576 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001577 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001578 instance, inner,
1579 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001580 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001581 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001582 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001583 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001584 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001585 out,
David Tolnay0b881402020-12-01 21:05:08 -08001586 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1587 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001588 );
David Tolnay63da4d32020-04-25 09:41:12 -07001589 writeln!(
1590 out,
David Tolnay0b881402020-12-01 21:05:08 -08001591 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1592 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001593 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001594 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001595 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001596 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001597 }
David Tolnay7db73692019-10-20 14:51:12 -04001598 writeln!(
1599 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001600 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001601 instance, inner, inner,
1602 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001603 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001604 writeln!(out, "}}");
1605 writeln!(
1606 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001607 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001608 inner, instance, inner,
1609 );
1610 writeln!(out, " return ptr.get();");
1611 writeln!(out, "}}");
1612 writeln!(
1613 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001614 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001615 inner, instance, inner,
1616 );
1617 writeln!(out, " return ptr.release();");
1618 writeln!(out, "}}");
1619 writeln!(
1620 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001621 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001622 instance, inner,
1623 );
David Tolnay53462762020-11-25 07:08:10 -08001624 if conditional_delete {
1625 out.builtin.deleter_if = true;
1626 writeln!(
1627 out,
David Tolnay75068632020-12-26 22:15:17 -08001628 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001629 inner,
1630 );
1631 } else {
1632 writeln!(out, " ptr->~unique_ptr();");
1633 }
David Tolnay7db73692019-10-20 14:51:12 -04001634 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001635}
Myron Ahneba35cf2020-02-05 19:41:51 +07001636
David Tolnay3abed472020-12-31 23:34:53 -08001637fn write_shared_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001638 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001639 let inner = resolve.name.to_fully_qualified();
1640 let instance = resolve.name.to_symbol();
David Tolnayb3b24a12020-12-01 15:27:43 -08001641
David Tolnayb3b24a12020-12-01 15:27:43 -08001642 out.include.new = true;
1643 out.include.utility = true;
1644
1645 // Some aliases are to opaque types; some are to trivial types. We can't
1646 // know at code generation time, so we generate both C++ and Rust side
1647 // bindings for a "new" method anyway. But the Rust code can't be called for
1648 // Opaque types because the 'new' method is not implemented.
David Tolnay3abed472020-12-31 23:34:53 -08001649 let can_construct_from_value = out.types.structs.contains_key(ident)
1650 || out.types.enums.contains_key(ident)
1651 || out.types.aliases.contains_key(ident);
David Tolnayb3b24a12020-12-01 15:27:43 -08001652
David Tolnayb3b24a12020-12-01 15:27:43 -08001653 writeln!(
1654 out,
1655 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1656 inner,
1657 );
1658 writeln!(
1659 out,
1660 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1661 inner,
1662 );
1663 writeln!(
1664 out,
1665 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1666 instance, inner,
1667 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001668 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001669 writeln!(out, "}}");
1670 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001671 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001672 writeln!(
1673 out,
David Tolnay0b881402020-12-01 21:05:08 -08001674 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1675 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001676 );
1677 writeln!(
1678 out,
David Tolnay0b881402020-12-01 21:05:08 -08001679 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1680 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001681 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001682 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001683 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001684 writeln!(out, "}}");
1685 }
1686 writeln!(
1687 out,
1688 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1689 instance, inner, inner,
1690 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001691 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001692 writeln!(out, "}}");
1693 writeln!(
1694 out,
1695 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1696 inner, instance, inner,
1697 );
1698 writeln!(out, " return self.get();");
1699 writeln!(out, "}}");
1700 writeln!(
1701 out,
1702 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1703 instance, inner,
1704 );
1705 writeln!(out, " self->~shared_ptr();");
1706 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001707}
1708
David Tolnay3abed472020-12-31 23:34:53 -08001709fn write_weak_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001710 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001711 let inner = resolve.name.to_fully_qualified();
1712 let instance = resolve.name.to_symbol();
David Tolnay215e77f2020-12-28 17:09:48 -08001713
1714 out.include.new = true;
1715 out.include.utility = true;
1716
1717 writeln!(
1718 out,
1719 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1720 inner,
1721 );
1722 writeln!(
1723 out,
1724 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1725 inner,
1726 );
1727 writeln!(
1728 out,
1729 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1730 instance, inner,
1731 );
1732 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1733 writeln!(out, "}}");
1734 writeln!(
1735 out,
1736 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1737 instance, inner, inner,
1738 );
1739 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1740 writeln!(out, "}}");
1741 writeln!(
1742 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001743 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1744 instance, inner, inner,
1745 );
1746 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1747 writeln!(out, "}}");
1748 writeln!(
1749 out,
David Tolnay7a487852020-12-28 18:02:25 -08001750 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1751 instance, inner, inner,
1752 );
1753 writeln!(
1754 out,
1755 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1756 inner,
1757 );
1758 writeln!(out, "}}");
1759 writeln!(
1760 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001761 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1762 instance, inner,
1763 );
1764 writeln!(out, " self->~weak_ptr();");
1765 writeln!(out, "}}");
1766}
1767
David Tolnay3abed472020-12-31 23:34:53 -08001768fn write_cxx_vector(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001769 let inner = element.to_typename(out.types);
1770 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001771
David Tolnay12320e12020-12-09 23:09:36 -08001772 out.include.cstddef = true;
1773
Myron Ahneba35cf2020-02-05 19:41:51 +07001774 writeln!(
1775 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001776 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001777 instance, inner,
1778 );
1779 writeln!(out, " return s.size();");
1780 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001781 writeln!(
1782 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001783 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001784 inner, instance, inner,
1785 );
David Tolnay767e00d2020-12-21 17:12:27 -08001786 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001787 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001788
David Tolnay70820672020-12-07 11:15:14 -08001789 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001790 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001791}