blob: 02f615deeb620e174c9d11ae2fb99b5955adab38 [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 Tolnay8d067de2020-12-26 16:18:23 -0800362 if !methods.is_empty() {
363 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700364 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800365
David Tolnayee6ecfc2020-12-26 21:54:37 -0800366 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800367 out.include.cstddef = true;
368 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800369 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800370 writeln!(out, " struct layout {{");
371 writeln!(out, " static ::std::size_t size() noexcept;");
372 writeln!(out, " static ::std::size_t align() noexcept;");
373 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700374 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700375 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700376}
377
David Tolnay0b9b9f82020-11-01 20:41:00 -0800378fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800379 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800380 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700381 writeln!(out, "#ifndef {}", guard);
382 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700383 for line in enm.doc.to_string().lines() {
384 writeln!(out, "//{}", line);
385 }
David Tolnay17a934c2020-11-02 00:40:04 -0800386 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700387 write_atom(out, enm.repr);
388 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700389 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800390 for line in variant.doc.to_string().lines() {
391 writeln!(out, " //{}", line);
392 }
David Tolnaye6f62142020-12-21 16:00:41 -0800393 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700394 }
395 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700396 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700397}
398
David Tolnay0b9b9f82020-11-01 20:41:00 -0800399fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800400 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800401 out.include.type_traits = true;
402 writeln!(
403 out,
404 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
405 enm.name.cxx,
406 );
David Tolnay17a934c2020-11-02 00:40:04 -0800407 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700408 write_atom(out, enm.repr);
409 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700410 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700411 write!(out, "static_assert(static_cast<");
412 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700413 writeln!(
414 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700415 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800416 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700417 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700418 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700419}
420
David Tolnay5b1d8632020-12-20 22:05:11 -0800421fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800422 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700423 // necessary for soundness. That's because triviality is always declared by
424 // the user in the form of an unsafe impl of cxx::ExternType:
425 //
426 // unsafe impl ExternType for MyType {
427 // type Id = cxx::type_id!("...");
428 // type Kind = cxx::kind::Trivial;
429 // }
430 //
431 // Since the user went on the record with their unsafe impl to unsafely
432 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800433 // them if that were wrong. However, in practice correctly reasoning about
434 // the relocatability of C++ types is challenging, particularly if the type
435 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700436 //
David Tolnay174bd952020-11-02 09:23:12 -0800437 // There may be legitimate reasons to opt out of this assertion for support
438 // of types that the programmer knows are soundly Rust-movable despite not
439 // being recognized as such by the C++ type system due to a move constructor
440 // or destructor. To opt out of the relocatability check, they need to do
441 // one of the following things in any header used by `include!` in their
442 // bridge.
443 //
444 // --- if they define the type:
445 // struct MyType {
446 // ...
447 // + using IsRelocatable = std::true_type;
448 // };
449 //
450 // --- otherwise:
451 // + template <>
452 // + struct rust::IsRelocatable<MyType> : std::true_type {};
453 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700454
David Tolnay5b1d8632020-12-20 22:05:11 -0800455 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800456 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800457 writeln!(out, "static_assert(");
458 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
459 writeln!(
460 out,
461 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
462 id.trim_start_matches("::"),
463 trivial::as_what(&alias.name, reasons),
464 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700465}
466
David Tolnayb960ed22020-11-27 14:34:30 -0800467fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
468 out.set_namespace(&strct.name.namespace);
469 out.begin_block(Block::ExternC);
470
471 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800472 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800473 writeln!(
474 out,
475 "bool {}(const {1} &, const {1} &) noexcept;",
476 link_name, strct.name.cxx,
477 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800478
479 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800480 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800481 writeln!(
482 out,
483 "bool {}(const {1} &, const {1} &) noexcept;",
484 link_name, strct.name.cxx,
485 );
486 }
David Tolnayb960ed22020-11-27 14:34:30 -0800487 }
488
David Tolnay84389352020-11-27 17:12:10 -0800489 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800490 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800491 writeln!(
492 out,
493 "bool {}(const {1} &, const {1} &) noexcept;",
494 link_name, strct.name.cxx,
495 );
496
David Tolnaya05f9402020-11-27 17:44:05 -0800497 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800498 writeln!(
499 out,
500 "bool {}(const {1} &, const {1} &) noexcept;",
501 link_name, strct.name.cxx,
502 );
503
504 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800505 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800506 writeln!(
507 out,
508 "bool {}(const {1} &, const {1} &) noexcept;",
509 link_name, strct.name.cxx,
510 );
511
David Tolnaya05f9402020-11-27 17:44:05 -0800512 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800513 writeln!(
514 out,
515 "bool {}(const {1} &, const {1} &) noexcept;",
516 link_name, strct.name.cxx,
517 );
518 }
519 }
520
David Tolnay7da38202020-11-27 17:36:16 -0800521 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800522 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800523 let link_name = mangle::operator(&strct.name, "hash");
524 writeln!(
525 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800526 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800527 link_name, strct.name.cxx,
528 );
529 }
530
David Tolnayb960ed22020-11-27 14:34:30 -0800531 out.end_block(Block::ExternC);
532}
533
534fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
535 if out.header {
536 return;
537 }
538
539 out.set_namespace(&strct.name.namespace);
540
541 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800542 out.next_section();
543 writeln!(
544 out,
545 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
546 strct.name.cxx,
547 );
David Tolnaya05f9402020-11-27 17:44:05 -0800548 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800549 writeln!(out, " return {}(*this, rhs);", link_name);
550 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800551
David Tolnayb960ed22020-11-27 14:34:30 -0800552 out.next_section();
553 writeln!(
554 out,
555 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
556 strct.name.cxx,
557 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800558 if derive::contains(&strct.derives, Trait::Eq) {
559 writeln!(out, " return !(*this == rhs);");
560 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800561 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800562 writeln!(out, " return {}(*this, rhs);", link_name);
563 }
David Tolnayb960ed22020-11-27 14:34:30 -0800564 writeln!(out, "}}");
565 }
David Tolnay84389352020-11-27 17:12:10 -0800566
567 if derive::contains(&strct.derives, Trait::PartialOrd) {
568 out.next_section();
569 writeln!(
570 out,
571 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
572 strct.name.cxx,
573 );
David Tolnaya05f9402020-11-27 17:44:05 -0800574 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800575 writeln!(out, " return {}(*this, rhs);", link_name);
576 writeln!(out, "}}");
577
578 out.next_section();
579 writeln!(
580 out,
581 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
582 strct.name.cxx,
583 );
David Tolnaya05f9402020-11-27 17:44:05 -0800584 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800585 writeln!(out, " return {}(*this, rhs);", link_name);
586 writeln!(out, "}}");
587
588 out.next_section();
589 writeln!(
590 out,
591 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
592 strct.name.cxx,
593 );
594 if derive::contains(&strct.derives, Trait::Ord) {
595 writeln!(out, " return !(*this <= rhs);");
596 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800597 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800598 writeln!(out, " return {}(*this, rhs);", link_name);
599 }
600 writeln!(out, "}}");
601
602 out.next_section();
603 writeln!(
604 out,
605 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
606 strct.name.cxx,
607 );
608 if derive::contains(&strct.derives, Trait::Ord) {
609 writeln!(out, " return !(*this < rhs);");
610 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800611 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800612 writeln!(out, " return {}(*this, rhs);", link_name);
613 }
614 writeln!(out, "}}");
615 }
David Tolnayb960ed22020-11-27 14:34:30 -0800616}
617
David Tolnay358bc4b2020-12-26 22:37:57 -0800618fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
619 out.set_namespace(&ety.name.namespace);
620 out.begin_block(Block::ExternC);
621
622 let link_name = mangle::operator(&ety.name, "sizeof");
623 writeln!(out, "::std::size_t {}() noexcept;", link_name);
624
625 let link_name = mangle::operator(&ety.name, "alignof");
626 writeln!(out, "::std::size_t {}() noexcept;", link_name);
627
628 out.end_block(Block::ExternC);
629}
630
631fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
632 if out.header {
633 return;
634 }
635
636 out.set_namespace(&ety.name.namespace);
637
638 out.next_section();
639 let link_name = mangle::operator(&ety.name, "sizeof");
640 writeln!(
641 out,
642 "::std::size_t {}::layout::size() noexcept {{",
643 ety.name.cxx,
644 );
645 writeln!(out, " return {}();", link_name);
646 writeln!(out, "}}");
647
648 out.next_section();
649 let link_name = mangle::operator(&ety.name, "alignof");
650 writeln!(
651 out,
652 "::std::size_t {}::layout::align() noexcept {{",
653 ety.name.cxx,
654 );
655 writeln!(out, " return {}();", link_name);
656 writeln!(out, "}}");
657}
658
David Tolnay1eadd262020-12-29 16:42:08 -0800659fn begin_function_definition(out: &mut OutFile) {
660 if let Some(annotation) = &out.opt.cxx_impl_annotations {
661 write!(out, "{} ", annotation);
662 }
663}
664
David Tolnay0b9b9f82020-11-01 20:41:00 -0800665fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800666 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800667 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800668 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800669 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700670 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700671 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700672 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700673 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700674 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700675 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700676 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700677 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700678 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800679 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700680 write!(out, "const ");
681 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700682 write!(
683 out,
684 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800685 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700686 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700687 }
David Tolnay7db73692019-10-20 14:51:12 -0400688 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700689 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400690 write!(out, ", ");
691 }
David Tolnaya46a2372020-03-06 10:03:48 -0800692 if arg.ty == RustString {
693 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700694 } else if let Type::RustVec(_) = arg.ty {
695 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800696 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700697 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400698 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700699 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400700 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700701 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400702 write!(out, ", ");
703 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700704 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400705 write!(out, "*return$");
706 }
707 writeln!(out, ") noexcept {{");
708 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700709 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700710 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800711 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700712 Some(receiver) => write!(
713 out,
714 "({}::*{}$)(",
David Tolnay1e5fe232021-01-01 18:11:40 -0800715 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800716 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700717 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700718 }
David Tolnay7db73692019-10-20 14:51:12 -0400719 for (i, arg) in efn.args.iter().enumerate() {
720 if i > 0 {
721 write!(out, ", ");
722 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700723 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400724 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700725 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700726 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800727 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700728 write!(out, " const");
729 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700730 }
731 write!(out, " = ");
732 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800733 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700734 Some(receiver) => write!(
735 out,
736 "&{}::{}",
David Tolnay1e5fe232021-01-01 18:11:40 -0800737 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800738 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700739 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700740 }
741 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400742 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700743 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700744 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700745 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700746 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700747 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700748 writeln!(out, " [&] {{");
749 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700750 }
David Tolnay7db73692019-10-20 14:51:12 -0400751 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700752 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400753 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700754 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400755 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700756 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400757 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700758 }
David Tolnayfe77f332021-01-02 17:39:51 -0800759 match &efn.ret {
760 Some(Type::Ref(_)) => write!(out, "&"),
761 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, "{{"),
762 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400763 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700764 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800765 None => write!(out, "{}$(", efn.name.rust),
766 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700767 }
David Tolnay7db73692019-10-20 14:51:12 -0400768 for (i, arg) in efn.args.iter().enumerate() {
769 if i > 0 {
770 write!(out, ", ");
771 }
772 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700773 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800774 write!(out, "::from_raw({})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400775 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700776 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800777 write!(out, "({})", arg.name.cxx);
David Tolnaya46a2372020-03-06 10:03:48 -0800778 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700779 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800780 write!(
781 out,
782 "::rust::String(::rust::unsafe_bitcopy, *{})",
David Tolnay84ed6ad2021-01-01 15:30:14 -0800783 arg.name.cxx,
David Tolnaycc3767f2020-03-06 10:41:51 -0800784 );
David Tolnay313b10e2020-04-25 16:30:51 -0700785 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700786 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700787 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800788 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.name.cxx);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700789 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700790 out.include.utility = true;
David Tolnay84ed6ad2021-01-01 15:30:14 -0800791 write!(out, "::std::move(*{})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400792 } else {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800793 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400794 }
795 }
796 write!(out, ")");
797 match &efn.ret {
798 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
799 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnayfe77f332021-01-02 17:39:51 -0800800 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, "}}"),
David Tolnay7db73692019-10-20 14:51:12 -0400801 _ => {}
802 }
803 if indirect_return {
804 write!(out, ")");
805 }
806 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700807 if efn.throws {
808 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800809 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700810 writeln!(out, " throw$.ptr = nullptr;");
811 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700812 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700813 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700814 writeln!(
815 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800816 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700817 );
David Tolnay5d121442020-03-17 22:14:40 -0700818 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700819 writeln!(out, " return throw$;");
820 }
David Tolnay7db73692019-10-20 14:51:12 -0400821 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700822 for arg in &efn.args {
823 if let Type::Fn(f) = &arg.ty {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800824 let var = &arg.name;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700825 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700826 }
827 }
David Tolnayca563ee2020-11-01 20:12:27 -0800828 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700829}
830
David Tolnay84ed6ad2021-01-01 15:30:14 -0800831fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700832 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700833 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700834 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700835
836 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700837 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
838 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400839}
840
David Tolnay0b9b9f82020-11-01 20:41:00 -0800841fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800842 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800843 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700844 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700845 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700846 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800847 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700848}
849
850fn write_rust_function_decl_impl(
851 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700852 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700854 indirect_call: bool,
855) {
David Tolnay04770742020-11-01 13:50:50 -0800856 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700857 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700858 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700859 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700860 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700861 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700862 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700863 write!(out, "{}(", link_name);
864 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700865 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800866 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700867 write!(out, "const ");
868 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700869 write!(
870 out,
871 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800872 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700873 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700874 needs_comma = true;
875 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700876 for arg in &sig.args {
877 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400878 write!(out, ", ");
879 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700880 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700881 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400882 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700883 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700884 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400885 write!(out, ", ");
886 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700887 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400888 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700889 needs_comma = true;
890 }
891 if indirect_call {
892 if needs_comma {
893 write!(out, ", ");
894 }
895 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400896 }
897 writeln!(out, ") noexcept;");
898}
899
David Tolnay0b9b9f82020-11-01 20:41:00 -0800900fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800901 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400902 for line in efn.doc.to_string().lines() {
903 writeln!(out, "//{}", line);
904 }
David Tolnaya73853b2020-04-20 01:19:56 -0700905 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800906 None => efn.name.cxx.to_string(),
David Tolnay1e5fe232021-01-01 18:11:40 -0800907 Some(receiver) => format!(
908 "{}::{}",
909 out.types.resolve(&receiver.ty).name.cxx,
910 efn.name.cxx,
911 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700912 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700913 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700914 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700915 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700916}
917
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700918fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700919 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700920 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700921 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700922 indirect_call: bool,
923) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700924 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700925 write!(out, "{}(", local_name);
926 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400927 if i > 0 {
928 write!(out, ", ");
929 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700930 write_type_space(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800931 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400932 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700933 if indirect_call {
934 if !sig.args.is_empty() {
935 write!(out, ", ");
936 }
937 write!(out, "void *extern$");
938 }
David Tolnay1e548172020-03-16 13:37:09 -0700939 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700940 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800941 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700942 write!(out, " const");
943 }
944 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700945 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700946 write!(out, " noexcept");
947 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700948}
949
950fn write_rust_function_shim_impl(
951 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700952 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700953 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700954 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700955 indirect_call: bool,
956) {
957 if out.header && sig.receiver.is_some() {
958 // We've already defined this inside the struct.
959 return;
960 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800961 if !out.header {
962 begin_function_definition(out);
963 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700964 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400965 if out.header {
966 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700967 return;
David Tolnay7db73692019-10-20 14:51:12 -0400968 }
David Tolnay439cde22020-04-20 00:46:25 -0700969 writeln!(out, " {{");
970 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700971 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700972 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700973 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700974 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700975 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800976 writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -0700977 }
978 }
979 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700980 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700981 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700982 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700983 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700984 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700985 writeln!(out, "> return$;");
986 write!(out, " ");
987 } else if let Some(ret) = &sig.ret {
988 write!(out, "return ");
989 match ret {
990 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700991 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700992 write!(out, "::from_raw(");
993 }
994 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700995 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700996 write!(out, "(");
997 }
998 Type::Ref(_) => write!(out, "*"),
999 _ => {}
1000 }
1001 }
1002 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -07001003 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001004 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001005 }
1006 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001007 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001008 if sig.receiver.is_some() {
1009 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001010 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001011 }
David Tolnay9d840362020-11-10 08:50:40 -08001012 for arg in &sig.args {
1013 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001014 write!(out, ", ");
1015 }
David Tolnay681f5c82021-01-01 22:56:27 -08001016 if out.types.needs_indirect_abi(&arg.ty) {
1017 write!(out, "&");
David Tolnay439cde22020-04-20 00:46:25 -07001018 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001019 write!(out, "{}", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -07001020 match &arg.ty {
1021 Type::RustBox(_) => write!(out, ".into_raw()"),
1022 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001023 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001024 _ => {}
1025 }
David Tolnay9d840362020-11-10 08:50:40 -08001026 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001027 }
1028 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001029 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001030 write!(out, ", ");
1031 }
1032 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001033 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001034 }
1035 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001036 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001037 write!(out, ", ");
1038 }
1039 write!(out, "extern$");
1040 }
1041 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001042 if !indirect_return {
1043 if let Some(ret) = &sig.ret {
David Tolnayfe77f332021-01-02 17:39:51 -08001044 match ret {
1045 Type::RustBox(_) | Type::UniquePtr(_) => write!(out, ")"),
1046 Type::Str(_) | Type::SliceRef(_) => write!(out, ".repr"),
1047 _ => {}
David Tolnay22602b42020-09-21 18:04:05 -04001048 }
David Tolnay439cde22020-04-20 00:46:25 -07001049 }
1050 }
1051 writeln!(out, ";");
1052 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001053 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001054 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001055 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001056 writeln!(out, " }}");
1057 }
1058 if indirect_return {
1059 out.include.utility = true;
1060 writeln!(out, " return ::std::move(return$.value);");
1061 }
1062 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001063}
1064
David Tolnaya7c2ea12020-10-30 21:32:53 -07001065fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001066 match ty {
1067 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001068 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001069 }
1070}
1071
David Tolnay75dca2e2020-03-25 20:17:52 -07001072fn indirect_return(sig: &Signature, types: &Types) -> bool {
1073 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001074 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001075 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001076}
1077
David Tolnaya7c2ea12020-10-30 21:32:53 -07001078fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001079 match ty {
1080 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001081 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001082 write!(out, "*");
1083 }
1084 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001085 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001086 write!(out, "const ");
1087 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001088 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001089 write!(out, " *");
1090 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001091 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001092 }
1093}
1094
David Tolnaya7c2ea12020-10-30 21:32:53 -07001095fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1096 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001097 match ty {
1098 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001099 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001100 _ => write_space_after_type(out, ty),
1101 }
1102}
1103
David Tolnaya7c2ea12020-10-30 21:32:53 -07001104fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001105 match ty {
1106 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001107 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001108 write!(out, "*");
1109 }
David Tolnay4a441222020-01-25 16:24:27 -08001110 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001111 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001112 write!(out, "const ");
1113 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001114 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001115 write!(out, " *");
1116 }
David Tolnayfe77f332021-01-02 17:39:51 -08001117 Some(ty @ Type::Str(_)) | Some(ty @ Type::SliceRef(_)) => {
1118 out.builtin.repr_fat = true;
1119 write!(out, "::rust::repr::Fat<");
1120 write_type(out, ty);
1121 write!(out, "> ");
1122 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001123 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1124 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001125 }
1126}
1127
David Tolnaya7c2ea12020-10-30 21:32:53 -07001128fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001129 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001130 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001131 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001132 write!(out, "*");
1133 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001134 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001135 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001136 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001137 write!(out, "*");
1138 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001139 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -04001140}
1141
David Tolnaya7c2ea12020-10-30 21:32:53 -07001142fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001143 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001144 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001145 Some(atom) => write_atom(out, atom),
David Tolnay1e5fe232021-01-01 18:11:40 -08001146 None => write!(
1147 out,
1148 "{}",
1149 out.types.resolve(ident).name.to_fully_qualified(),
1150 ),
David Tolnay7db73692019-10-20 14:51:12 -04001151 },
1152 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001153 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001154 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001155 write!(out, ">");
1156 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001157 Type::RustVec(ty) => {
1158 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001159 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001160 write!(out, ">");
1161 }
David Tolnay7db73692019-10-20 14:51:12 -04001162 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001163 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001164 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001165 write!(out, ">");
1166 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001167 Type::SharedPtr(ptr) => {
1168 write!(out, "::std::shared_ptr<");
1169 write_type(out, &ptr.inner);
1170 write!(out, ">");
1171 }
David Tolnay215e77f2020-12-28 17:09:48 -08001172 Type::WeakPtr(ptr) => {
1173 write!(out, "::std::weak_ptr<");
1174 write_type(out, &ptr.inner);
1175 write!(out, ">");
1176 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001177 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001178 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001179 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001180 write!(out, ">");
1181 }
David Tolnay7db73692019-10-20 14:51:12 -04001182 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001183 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001184 write!(out, "const ");
1185 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001186 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001187 write!(out, " &");
1188 }
1189 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001190 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001191 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001192 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001193 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001194 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001195 write!(out, "const ");
1196 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001197 write_type(out, &slice.inner);
1198 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001199 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001200 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001201 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001202 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001203 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001204 None => write!(out, "void"),
1205 }
1206 write!(out, "(");
1207 for (i, arg) in f.args.iter().enumerate() {
1208 if i > 0 {
1209 write!(out, ", ");
1210 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001211 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001212 }
1213 write!(out, ")>");
1214 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001215 Type::Array(a) => {
1216 write!(out, "::std::array<");
1217 write_type(out, &a.inner);
1218 write!(out, ", {}>", &a.len);
1219 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001220 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001221 }
1222}
1223
David Tolnayf6a89f22020-05-10 23:39:27 -07001224fn write_atom(out: &mut OutFile, atom: Atom) {
1225 match atom {
1226 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001227 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001228 U8 => write!(out, "::std::uint8_t"),
1229 U16 => write!(out, "::std::uint16_t"),
1230 U32 => write!(out, "::std::uint32_t"),
1231 U64 => write!(out, "::std::uint64_t"),
1232 Usize => write!(out, "::std::size_t"),
1233 I8 => write!(out, "::std::int8_t"),
1234 I16 => write!(out, "::std::int16_t"),
1235 I32 => write!(out, "::std::int32_t"),
1236 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001237 Isize => write!(out, "::rust::isize"),
1238 F32 => write!(out, "float"),
1239 F64 => write!(out, "double"),
1240 CxxString => write!(out, "::std::string"),
1241 RustString => write!(out, "::rust::String"),
1242 }
1243}
1244
David Tolnaya7c2ea12020-10-30 21:32:53 -07001245fn write_type_space(out: &mut OutFile, ty: &Type) {
1246 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001247 write_space_after_type(out, ty);
1248}
1249
1250fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001251 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001252 Type::Ident(_)
1253 | Type::RustBox(_)
1254 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001255 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001256 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001257 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001258 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001259 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001260 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001261 | Type::Fn(_)
1262 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001263 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001264 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001265 }
1266}
1267
David Tolnay1bdb4712020-11-25 07:27:54 -08001268#[derive(Copy, Clone)]
1269enum UniquePtr<'a> {
David Tolnay3abed472020-12-31 23:34:53 -08001270 Ident(&'a Ident),
1271 CxxVector(&'a Ident),
David Tolnay1bdb4712020-11-25 07:27:54 -08001272}
1273
1274trait ToTypename {
1275 fn to_typename(&self, types: &Types) -> String;
1276}
1277
David Tolnay3abed472020-12-31 23:34:53 -08001278impl ToTypename for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001279 fn to_typename(&self, types: &Types) -> String {
David Tolnay1e5fe232021-01-01 18:11:40 -08001280 types.resolve(self).name.to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001281 }
1282}
1283
David Tolnay1bdb4712020-11-25 07:27:54 -08001284impl<'a> ToTypename for UniquePtr<'a> {
1285 fn to_typename(&self, types: &Types) -> String {
1286 match self {
1287 UniquePtr::Ident(ident) => ident.to_typename(types),
1288 UniquePtr::CxxVector(element) => {
1289 format!("::std::vector<{}>", element.to_typename(types))
1290 }
1291 }
1292 }
1293}
1294
1295trait ToMangled {
1296 fn to_mangled(&self, types: &Types) -> Symbol;
1297}
1298
David Tolnay3abed472020-12-31 23:34:53 -08001299impl ToMangled for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001300 fn to_mangled(&self, types: &Types) -> Symbol {
David Tolnay1e5fe232021-01-01 18:11:40 -08001301 types.resolve(self).name.to_symbol()
David Tolnay1bdb4712020-11-25 07:27:54 -08001302 }
1303}
1304
1305impl<'a> ToMangled for UniquePtr<'a> {
1306 fn to_mangled(&self, types: &Types) -> Symbol {
1307 match self {
1308 UniquePtr::Ident(ident) => ident.to_mangled(types),
1309 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1310 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001311 }
1312}
1313
David Tolnaya7c2ea12020-10-30 21:32:53 -07001314fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001315 if out.header {
1316 return;
1317 }
1318
1319 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001320 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001321 out.begin_block(Block::ExternC);
David Tolnay3abed472020-12-31 23:34:53 -08001322 for impl_key in out.types.impls.keys() {
1323 out.next_section();
1324 match impl_key {
1325 ImplKey::RustBox(ident) => write_rust_box_extern(out, ident),
1326 ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident),
1327 ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident),
1328 ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident),
1329 ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident),
1330 ImplKey::CxxVector(ident) => write_cxx_vector(out, ident),
David Tolnay7db73692019-10-20 14:51:12 -04001331 }
1332 }
David Tolnay0c033e32020-11-01 15:15:48 -08001333 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001334
David Tolnay0c033e32020-11-01 15:15:48 -08001335 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001336 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3abed472020-12-31 23:34:53 -08001337 for impl_key in out.types.impls.keys() {
1338 match impl_key {
1339 ImplKey::RustBox(ident) => write_rust_box_impl(out, ident),
1340 ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident),
1341 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -04001342 }
1343 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001344 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001345 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001346}
1347
David Tolnay3abed472020-12-31 23:34:53 -08001348fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1349 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001350 let inner = resolve.name.to_fully_qualified();
1351 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001352
David Tolnay7db73692019-10-20 14:51:12 -04001353 writeln!(
1354 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001355 "{} *cxxbridge1$box${}$alloc() noexcept;",
1356 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001357 );
1358 writeln!(
1359 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001360 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1361 instance, inner,
1362 );
1363 writeln!(
1364 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001365 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001366 instance, inner,
1367 );
David Tolnay7db73692019-10-20 14:51:12 -04001368}
1369
David Tolnay3abed472020-12-31 23:34:53 -08001370fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001371 let inner = element.to_typename(out.types);
1372 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001373
David Tolnay12320e12020-12-09 23:09:36 -08001374 out.include.cstddef = true;
1375
Myron Ahneba35cf2020-02-05 19:41:51 +07001376 writeln!(
1377 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001378 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001379 instance, inner,
1380 );
1381 writeln!(
1382 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001383 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001384 instance, inner,
1385 );
1386 writeln!(
1387 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001388 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001389 instance, inner,
1390 );
David Tolnay219c0792020-04-24 20:31:37 -07001391 writeln!(
1392 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001393 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001394 instance, inner,
1395 );
1396 writeln!(
1397 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001398 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001399 inner, instance,
1400 );
David Tolnay503d0192020-04-24 22:18:56 -07001401 writeln!(
1402 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001403 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001404 instance, inner,
1405 );
1406 writeln!(
1407 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001408 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001409 instance, inner,
1410 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001411}
1412
David Tolnay3abed472020-12-31 23:34:53 -08001413fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1414 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001415 let inner = resolve.name.to_fully_qualified();
1416 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001417
1418 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001419 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001420 writeln!(
1421 out,
1422 "{} *Box<{}>::allocation::alloc() noexcept {{",
1423 inner, inner,
1424 );
David Tolnayc4b34222020-12-12 13:06:26 -08001425 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001426 writeln!(out, "}}");
1427
1428 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001429 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001430 writeln!(
1431 out,
David Tolnaye5703162020-12-12 16:26:35 -08001432 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001433 inner, inner,
1434 );
1435 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1436 writeln!(out, "}}");
1437
1438 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001439 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001440 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001441 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001442 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001443}
1444
David Tolnay3abed472020-12-31 23:34:53 -08001445fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001446 let inner = element.to_typename(out.types);
1447 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001448
David Tolnay12320e12020-12-09 23:09:36 -08001449 out.include.cstddef = true;
1450
Myron Ahneba35cf2020-02-05 19:41:51 +07001451 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001452 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001453 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001454 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001455 writeln!(out, "}}");
1456
1457 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001458 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001459 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001460 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001461 writeln!(out, "}}");
1462
1463 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001464 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001465 writeln!(
1466 out,
1467 "::std::size_t Vec<{}>::size() const noexcept {{",
1468 inner,
1469 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001470 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001471 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001472
1473 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001474 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001475 writeln!(
1476 out,
1477 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1478 inner,
1479 );
David Tolnay381d9532020-12-11 13:59:45 -08001480 writeln!(
1481 out,
1482 " return cxxbridge1$rust_vec${}$capacity(this);",
1483 instance,
1484 );
David Tolnaydc62d712020-12-11 13:51:53 -08001485 writeln!(out, "}}");
1486
1487 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001488 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001489 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001490 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001491 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001492
1493 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001494 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001495 writeln!(
1496 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001497 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001498 inner,
1499 );
1500 writeln!(
1501 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001502 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001503 instance,
1504 );
1505 writeln!(out, "}}");
1506
1507 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001508 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001509 writeln!(
1510 out,
1511 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1512 inner,
1513 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001514 writeln!(
1515 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001516 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001517 instance,
1518 );
1519 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001520}
1521
David Tolnay3abed472020-12-31 23:34:53 -08001522fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001523 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001524 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001525}
1526
1527// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001528fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001529 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001530 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001531 let inner = ty.to_typename(out.types);
1532 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001533
David Tolnay63da4d32020-04-25 09:41:12 -07001534 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001535 // Some aliases are to opaque types; some are to trivial types. We can't
1536 // know at code generation time, so we generate both C++ and Rust side
1537 // bindings for a "new" method anyway. But the Rust code can't be called
1538 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001539 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001540 out.types.structs.contains_key(ident)
1541 || out.types.enums.contains_key(ident)
1542 || out.types.aliases.contains_key(ident)
David Tolnayca0f9da2020-10-16 13:16:17 -07001543 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001544 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001545 };
1546
David Tolnay53462762020-11-25 07:08:10 -08001547 let conditional_delete = match ty {
1548 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001549 !out.types.structs.contains_key(ident) && !out.types.enums.contains_key(ident)
David Tolnay53462762020-11-25 07:08:10 -08001550 }
1551 UniquePtr::CxxVector(_) => false,
1552 };
1553
1554 if conditional_delete {
1555 out.builtin.is_complete = true;
1556 let definition = match ty {
David Tolnay1e5fe232021-01-01 18:11:40 -08001557 UniquePtr::Ident(ty) => &out.types.resolve(ty).name.cxx,
David Tolnay53462762020-11-25 07:08:10 -08001558 UniquePtr::CxxVector(_) => unreachable!(),
1559 };
1560 writeln!(
1561 out,
David Tolnay75068632020-12-26 22:15:17 -08001562 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001563 inner, definition,
1564 );
1565 }
David Tolnay7db73692019-10-20 14:51:12 -04001566 writeln!(
1567 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001568 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001569 inner,
1570 );
1571 writeln!(
1572 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001573 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001574 inner,
1575 );
1576 writeln!(
1577 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001578 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001579 instance, inner,
1580 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001581 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001582 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001583 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001584 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001585 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001586 out,
David Tolnay0b881402020-12-01 21:05:08 -08001587 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1588 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001589 );
David Tolnay63da4d32020-04-25 09:41:12 -07001590 writeln!(
1591 out,
David Tolnay0b881402020-12-01 21:05:08 -08001592 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1593 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001594 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001595 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001596 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001597 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001598 }
David Tolnay7db73692019-10-20 14:51:12 -04001599 writeln!(
1600 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001601 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001602 instance, inner, inner,
1603 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001604 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001605 writeln!(out, "}}");
1606 writeln!(
1607 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001608 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001609 inner, instance, inner,
1610 );
1611 writeln!(out, " return ptr.get();");
1612 writeln!(out, "}}");
1613 writeln!(
1614 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001615 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001616 inner, instance, inner,
1617 );
1618 writeln!(out, " return ptr.release();");
1619 writeln!(out, "}}");
1620 writeln!(
1621 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001622 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001623 instance, inner,
1624 );
David Tolnay53462762020-11-25 07:08:10 -08001625 if conditional_delete {
1626 out.builtin.deleter_if = true;
1627 writeln!(
1628 out,
David Tolnay75068632020-12-26 22:15:17 -08001629 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001630 inner,
1631 );
1632 } else {
1633 writeln!(out, " ptr->~unique_ptr();");
1634 }
David Tolnay7db73692019-10-20 14:51:12 -04001635 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001636}
Myron Ahneba35cf2020-02-05 19:41:51 +07001637
David Tolnay3abed472020-12-31 23:34:53 -08001638fn write_shared_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001639 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001640 let inner = resolve.name.to_fully_qualified();
1641 let instance = resolve.name.to_symbol();
David Tolnayb3b24a12020-12-01 15:27:43 -08001642
David Tolnayb3b24a12020-12-01 15:27:43 -08001643 out.include.new = true;
1644 out.include.utility = true;
1645
1646 // Some aliases are to opaque types; some are to trivial types. We can't
1647 // know at code generation time, so we generate both C++ and Rust side
1648 // bindings for a "new" method anyway. But the Rust code can't be called for
1649 // Opaque types because the 'new' method is not implemented.
David Tolnay3abed472020-12-31 23:34:53 -08001650 let can_construct_from_value = out.types.structs.contains_key(ident)
1651 || out.types.enums.contains_key(ident)
1652 || out.types.aliases.contains_key(ident);
David Tolnayb3b24a12020-12-01 15:27:43 -08001653
David Tolnayb3b24a12020-12-01 15:27:43 -08001654 writeln!(
1655 out,
1656 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1657 inner,
1658 );
1659 writeln!(
1660 out,
1661 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1662 inner,
1663 );
1664 writeln!(
1665 out,
1666 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1667 instance, inner,
1668 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001669 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001670 writeln!(out, "}}");
1671 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001672 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001673 writeln!(
1674 out,
David Tolnay0b881402020-12-01 21:05:08 -08001675 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1676 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001677 );
1678 writeln!(
1679 out,
David Tolnay0b881402020-12-01 21:05:08 -08001680 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1681 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001682 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001683 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001684 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001685 writeln!(out, "}}");
1686 }
1687 writeln!(
1688 out,
1689 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1690 instance, inner, inner,
1691 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001692 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001693 writeln!(out, "}}");
1694 writeln!(
1695 out,
1696 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1697 inner, instance, inner,
1698 );
1699 writeln!(out, " return self.get();");
1700 writeln!(out, "}}");
1701 writeln!(
1702 out,
1703 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1704 instance, inner,
1705 );
1706 writeln!(out, " self->~shared_ptr();");
1707 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001708}
1709
David Tolnay3abed472020-12-31 23:34:53 -08001710fn write_weak_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001711 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001712 let inner = resolve.name.to_fully_qualified();
1713 let instance = resolve.name.to_symbol();
David Tolnay215e77f2020-12-28 17:09:48 -08001714
1715 out.include.new = true;
1716 out.include.utility = true;
1717
1718 writeln!(
1719 out,
1720 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1721 inner,
1722 );
1723 writeln!(
1724 out,
1725 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1726 inner,
1727 );
1728 writeln!(
1729 out,
1730 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1731 instance, inner,
1732 );
1733 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1734 writeln!(out, "}}");
1735 writeln!(
1736 out,
1737 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1738 instance, inner, inner,
1739 );
1740 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1741 writeln!(out, "}}");
1742 writeln!(
1743 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001744 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1745 instance, inner, inner,
1746 );
1747 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1748 writeln!(out, "}}");
1749 writeln!(
1750 out,
David Tolnay7a487852020-12-28 18:02:25 -08001751 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1752 instance, inner, inner,
1753 );
1754 writeln!(
1755 out,
1756 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1757 inner,
1758 );
1759 writeln!(out, "}}");
1760 writeln!(
1761 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001762 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1763 instance, inner,
1764 );
1765 writeln!(out, " self->~weak_ptr();");
1766 writeln!(out, "}}");
1767}
1768
David Tolnay3abed472020-12-31 23:34:53 -08001769fn write_cxx_vector(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001770 let inner = element.to_typename(out.types);
1771 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001772
David Tolnay12320e12020-12-09 23:09:36 -08001773 out.include.cstddef = true;
1774
Myron Ahneba35cf2020-02-05 19:41:51 +07001775 writeln!(
1776 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001777 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001778 instance, inner,
1779 );
1780 writeln!(out, " return s.size();");
1781 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001782 writeln!(
1783 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001784 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001785 inner, instance, inner,
1786 );
David Tolnay767e00d2020-12-21 17:12:27 -08001787 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001788 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001789
David Tolnay70820672020-12-07 11:15:14 -08001790 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001791 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001792}