blob: e20f54f9172545d86a5780f0161d50829293a7d9 [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 Tolnay891061b2020-04-19 22:42:33 -07006use crate::syntax::symbol::Symbol;
David Tolnay5b1d8632020-12-20 22:05:11 -08007use crate::syntax::trivial::{self, TrivialReason};
Adrian Taylorc8713432020-10-21 18:20:55 -07008use crate::syntax::{
David Tolnay75ea17c2020-12-06 21:08:34 -08009 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, RustName, Signature, Struct, Trait,
David Tolnay5b1d8632020-12-20 22:05:11 -080010 Type, TypeAlias, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070011};
David Tolnay7db73692019-10-20 14:51:12 -040012use proc_macro2::Ident;
David Tolnay5439fa12020-11-03 18:45:01 -080013use std::collections::{HashMap, HashSet};
David Tolnay7db73692019-10-20 14:51:12 -040014
David Tolnayac7188c2020-11-01 16:58:16 -080015pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080016 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040017 let out = &mut out_file;
18
David Tolnaydfb82d72020-11-02 00:10:10 -080019 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070020 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040021
David Tolnay7b0e5102020-11-01 23:22:12 -080022 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080023 write_data_structures(out, apis);
24 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080025 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040026
David Tolnay3374d8d2020-10-31 22:18:45 -070027 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070028 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070029
David Tolnayac7188c2020-11-01 16:58:16 -080030 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070031}
32
David Tolnay7b0e5102020-11-01 23:22:12 -080033fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
34 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080035 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080036 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080037 _ => false,
38 };
Adrian Taylorc8713432020-10-21 18:20:55 -070039
David Tolnay7b0e5102020-11-01 23:22:12 -080040 let apis_by_namespace =
41 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
42
David Tolnayd920be52020-11-01 23:34:30 -080043 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080044
David Tolnayd920be52020-11-01 23:34:30 -080045 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080046 let apis = ns_entries.direct_content();
47
48 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080049 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080050 match api {
David Tolnay17a934c2020-11-02 00:40:04 -080051 Api::Struct(strct) => write_struct_decl(out, &strct.name.cxx),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080052 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080053 Api::CxxType(ety) => write_struct_using(out, &ety.name),
54 Api::RustType(ety) => write_struct_decl(out, &ety.name.cxx),
David Tolnay7b0e5102020-11-01 23:22:12 -080055 _ => unreachable!(),
56 }
David Tolnay7db73692019-10-20 14:51:12 -040057 }
David Tolnay7db73692019-10-20 14:51:12 -040058
David Tolnay7b0e5102020-11-01 23:22:12 -080059 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080060 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
61 write(out, nested_ns_entries, indent + 2);
62 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080063 }
Adrian Taylorf9213622020-10-31 22:25:42 -070064 }
65}
66
David Tolnaye1109d92020-11-01 20:51:56 -080067fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayf94bef12020-04-17 14:46:42 -070068 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070069 for api in apis {
David Tolnay102c7ea2020-11-08 18:58:09 -080070 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayf94bef12020-04-17 14:46:42 -070071 if let Some(receiver) = &efn.sig.receiver {
72 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070073 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070074 .or_insert_with(Vec::new)
75 .push(efn);
76 }
77 }
78 }
Joel Galenson968738f2020-04-15 14:19:33 -070079
David Tolnay5439fa12020-11-03 18:45:01 -080080 let mut structs_written = HashSet::new();
81 let mut toposorted_structs = out.types.toposorted_structs.iter();
David Tolnay9622b462020-11-02 21:34:42 -080082 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070083 match api {
David Tolnay5439fa12020-11-03 18:45:01 -080084 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
85 for next in &mut toposorted_structs {
86 if !out.types.cxx.contains(&strct.name.rust) {
87 out.next_section();
David Tolnay102c7ea2020-11-08 18:58:09 -080088 let methods = methods_for_type
89 .get(&strct.name.rust)
90 .map(Vec::as_slice)
91 .unwrap_or_default();
92 write_struct(out, next, methods);
David Tolnay5439fa12020-11-03 18:45:01 -080093 }
94 structs_written.insert(&next.name.rust);
95 if next.name.rust == strct.name.rust {
96 break;
97 }
98 }
99 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700100 Api::Enum(enm) => {
101 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800102 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700103 check_enum(out, enm);
104 } else {
105 write_enum(out, enm);
106 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700107 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700108 Api::RustType(ety) => {
David Tolnay8d067de2020-12-26 16:18:23 -0800109 out.next_section();
110 let methods = methods_for_type
111 .get(&ety.name.rust)
112 .map(Vec::as_slice)
113 .unwrap_or_default();
114 write_opaque_type(out, ety, methods);
David Tolnayc1fe0052020-04-17 15:15:06 -0700115 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700116 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400117 }
118 }
119
David Tolnayfabca772020-10-03 21:25:41 -0700120 out.next_section();
121 for api in apis {
122 if let Api::TypeAlias(ety) = api {
Adrian Taylorf831a5a2020-12-07 16:49:19 -0800123 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
David Tolnay5b1d8632020-12-20 22:05:11 -0800124 check_trivial_extern_type(out, ety, reasons)
David Tolnayfabca772020-10-03 21:25:41 -0700125 }
126 }
127 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800128}
129
David Tolnaye1109d92020-11-01 20:51:56 -0800130fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700131 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400132 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800133 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800134 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800135 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800136 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
137 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
138 _ => {}
139 }
David Tolnay7db73692019-10-20 14:51:12 -0400140 }
David Tolnay7da38202020-11-27 17:36:16 -0800141
142 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400143 }
144
145 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800146 match api {
147 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800148 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800149 Api::RustFunction(efn) => {
150 out.next_section();
151 write_rust_function_shim(out, efn);
152 }
153 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400154 }
155 }
David Tolnay7db73692019-10-20 14:51:12 -0400156}
157
David Tolnay7da38202020-11-27 17:36:16 -0800158fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
159 out.set_namespace(Default::default());
160 out.begin_block(Block::Namespace("std"));
161
162 for api in apis {
163 if let Api::Struct(strct) = api {
164 if derive::contains(&strct.derives, Trait::Hash) {
165 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800166 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800167 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800168 let qualified = strct.name.to_fully_qualified();
169 writeln!(out, "template <> struct hash<{}> {{", qualified);
170 writeln!(
171 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800172 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800173 qualified,
174 );
175 let link_name = mangle::operator(&strct.name, "hash");
176 write!(out, " return ::");
177 for name in &strct.name.namespace {
178 write!(out, "{}::", name);
179 }
180 writeln!(out, "{}(self);", link_name);
181 writeln!(out, " }}");
182 writeln!(out, "}};");
183 }
184 }
185 }
186
187 out.end_block(Block::Namespace("std"));
188}
189
David Tolnaydfb82d72020-11-02 00:10:10 -0800190fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
191 for api in apis {
192 if let Api::Include(include) = api {
193 out.include.insert(include);
194 }
195 }
196
David Tolnaya7c2ea12020-10-30 21:32:53 -0700197 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400198 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700199 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700200 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
201 | Some(I64) => out.include.cstdint = true,
202 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800203 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700204 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800205 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800206 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700207 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800208 Type::RustBox(_) => out.builtin.rust_box = true,
209 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700210 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb3b24a12020-12-01 15:27:43 -0800211 Type::SharedPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800212 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700213 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800214 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800215 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800216 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800217 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400218 }
219 }
David Tolnayec66d112020-10-31 21:00:26 -0700220}
David Tolnayf51447e2020-03-06 14:14:27 -0800221
David Tolnay102c7ea2020-11-08 18:58:09 -0800222fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800223 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800224 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800225
David Tolnay17a934c2020-11-02 00:40:04 -0800226 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800227 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700228 writeln!(out, "#ifndef {}", guard);
229 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400230 for line in strct.doc.to_string().lines() {
231 writeln!(out, "//{}", line);
232 }
David Tolnay17a934c2020-11-02 00:40:04 -0800233 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800234
David Tolnay7db73692019-10-20 14:51:12 -0400235 for field in &strct.fields {
236 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700237 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400238 writeln!(out, "{};", field.ident);
239 }
David Tolnay84389352020-11-27 17:12:10 -0800240
David Tolnay5554ebd2020-12-10 11:34:41 -0800241 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800242
David Tolnay102c7ea2020-11-08 18:58:09 -0800243 for method in methods {
244 write!(out, " ");
245 let sig = &method.sig;
246 let local_name = method.name.cxx.to_string();
247 write_rust_function_shim_decl(out, &local_name, sig, false);
248 writeln!(out, ";");
249 }
David Tolnay84389352020-11-27 17:12:10 -0800250
David Tolnayb960ed22020-11-27 14:34:30 -0800251 if operator_eq {
252 writeln!(
253 out,
254 " bool operator==(const {} &) const noexcept;",
255 strct.name.cxx,
256 );
257 writeln!(
258 out,
259 " bool operator!=(const {} &) const noexcept;",
260 strct.name.cxx,
261 );
262 }
David Tolnay84389352020-11-27 17:12:10 -0800263
264 if operator_ord {
265 writeln!(
266 out,
267 " bool operator<(const {} &) const noexcept;",
268 strct.name.cxx,
269 );
270 writeln!(
271 out,
272 " bool operator<=(const {} &) const noexcept;",
273 strct.name.cxx,
274 );
275 writeln!(
276 out,
277 " bool operator>(const {} &) const noexcept;",
278 strct.name.cxx,
279 );
280 writeln!(
281 out,
282 " bool operator>=(const {} &) const noexcept;",
283 strct.name.cxx,
284 );
285 }
286
David Tolnay5554ebd2020-12-10 11:34:41 -0800287 out.include.type_traits = true;
288 writeln!(out, " using IsRelocatable = ::std::true_type;");
289
David Tolnay7db73692019-10-20 14:51:12 -0400290 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700291 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400292}
293
294fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
295 writeln!(out, "struct {};", ident);
296}
297
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800298fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800299 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800300 write_atom(out, enm.repr);
301 writeln!(out, ";");
302}
303
David Tolnay8faec772020-11-02 00:18:19 -0800304fn write_struct_using(out: &mut OutFile, ident: &Pair) {
305 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800306}
307
David Tolnay8d067de2020-12-26 16:18:23 -0800308fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800309 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800310 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700311 writeln!(out, "#ifndef {}", guard);
312 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700313 for line in ety.doc.to_string().lines() {
314 writeln!(out, "//{}", line);
315 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800316
David Tolnay365fc7c2020-11-25 16:08:13 -0800317 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800318 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800319 out,
320 "struct {} final : public ::rust::Opaque {{",
321 ety.name.cxx,
322 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800323
Joel Galenson968738f2020-04-15 14:19:33 -0700324 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800325 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700326 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800327 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700328 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800329 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800330 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800331
David Tolnay8d067de2020-12-26 16:18:23 -0800332 if !methods.is_empty() {
333 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700334 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800335
336 out.include.cstddef = true;
337 writeln!(out, "private:");
338 writeln!(out, " struct layout {{");
339 writeln!(out, " static ::std::size_t size() noexcept;");
340 writeln!(out, " static ::std::size_t align() noexcept;");
341 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700342 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700343 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700344}
345
David Tolnay0b9b9f82020-11-01 20:41:00 -0800346fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800347 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800348 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700349 writeln!(out, "#ifndef {}", guard);
350 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700351 for line in enm.doc.to_string().lines() {
352 writeln!(out, "//{}", line);
353 }
David Tolnay17a934c2020-11-02 00:40:04 -0800354 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700355 write_atom(out, enm.repr);
356 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700357 for variant in &enm.variants {
David Tolnaye6f62142020-12-21 16:00:41 -0800358 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700359 }
360 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700361 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700362}
363
David Tolnay0b9b9f82020-11-01 20:41:00 -0800364fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800365 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800366 out.include.type_traits = true;
367 writeln!(
368 out,
369 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
370 enm.name.cxx,
371 );
David Tolnay17a934c2020-11-02 00:40:04 -0800372 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700373 write_atom(out, enm.repr);
374 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700375 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700376 write!(out, "static_assert(static_cast<");
377 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700378 writeln!(
379 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700380 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800381 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700382 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700383 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700384}
385
David Tolnay5b1d8632020-12-20 22:05:11 -0800386fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800387 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700388 // necessary for soundness. That's because triviality is always declared by
389 // the user in the form of an unsafe impl of cxx::ExternType:
390 //
391 // unsafe impl ExternType for MyType {
392 // type Id = cxx::type_id!("...");
393 // type Kind = cxx::kind::Trivial;
394 // }
395 //
396 // Since the user went on the record with their unsafe impl to unsafely
397 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800398 // them if that were wrong. However, in practice correctly reasoning about
399 // the relocatability of C++ types is challenging, particularly if the type
400 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700401 //
David Tolnay174bd952020-11-02 09:23:12 -0800402 // There may be legitimate reasons to opt out of this assertion for support
403 // of types that the programmer knows are soundly Rust-movable despite not
404 // being recognized as such by the C++ type system due to a move constructor
405 // or destructor. To opt out of the relocatability check, they need to do
406 // one of the following things in any header used by `include!` in their
407 // bridge.
408 //
409 // --- if they define the type:
410 // struct MyType {
411 // ...
412 // + using IsRelocatable = std::true_type;
413 // };
414 //
415 // --- otherwise:
416 // + template <>
417 // + struct rust::IsRelocatable<MyType> : std::true_type {};
418 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700419
David Tolnay5b1d8632020-12-20 22:05:11 -0800420 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800421 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800422 writeln!(out, "static_assert(");
423 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
424 writeln!(
425 out,
426 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
427 id.trim_start_matches("::"),
428 trivial::as_what(&alias.name, reasons),
429 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700430}
431
David Tolnayb960ed22020-11-27 14:34:30 -0800432fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
433 out.set_namespace(&strct.name.namespace);
434 out.begin_block(Block::ExternC);
435
436 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800437 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800438 writeln!(
439 out,
440 "bool {}(const {1} &, const {1} &) noexcept;",
441 link_name, strct.name.cxx,
442 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800443
444 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800445 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800446 writeln!(
447 out,
448 "bool {}(const {1} &, const {1} &) noexcept;",
449 link_name, strct.name.cxx,
450 );
451 }
David Tolnayb960ed22020-11-27 14:34:30 -0800452 }
453
David Tolnay84389352020-11-27 17:12:10 -0800454 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800455 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800456 writeln!(
457 out,
458 "bool {}(const {1} &, const {1} &) noexcept;",
459 link_name, strct.name.cxx,
460 );
461
David Tolnaya05f9402020-11-27 17:44:05 -0800462 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800463 writeln!(
464 out,
465 "bool {}(const {1} &, const {1} &) noexcept;",
466 link_name, strct.name.cxx,
467 );
468
469 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800470 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800471 writeln!(
472 out,
473 "bool {}(const {1} &, const {1} &) noexcept;",
474 link_name, strct.name.cxx,
475 );
476
David Tolnaya05f9402020-11-27 17:44:05 -0800477 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800478 writeln!(
479 out,
480 "bool {}(const {1} &, const {1} &) noexcept;",
481 link_name, strct.name.cxx,
482 );
483 }
484 }
485
David Tolnay7da38202020-11-27 17:36:16 -0800486 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800487 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800488 let link_name = mangle::operator(&strct.name, "hash");
489 writeln!(
490 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800491 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800492 link_name, strct.name.cxx,
493 );
494 }
495
David Tolnayb960ed22020-11-27 14:34:30 -0800496 out.end_block(Block::ExternC);
497}
498
499fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
500 if out.header {
501 return;
502 }
503
504 out.set_namespace(&strct.name.namespace);
505
506 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800507 out.next_section();
508 writeln!(
509 out,
510 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
511 strct.name.cxx,
512 );
David Tolnaya05f9402020-11-27 17:44:05 -0800513 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800514 writeln!(out, " return {}(*this, rhs);", link_name);
515 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800516
David Tolnayb960ed22020-11-27 14:34:30 -0800517 out.next_section();
518 writeln!(
519 out,
520 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
521 strct.name.cxx,
522 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800523 if derive::contains(&strct.derives, Trait::Eq) {
524 writeln!(out, " return !(*this == rhs);");
525 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800526 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800527 writeln!(out, " return {}(*this, rhs);", link_name);
528 }
David Tolnayb960ed22020-11-27 14:34:30 -0800529 writeln!(out, "}}");
530 }
David Tolnay84389352020-11-27 17:12:10 -0800531
532 if derive::contains(&strct.derives, Trait::PartialOrd) {
533 out.next_section();
534 writeln!(
535 out,
536 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
537 strct.name.cxx,
538 );
David Tolnaya05f9402020-11-27 17:44:05 -0800539 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800540 writeln!(out, " return {}(*this, rhs);", link_name);
541 writeln!(out, "}}");
542
543 out.next_section();
544 writeln!(
545 out,
546 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
547 strct.name.cxx,
548 );
David Tolnaya05f9402020-11-27 17:44:05 -0800549 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800550 writeln!(out, " return {}(*this, rhs);", link_name);
551 writeln!(out, "}}");
552
553 out.next_section();
554 writeln!(
555 out,
556 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
557 strct.name.cxx,
558 );
559 if derive::contains(&strct.derives, Trait::Ord) {
560 writeln!(out, " return !(*this <= rhs);");
561 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800562 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800563 writeln!(out, " return {}(*this, rhs);", link_name);
564 }
565 writeln!(out, "}}");
566
567 out.next_section();
568 writeln!(
569 out,
570 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
571 strct.name.cxx,
572 );
573 if derive::contains(&strct.derives, Trait::Ord) {
574 writeln!(out, " return !(*this < rhs);");
575 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800576 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800577 writeln!(out, " return {}(*this, rhs);", link_name);
578 }
579 writeln!(out, "}}");
580 }
David Tolnayb960ed22020-11-27 14:34:30 -0800581}
582
David Tolnay358bc4b2020-12-26 22:37:57 -0800583fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
584 out.set_namespace(&ety.name.namespace);
585 out.begin_block(Block::ExternC);
586
587 let link_name = mangle::operator(&ety.name, "sizeof");
588 writeln!(out, "::std::size_t {}() noexcept;", link_name);
589
590 let link_name = mangle::operator(&ety.name, "alignof");
591 writeln!(out, "::std::size_t {}() noexcept;", link_name);
592
593 out.end_block(Block::ExternC);
594}
595
596fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
597 if out.header {
598 return;
599 }
600
601 out.set_namespace(&ety.name.namespace);
602
603 out.next_section();
604 let link_name = mangle::operator(&ety.name, "sizeof");
605 writeln!(
606 out,
607 "::std::size_t {}::layout::size() noexcept {{",
608 ety.name.cxx,
609 );
610 writeln!(out, " return {}();", link_name);
611 writeln!(out, "}}");
612
613 out.next_section();
614 let link_name = mangle::operator(&ety.name, "alignof");
615 writeln!(
616 out,
617 "::std::size_t {}::layout::align() noexcept {{",
618 ety.name.cxx,
619 );
620 writeln!(out, " return {}();", link_name);
621 writeln!(out, "}}");
622}
623
David Tolnay0b9b9f82020-11-01 20:41:00 -0800624fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800625 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800626 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800627 out.begin_block(Block::ExternC);
David Tolnaye1476af2020-11-01 13:47:25 -0800628 if let Some(annotation) = &out.opt.cxx_impl_annotations {
David Tolnaycc1ae762020-10-31 15:53:50 -0700629 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700630 }
David Tolnayebef4a22020-03-17 15:33:47 -0700631 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700632 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700633 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700634 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700635 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700636 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700637 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700638 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700639 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800640 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700641 write!(out, "const ");
642 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700643 write!(
644 out,
645 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800646 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700647 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700648 }
David Tolnay7db73692019-10-20 14:51:12 -0400649 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700650 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400651 write!(out, ", ");
652 }
David Tolnaya46a2372020-03-06 10:03:48 -0800653 if arg.ty == RustString {
654 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700655 } else if let Type::RustVec(_) = arg.ty {
656 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800657 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700658 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400659 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700660 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400661 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700662 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400663 write!(out, ", ");
664 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700665 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400666 write!(out, "*return$");
667 }
668 writeln!(out, ") noexcept {{");
669 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700670 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700671 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800672 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700673 Some(receiver) => write!(
674 out,
675 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700676 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800677 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700678 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700679 }
David Tolnay7db73692019-10-20 14:51:12 -0400680 for (i, arg) in efn.args.iter().enumerate() {
681 if i > 0 {
682 write!(out, ", ");
683 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700684 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400685 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700686 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700687 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800688 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700689 write!(out, " const");
690 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700691 }
692 write!(out, " = ");
693 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800694 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700695 Some(receiver) => write!(
696 out,
697 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700698 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800699 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700700 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700701 }
702 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400703 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700704 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700705 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700706 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700707 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700708 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700709 writeln!(out, " [&] {{");
710 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700711 }
David Tolnay7db73692019-10-20 14:51:12 -0400712 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700713 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400714 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700715 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400716 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700717 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400718 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700719 }
720 match &efn.ret {
721 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700722 Some(Type::Str(_)) if !indirect_return => {
723 out.builtin.rust_str_repr = true;
724 write!(out, "::rust::impl<::rust::Str>::repr(");
725 }
David Tolnay73b72642020-11-25 17:44:05 -0800726 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700727 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800728 write!(out, "::rust::impl<");
729 write_type(out, ty);
730 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700731 }
David Tolnay99642622020-03-25 13:07:35 -0700732 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400733 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700734 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800735 None => write!(out, "{}$(", efn.name.rust),
736 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700737 }
David Tolnay7db73692019-10-20 14:51:12 -0400738 for (i, arg) in efn.args.iter().enumerate() {
739 if i > 0 {
740 write!(out, ", ");
741 }
742 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700743 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400744 write!(out, "::from_raw({})", arg.ident);
745 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700746 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400747 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700748 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700749 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700750 write!(
751 out,
752 "::rust::impl<::rust::Str>::new_unchecked({})",
753 arg.ident,
754 );
David Tolnaya46a2372020-03-06 10:03:48 -0800755 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700756 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800757 write!(
758 out,
759 "::rust::String(::rust::unsafe_bitcopy, *{})",
760 arg.ident,
761 );
David Tolnay313b10e2020-04-25 16:30:51 -0700762 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700763 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700764 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700765 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800766 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800767 write_type(out, &arg.ty);
768 write!(out, "(static_cast<");
769 if slice.mutability.is_none() {
770 write!(out, "const ");
771 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800772 write_type_space(out, &slice.inner);
773 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700774 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700775 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800776 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400777 } else {
778 write!(out, "{}", arg.ident);
779 }
780 }
781 write!(out, ")");
782 match &efn.ret {
783 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
784 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800785 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400786 _ => {}
787 }
788 if indirect_return {
789 write!(out, ")");
790 }
791 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700792 if efn.throws {
793 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800794 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700795 writeln!(out, " throw$.ptr = nullptr;");
796 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700797 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700798 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700799 writeln!(
800 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800801 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700802 );
David Tolnay5d121442020-03-17 22:14:40 -0700803 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700804 writeln!(out, " return throw$;");
805 }
David Tolnay7db73692019-10-20 14:51:12 -0400806 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700807 for arg in &efn.args {
808 if let Type::Fn(f) = &arg.ty {
809 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700810 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700811 }
812 }
David Tolnayca563ee2020-11-01 20:12:27 -0800813 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700814}
815
816fn write_function_pointer_trampoline(
817 out: &mut OutFile,
818 efn: &ExternFn,
819 var: &Ident,
820 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700821) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700822 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700823 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700824 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700825
826 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700827 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
828 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400829}
830
David Tolnay0b9b9f82020-11-01 20:41:00 -0800831fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800832 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800833 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700834 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700835 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700836 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800837 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700838}
839
840fn write_rust_function_decl_impl(
841 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700842 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700843 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700844 indirect_call: bool,
845) {
David Tolnay04770742020-11-01 13:50:50 -0800846 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700847 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700848 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700849 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700850 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700851 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700852 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 write!(out, "{}(", link_name);
854 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700855 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800856 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700857 write!(out, "const ");
858 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700859 write!(
860 out,
861 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800862 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700863 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700864 needs_comma = true;
865 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700866 for arg in &sig.args {
867 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400868 write!(out, ", ");
869 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700870 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700871 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400872 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700873 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700874 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400875 write!(out, ", ");
876 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700877 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400878 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700879 needs_comma = true;
880 }
881 if indirect_call {
882 if needs_comma {
883 write!(out, ", ");
884 }
885 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400886 }
887 writeln!(out, ") noexcept;");
888}
889
David Tolnay0b9b9f82020-11-01 20:41:00 -0800890fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800891 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400892 for line in efn.doc.to_string().lines() {
893 writeln!(out, "//{}", line);
894 }
David Tolnaya73853b2020-04-20 01:19:56 -0700895 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800896 None => efn.name.cxx.to_string(),
897 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700898 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700899 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700900 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700901 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700902}
903
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700904fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700905 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700906 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700907 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700908 indirect_call: bool,
909) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700910 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700911 write!(out, "{}(", local_name);
912 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400913 if i > 0 {
914 write!(out, ", ");
915 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700916 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400917 write!(out, "{}", arg.ident);
918 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700919 if indirect_call {
920 if !sig.args.is_empty() {
921 write!(out, ", ");
922 }
923 write!(out, "void *extern$");
924 }
David Tolnay1e548172020-03-16 13:37:09 -0700925 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700926 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800927 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700928 write!(out, " const");
929 }
930 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700931 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700932 write!(out, " noexcept");
933 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700934}
935
936fn write_rust_function_shim_impl(
937 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700938 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700939 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700940 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700941 indirect_call: bool,
942) {
943 if out.header && sig.receiver.is_some() {
944 // We've already defined this inside the struct.
945 return;
946 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700947 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400948 if out.header {
949 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700950 return;
David Tolnay7db73692019-10-20 14:51:12 -0400951 }
David Tolnay439cde22020-04-20 00:46:25 -0700952 writeln!(out, " {{");
953 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700954 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700955 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700956 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700957 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700958 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700959 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
960 }
961 }
962 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700963 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700964 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700965 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700966 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700967 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700968 writeln!(out, "> return$;");
969 write!(out, " ");
970 } else if let Some(ret) = &sig.ret {
971 write!(out, "return ");
972 match ret {
973 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700974 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700975 write!(out, "::from_raw(");
976 }
977 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700978 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700979 write!(out, "(");
980 }
981 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700982 Type::Str(_) => {
983 out.builtin.rust_str_new_unchecked = true;
984 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
985 }
David Tolnay73b72642020-11-25 17:44:05 -0800986 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700987 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800988 write!(out, "::rust::impl<");
989 write_type(out, ret);
990 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -0700991 }
David Tolnay439cde22020-04-20 00:46:25 -0700992 _ => {}
993 }
994 }
995 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700996 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700997 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700998 }
999 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001000 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001001 if sig.receiver.is_some() {
1002 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001003 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001004 }
David Tolnay9d840362020-11-10 08:50:40 -08001005 for arg in &sig.args {
1006 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001007 write!(out, ", ");
1008 }
1009 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -07001010 Type::Str(_) => {
1011 out.builtin.rust_str_repr = true;
1012 write!(out, "::rust::impl<::rust::Str>::repr(");
1013 }
David Tolnay73b72642020-11-25 17:44:05 -08001014 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001015 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001016 write!(out, "::rust::impl<");
1017 write_type(out, &arg.ty);
1018 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001019 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001020 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -07001021 _ => {}
1022 }
1023 write!(out, "{}", arg.ident);
1024 match &arg.ty {
1025 Type::RustBox(_) => write!(out, ".into_raw()"),
1026 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -08001027 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001028 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001029 _ => {}
1030 }
David Tolnay9d840362020-11-10 08:50:40 -08001031 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001032 }
1033 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001034 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001035 write!(out, ", ");
1036 }
1037 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001038 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001039 }
1040 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001041 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001042 write!(out, ", ");
1043 }
1044 write!(out, "extern$");
1045 }
1046 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001047 if !indirect_return {
1048 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -08001049 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001050 write!(out, ")");
1051 }
David Tolnay439cde22020-04-20 00:46:25 -07001052 }
1053 }
1054 writeln!(out, ";");
1055 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001056 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001057 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001058 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001059 writeln!(out, " }}");
1060 }
1061 if indirect_return {
1062 out.include.utility = true;
1063 writeln!(out, " return ::std::move(return$.value);");
1064 }
1065 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001066}
1067
David Tolnaya7c2ea12020-10-30 21:32:53 -07001068fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001069 match ty {
1070 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001071 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001072 }
1073}
1074
David Tolnay75dca2e2020-03-25 20:17:52 -07001075fn indirect_return(sig: &Signature, types: &Types) -> bool {
1076 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001077 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001078 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001079}
1080
David Tolnaya7c2ea12020-10-30 21:32:53 -07001081fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001082 match ty {
1083 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001084 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001085 write!(out, "*");
1086 }
1087 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001088 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001089 write!(out, "const ");
1090 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001091 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001092 write!(out, " *");
1093 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001094 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001095 }
1096}
1097
David Tolnaya7c2ea12020-10-30 21:32:53 -07001098fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1099 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001100 match ty {
1101 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001102 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001103 _ => write_space_after_type(out, ty),
1104 }
1105}
1106
David Tolnaya7c2ea12020-10-30 21:32:53 -07001107fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001108 match ty {
1109 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001110 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001111 write!(out, "*");
1112 }
David Tolnay4a441222020-01-25 16:24:27 -08001113 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001114 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001115 write!(out, "const ");
1116 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001117 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001118 write!(out, " *");
1119 }
David Tolnay73b72642020-11-25 17:44:05 -08001120 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001121 out.builtin.ptr_len = true;
1122 write!(out, "::rust::repr::PtrLen ");
1123 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001124 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1125 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001126 }
1127}
1128
David Tolnaya7c2ea12020-10-30 21:32:53 -07001129fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001130 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001131 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001132 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001133 write!(out, "*");
1134 }
David Tolnay73b72642020-11-25 17:44:05 -08001135 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001136 out.builtin.ptr_len = true;
1137 write!(out, "::rust::repr::PtrLen ");
1138 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001139 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001140 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001141 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001142 write!(out, "*");
1143 }
1144 write!(out, "{}", arg.ident);
1145}
1146
David Tolnaya7c2ea12020-10-30 21:32:53 -07001147fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001148 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001149 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001150 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001151 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001152 },
1153 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001154 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001155 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001156 write!(out, ">");
1157 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001158 Type::RustVec(ty) => {
1159 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001160 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001161 write!(out, ">");
1162 }
David Tolnay7db73692019-10-20 14:51:12 -04001163 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001164 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001165 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001166 write!(out, ">");
1167 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001168 Type::SharedPtr(ptr) => {
1169 write!(out, "::std::shared_ptr<");
1170 write_type(out, &ptr.inner);
1171 write!(out, ">");
1172 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001173 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001174 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001175 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001176 write!(out, ">");
1177 }
David Tolnay7db73692019-10-20 14:51:12 -04001178 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001179 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001180 write!(out, "const ");
1181 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001182 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001183 write!(out, " &");
1184 }
1185 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001186 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001187 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001188 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001189 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001190 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001191 write!(out, "const ");
1192 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001193 write_type(out, &slice.inner);
1194 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001195 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001196 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001197 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001198 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001199 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001200 None => write!(out, "void"),
1201 }
1202 write!(out, "(");
1203 for (i, arg) in f.args.iter().enumerate() {
1204 if i > 0 {
1205 write!(out, ", ");
1206 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001207 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001208 }
1209 write!(out, ")>");
1210 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001211 Type::Array(a) => {
1212 write!(out, "::std::array<");
1213 write_type(out, &a.inner);
1214 write!(out, ", {}>", &a.len);
1215 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001216 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001217 }
1218}
1219
David Tolnayf6a89f22020-05-10 23:39:27 -07001220fn write_atom(out: &mut OutFile, atom: Atom) {
1221 match atom {
1222 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001223 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001224 U8 => write!(out, "::std::uint8_t"),
1225 U16 => write!(out, "::std::uint16_t"),
1226 U32 => write!(out, "::std::uint32_t"),
1227 U64 => write!(out, "::std::uint64_t"),
1228 Usize => write!(out, "::std::size_t"),
1229 I8 => write!(out, "::std::int8_t"),
1230 I16 => write!(out, "::std::int16_t"),
1231 I32 => write!(out, "::std::int32_t"),
1232 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001233 Isize => write!(out, "::rust::isize"),
1234 F32 => write!(out, "float"),
1235 F64 => write!(out, "double"),
1236 CxxString => write!(out, "::std::string"),
1237 RustString => write!(out, "::rust::String"),
1238 }
1239}
1240
David Tolnaya7c2ea12020-10-30 21:32:53 -07001241fn write_type_space(out: &mut OutFile, ty: &Type) {
1242 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001243 write_space_after_type(out, ty);
1244}
1245
1246fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001247 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001248 Type::Ident(_)
1249 | Type::RustBox(_)
1250 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001251 | Type::SharedPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001252 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001253 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001254 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001255 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001256 | Type::Fn(_)
1257 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001258 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001259 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001260 }
1261}
1262
David Tolnay1bdb4712020-11-25 07:27:54 -08001263#[derive(Copy, Clone)]
1264enum UniquePtr<'a> {
David Tolnay75ea17c2020-12-06 21:08:34 -08001265 Ident(&'a RustName),
1266 CxxVector(&'a RustName),
David Tolnay1bdb4712020-11-25 07:27:54 -08001267}
1268
1269trait ToTypename {
1270 fn to_typename(&self, types: &Types) -> String;
1271}
1272
David Tolnay75ea17c2020-12-06 21:08:34 -08001273impl ToTypename for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001274 fn to_typename(&self, types: &Types) -> String {
1275 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001276 }
1277}
1278
David Tolnay1bdb4712020-11-25 07:27:54 -08001279impl<'a> ToTypename for UniquePtr<'a> {
1280 fn to_typename(&self, types: &Types) -> String {
1281 match self {
1282 UniquePtr::Ident(ident) => ident.to_typename(types),
1283 UniquePtr::CxxVector(element) => {
1284 format!("::std::vector<{}>", element.to_typename(types))
1285 }
1286 }
1287 }
1288}
1289
1290trait ToMangled {
1291 fn to_mangled(&self, types: &Types) -> Symbol;
1292}
1293
David Tolnay75ea17c2020-12-06 21:08:34 -08001294impl ToMangled for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001295 fn to_mangled(&self, types: &Types) -> Symbol {
1296 self.to_symbol(types)
1297 }
1298}
1299
1300impl<'a> ToMangled for UniquePtr<'a> {
1301 fn to_mangled(&self, types: &Types) -> Symbol {
1302 match self {
1303 UniquePtr::Ident(ident) => ident.to_mangled(types),
1304 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1305 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001306 }
1307}
1308
David Tolnaya7c2ea12020-10-30 21:32:53 -07001309fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001310 if out.header {
1311 return;
1312 }
1313
1314 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001315 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001316 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001317 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001318 if let Type::RustBox(ptr) = ty {
1319 if let Type::Ident(inner) = &ptr.inner {
1320 if Atom::from(&inner.rust).is_none()
1321 && (!out.types.aliases.contains_key(&inner.rust)
1322 || out.types.explicit_impls.contains(ty))
1323 {
1324 out.next_section();
1325 write_rust_box_extern(out, &out.types.resolve(&inner));
1326 }
David Tolnay7db73692019-10-20 14:51:12 -04001327 }
David Tolnayf33bc242020-12-04 18:27:02 -08001328 } else if let Type::RustVec(vec) = ty {
1329 if let Type::Ident(inner) = &vec.inner {
1330 if Atom::from(&inner.rust).is_none()
1331 && (!out.types.aliases.contains_key(&inner.rust)
1332 || out.types.explicit_impls.contains(ty))
1333 {
David Tolnay6787be62020-04-25 11:01:02 -07001334 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001335 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001336 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001337 }
David Tolnay7db73692019-10-20 14:51:12 -04001338 } else if let Type::UniquePtr(ptr) = ty {
1339 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001340 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001341 && (!out.types.aliases.contains_key(&inner.rust)
1342 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001343 {
David Tolnay7db73692019-10-20 14:51:12 -04001344 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001345 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001346 }
1347 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001348 } else if let Type::SharedPtr(ptr) = ty {
1349 if let Type::Ident(inner) = &ptr.inner {
1350 if Atom::from(&inner.rust).is_none()
David Tolnayb7453812020-12-01 21:46:55 -08001351 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnayb3b24a12020-12-01 15:27:43 -08001352 || out.types.explicit_impls.contains(ty))
1353 {
1354 out.next_section();
1355 write_shared_ptr(out, inner);
1356 }
1357 }
David Tolnayf33bc242020-12-04 18:27:02 -08001358 } else if let Type::CxxVector(vector) = ty {
1359 if let Type::Ident(inner) = &vector.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001360 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001361 && (!out.types.aliases.contains_key(&inner.rust)
1362 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001363 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001364 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001365 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001366 }
1367 }
1368 }
1369 }
David Tolnay0c033e32020-11-01 15:15:48 -08001370 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001371
David Tolnay0c033e32020-11-01 15:15:48 -08001372 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001373 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001374 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001375 if let Type::RustBox(ptr) = ty {
1376 if let Type::Ident(inner) = &ptr.inner {
1377 if Atom::from(&inner.rust).is_none()
1378 && (!out.types.aliases.contains_key(&inner.rust)
1379 || out.types.explicit_impls.contains(ty))
1380 {
1381 write_rust_box_impl(out, &out.types.resolve(&inner));
1382 }
David Tolnay7db73692019-10-20 14:51:12 -04001383 }
David Tolnayf33bc242020-12-04 18:27:02 -08001384 } else if let Type::RustVec(vec) = ty {
1385 if let Type::Ident(inner) = &vec.inner {
1386 if Atom::from(&inner.rust).is_none()
1387 && (!out.types.aliases.contains_key(&inner.rust)
1388 || out.types.explicit_impls.contains(ty))
1389 {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001390 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001391 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001392 }
David Tolnay7db73692019-10-20 14:51:12 -04001393 }
1394 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001395 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001396 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001397}
1398
David Tolnay8faec772020-11-02 00:18:19 -08001399fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001400 let inner = ident.to_fully_qualified();
1401 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001402
David Tolnay0f0162f2020-11-16 23:43:37 -08001403 writeln!(out, "#ifndef CXXBRIDGE1_RUST_BOX_{}", instance);
1404 writeln!(out, "#define CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001405 writeln!(
1406 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001407 "{} *cxxbridge1$box${}$alloc() noexcept;",
1408 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001409 );
1410 writeln!(
1411 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001412 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1413 instance, inner,
1414 );
1415 writeln!(
1416 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001417 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001418 instance, inner,
1419 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001420 writeln!(out, "#endif // CXXBRIDGE1_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001421}
1422
David Tolnay75ea17c2020-12-06 21:08:34 -08001423fn write_rust_vec_extern(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001424 let inner = element.to_typename(out.types);
1425 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001426
David Tolnay12320e12020-12-09 23:09:36 -08001427 out.include.cstddef = true;
1428
David Tolnay0f0162f2020-11-16 23:43:37 -08001429 writeln!(out, "#ifndef CXXBRIDGE1_RUST_VEC_{}", instance);
1430 writeln!(out, "#define CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001431 writeln!(
1432 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001433 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001434 instance, inner,
1435 );
1436 writeln!(
1437 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001438 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001439 instance, inner,
1440 );
1441 writeln!(
1442 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001443 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001444 instance, inner,
1445 );
David Tolnay219c0792020-04-24 20:31:37 -07001446 writeln!(
1447 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001448 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001449 instance, inner,
1450 );
1451 writeln!(
1452 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001453 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001454 inner, instance,
1455 );
David Tolnay503d0192020-04-24 22:18:56 -07001456 writeln!(
1457 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001458 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001459 instance, inner,
1460 );
1461 writeln!(
1462 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001463 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001464 instance, inner,
1465 );
1466 writeln!(
1467 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001468 "::std::size_t cxxbridge1$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001469 instance,
1470 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001471 writeln!(out, "#endif // CXXBRIDGE1_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001472}
1473
David Tolnay8faec772020-11-02 00:18:19 -08001474fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001475 let inner = ident.to_fully_qualified();
1476 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001477
1478 writeln!(out, "template <>");
David Tolnaye5703162020-12-12 16:26:35 -08001479 writeln!(
1480 out,
1481 "{} *Box<{}>::allocation::alloc() noexcept {{",
1482 inner, inner,
1483 );
David Tolnayc4b34222020-12-12 13:06:26 -08001484 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001485 writeln!(out, "}}");
1486
1487 writeln!(out, "template <>");
David Tolnay25b3c1d2020-12-12 15:50:10 -08001488 writeln!(
1489 out,
David Tolnaye5703162020-12-12 16:26:35 -08001490 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001491 inner, inner,
1492 );
1493 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1494 writeln!(out, "}}");
1495
1496 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001497 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001498 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001499 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001500}
1501
David Tolnay75ea17c2020-12-06 21:08:34 -08001502fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001503 let inner = element.to_typename(out.types);
1504 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001505
David Tolnay12320e12020-12-09 23:09:36 -08001506 out.include.cstddef = true;
1507
Myron Ahneba35cf2020-02-05 19:41:51 +07001508 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001509 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001510 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001511 writeln!(out, "}}");
1512
1513 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001514 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001515 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001516 writeln!(out, "}}");
1517
1518 writeln!(out, "template <>");
David Tolnaybe3cbf72020-12-12 22:12:07 -08001519 writeln!(
1520 out,
1521 "::std::size_t Vec<{}>::size() const noexcept {{",
1522 inner,
1523 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001524 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001525 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001526
1527 writeln!(out, "template <>");
David Tolnaybe3cbf72020-12-12 22:12:07 -08001528 writeln!(
1529 out,
1530 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1531 inner,
1532 );
David Tolnay381d9532020-12-11 13:59:45 -08001533 writeln!(
1534 out,
1535 " return cxxbridge1$rust_vec${}$capacity(this);",
1536 instance,
1537 );
David Tolnaydc62d712020-12-11 13:51:53 -08001538 writeln!(out, "}}");
1539
1540 writeln!(out, "template <>");
David Tolnay219c0792020-04-24 20:31:37 -07001541 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001542 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001543 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001544
1545 writeln!(out, "template <>");
David Tolnayfb6b73c2020-11-10 14:32:16 -08001546 writeln!(
1547 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001548 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001549 inner,
1550 );
1551 writeln!(
1552 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001553 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001554 instance,
1555 );
1556 writeln!(out, "}}");
1557
1558 writeln!(out, "template <>");
David Tolnaybe3cbf72020-12-12 22:12:07 -08001559 writeln!(
1560 out,
1561 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1562 inner,
1563 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001564 writeln!(
1565 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001566 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001567 instance,
1568 );
1569 writeln!(out, "}}");
1570
1571 writeln!(out, "template <>");
David Tolnaybe3cbf72020-12-12 22:12:07 -08001572 writeln!(out, "::std::size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001573 writeln!(out, " return cxxbridge1$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001574 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001575}
1576
David Tolnay75ea17c2020-12-06 21:08:34 -08001577fn write_unique_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001578 let ty = UniquePtr::Ident(ident);
1579 let instance = ty.to_mangled(out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001580
David Tolnay0f0162f2020-11-16 23:43:37 -08001581 writeln!(out, "#ifndef CXXBRIDGE1_UNIQUE_PTR_{}", instance);
1582 writeln!(out, "#define CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001583
David Tolnay1bdb4712020-11-25 07:27:54 -08001584 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001585
David Tolnay0f0162f2020-11-16 23:43:37 -08001586 writeln!(out, "#endif // CXXBRIDGE1_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001587}
1588
1589// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001590fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001591 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001592 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001593 let inner = ty.to_typename(out.types);
1594 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001595
David Tolnay63da4d32020-04-25 09:41:12 -07001596 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001597 // Some aliases are to opaque types; some are to trivial types. We can't
1598 // know at code generation time, so we generate both C++ and Rust side
1599 // bindings for a "new" method anyway. But the Rust code can't be called
1600 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001601 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001602 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001603 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001604 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001605 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001606 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001607 };
1608
David Tolnay53462762020-11-25 07:08:10 -08001609 let conditional_delete = match ty {
1610 UniquePtr::Ident(ident) => {
1611 !out.types.structs.contains_key(&ident.rust)
1612 && !out.types.enums.contains_key(&ident.rust)
1613 }
1614 UniquePtr::CxxVector(_) => false,
1615 };
1616
1617 if conditional_delete {
1618 out.builtin.is_complete = true;
1619 let definition = match ty {
1620 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1621 UniquePtr::CxxVector(_) => unreachable!(),
1622 };
1623 writeln!(
1624 out,
David Tolnay75068632020-12-26 22:15:17 -08001625 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001626 inner, definition,
1627 );
1628 }
David Tolnay7db73692019-10-20 14:51:12 -04001629 writeln!(
1630 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001631 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001632 inner,
1633 );
1634 writeln!(
1635 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001636 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001637 inner,
1638 );
1639 writeln!(
1640 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001641 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001642 instance, inner,
1643 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001644 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001645 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001646 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001647 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001648 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001649 out,
David Tolnay0b881402020-12-01 21:05:08 -08001650 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1651 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001652 );
David Tolnay63da4d32020-04-25 09:41:12 -07001653 writeln!(
1654 out,
David Tolnay0b881402020-12-01 21:05:08 -08001655 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1656 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001657 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001658 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001659 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001660 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001661 }
David Tolnay7db73692019-10-20 14:51:12 -04001662 writeln!(
1663 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001664 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001665 instance, inner, inner,
1666 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001667 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001668 writeln!(out, "}}");
1669 writeln!(
1670 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001671 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001672 inner, instance, inner,
1673 );
1674 writeln!(out, " return ptr.get();");
1675 writeln!(out, "}}");
1676 writeln!(
1677 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001678 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001679 inner, instance, inner,
1680 );
1681 writeln!(out, " return ptr.release();");
1682 writeln!(out, "}}");
1683 writeln!(
1684 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001685 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001686 instance, inner,
1687 );
David Tolnay53462762020-11-25 07:08:10 -08001688 if conditional_delete {
1689 out.builtin.deleter_if = true;
1690 writeln!(
1691 out,
David Tolnay75068632020-12-26 22:15:17 -08001692 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001693 inner,
1694 );
1695 } else {
1696 writeln!(out, " ptr->~unique_ptr();");
1697 }
David Tolnay7db73692019-10-20 14:51:12 -04001698 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001699}
Myron Ahneba35cf2020-02-05 19:41:51 +07001700
David Tolnay75ea17c2020-12-06 21:08:34 -08001701fn write_shared_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnayb3b24a12020-12-01 15:27:43 -08001702 let resolved = out.types.resolve(ident);
1703 let inner = resolved.to_fully_qualified();
1704 let instance = ident.to_symbol(out.types);
1705
1706 writeln!(out, "#ifndef CXXBRIDGE1_SHARED_PTR_{}", instance);
1707 writeln!(out, "#define CXXBRIDGE1_SHARED_PTR_{}", instance);
1708 out.include.new = true;
1709 out.include.utility = true;
1710
1711 // Some aliases are to opaque types; some are to trivial types. We can't
1712 // know at code generation time, so we generate both C++ and Rust side
1713 // bindings for a "new" method anyway. But the Rust code can't be called for
1714 // Opaque types because the 'new' method is not implemented.
1715 let can_construct_from_value = out.types.structs.contains_key(&ident.rust)
1716 || out.types.enums.contains_key(&ident.rust)
1717 || out.types.aliases.contains_key(&ident.rust);
1718
David Tolnayb3b24a12020-12-01 15:27:43 -08001719 writeln!(
1720 out,
1721 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1722 inner,
1723 );
1724 writeln!(
1725 out,
1726 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1727 inner,
1728 );
1729 writeln!(
1730 out,
1731 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1732 instance, inner,
1733 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001734 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001735 writeln!(out, "}}");
1736 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001737 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001738 writeln!(
1739 out,
David Tolnay0b881402020-12-01 21:05:08 -08001740 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1741 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001742 );
1743 writeln!(
1744 out,
David Tolnay0b881402020-12-01 21:05:08 -08001745 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1746 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001747 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001748 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001749 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001750 writeln!(out, "}}");
1751 }
1752 writeln!(
1753 out,
1754 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1755 instance, inner, inner,
1756 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001757 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001758 writeln!(out, "}}");
1759 writeln!(
1760 out,
1761 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1762 inner, instance, inner,
1763 );
1764 writeln!(out, " return self.get();");
1765 writeln!(out, "}}");
1766 writeln!(
1767 out,
1768 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1769 instance, inner,
1770 );
1771 writeln!(out, " self->~shared_ptr();");
1772 writeln!(out, "}}");
1773
1774 writeln!(out, "#endif // CXXBRIDGE1_SHARED_PTR_{}", instance);
1775}
1776
David Tolnay75ea17c2020-12-06 21:08:34 -08001777fn write_cxx_vector(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001778 let inner = element.to_typename(out.types);
1779 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001780
David Tolnay12320e12020-12-09 23:09:36 -08001781 out.include.cstddef = true;
1782
David Tolnay0f0162f2020-11-16 23:43:37 -08001783 writeln!(out, "#ifndef CXXBRIDGE1_VECTOR_{}", instance);
1784 writeln!(out, "#define CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001785 writeln!(
1786 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001787 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001788 instance, inner,
1789 );
1790 writeln!(out, " return s.size();");
1791 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001792 writeln!(
1793 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001794 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001795 inner, instance, inner,
1796 );
David Tolnay767e00d2020-12-21 17:12:27 -08001797 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001798 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001799
David Tolnay70820672020-12-07 11:15:14 -08001800 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001801 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
David Tolnay63da4d32020-04-25 09:41:12 -07001802
David Tolnay0f0162f2020-11-16 23:43:37 -08001803 writeln!(out, "#endif // CXXBRIDGE1_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001804}