blob: 6b891786550a55e48bb54ea68a767014e59a771e [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 Tolnay215e77f2020-12-28 17:09:48 -0800211 Type::SharedPtr(_) | Type::WeakPtr(_) => 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 {
David Tolnay5573e522020-12-31 11:07:38 -0800236 for line in field.doc.to_string().lines() {
237 writeln!(out, " //{}", line);
238 }
David Tolnay7db73692019-10-20 14:51:12 -0400239 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700240 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400241 writeln!(out, "{};", field.ident);
242 }
David Tolnay84389352020-11-27 17:12:10 -0800243
David Tolnay5554ebd2020-12-10 11:34:41 -0800244 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800245
David Tolnay102c7ea2020-11-08 18:58:09 -0800246 for method in methods {
247 write!(out, " ");
248 let sig = &method.sig;
249 let local_name = method.name.cxx.to_string();
250 write_rust_function_shim_decl(out, &local_name, sig, false);
251 writeln!(out, ";");
252 }
David Tolnay84389352020-11-27 17:12:10 -0800253
David Tolnayb960ed22020-11-27 14:34:30 -0800254 if operator_eq {
255 writeln!(
256 out,
257 " bool operator==(const {} &) const noexcept;",
258 strct.name.cxx,
259 );
260 writeln!(
261 out,
262 " bool operator!=(const {} &) const noexcept;",
263 strct.name.cxx,
264 );
265 }
David Tolnay84389352020-11-27 17:12:10 -0800266
267 if operator_ord {
268 writeln!(
269 out,
270 " bool operator<(const {} &) const noexcept;",
271 strct.name.cxx,
272 );
273 writeln!(
274 out,
275 " bool operator<=(const {} &) const noexcept;",
276 strct.name.cxx,
277 );
278 writeln!(
279 out,
280 " bool operator>(const {} &) const noexcept;",
281 strct.name.cxx,
282 );
283 writeln!(
284 out,
285 " bool operator>=(const {} &) const noexcept;",
286 strct.name.cxx,
287 );
288 }
289
David Tolnay5554ebd2020-12-10 11:34:41 -0800290 out.include.type_traits = true;
291 writeln!(out, " using IsRelocatable = ::std::true_type;");
292
David Tolnay7db73692019-10-20 14:51:12 -0400293 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700294 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400295}
296
297fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
298 writeln!(out, "struct {};", ident);
299}
300
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800301fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800302 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800303 write_atom(out, enm.repr);
304 writeln!(out, ";");
305}
306
David Tolnay8faec772020-11-02 00:18:19 -0800307fn write_struct_using(out: &mut OutFile, ident: &Pair) {
308 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800309}
310
David Tolnay8d067de2020-12-26 16:18:23 -0800311fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800312 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800313 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700314 writeln!(out, "#ifndef {}", guard);
315 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700316 for line in ety.doc.to_string().lines() {
317 writeln!(out, "//{}", line);
318 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800319
David Tolnay365fc7c2020-11-25 16:08:13 -0800320 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800321 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800322 out,
323 "struct {} final : public ::rust::Opaque {{",
324 ety.name.cxx,
325 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800326
Joel Galenson968738f2020-04-15 14:19:33 -0700327 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800328 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700329 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800330 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700331 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800332 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800333 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800334
David Tolnay8d067de2020-12-26 16:18:23 -0800335 if !methods.is_empty() {
336 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700337 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800338
David Tolnayee6ecfc2020-12-26 21:54:37 -0800339 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800340 out.include.cstddef = true;
341 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800342 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800343 writeln!(out, " struct layout {{");
344 writeln!(out, " static ::std::size_t size() noexcept;");
345 writeln!(out, " static ::std::size_t align() noexcept;");
346 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700347 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700348 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700349}
350
David Tolnay0b9b9f82020-11-01 20:41:00 -0800351fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800352 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800353 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700354 writeln!(out, "#ifndef {}", guard);
355 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700356 for line in enm.doc.to_string().lines() {
357 writeln!(out, "//{}", line);
358 }
David Tolnay17a934c2020-11-02 00:40:04 -0800359 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700360 write_atom(out, enm.repr);
361 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700362 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800363 for line in variant.doc.to_string().lines() {
364 writeln!(out, " //{}", line);
365 }
David Tolnaye6f62142020-12-21 16:00:41 -0800366 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700367 }
368 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700369 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700370}
371
David Tolnay0b9b9f82020-11-01 20:41:00 -0800372fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800373 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800374 out.include.type_traits = true;
375 writeln!(
376 out,
377 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
378 enm.name.cxx,
379 );
David Tolnay17a934c2020-11-02 00:40:04 -0800380 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700381 write_atom(out, enm.repr);
382 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700383 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700384 write!(out, "static_assert(static_cast<");
385 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700386 writeln!(
387 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700388 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800389 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700390 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700391 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700392}
393
David Tolnay5b1d8632020-12-20 22:05:11 -0800394fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800395 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700396 // necessary for soundness. That's because triviality is always declared by
397 // the user in the form of an unsafe impl of cxx::ExternType:
398 //
399 // unsafe impl ExternType for MyType {
400 // type Id = cxx::type_id!("...");
401 // type Kind = cxx::kind::Trivial;
402 // }
403 //
404 // Since the user went on the record with their unsafe impl to unsafely
405 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800406 // them if that were wrong. However, in practice correctly reasoning about
407 // the relocatability of C++ types is challenging, particularly if the type
408 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700409 //
David Tolnay174bd952020-11-02 09:23:12 -0800410 // There may be legitimate reasons to opt out of this assertion for support
411 // of types that the programmer knows are soundly Rust-movable despite not
412 // being recognized as such by the C++ type system due to a move constructor
413 // or destructor. To opt out of the relocatability check, they need to do
414 // one of the following things in any header used by `include!` in their
415 // bridge.
416 //
417 // --- if they define the type:
418 // struct MyType {
419 // ...
420 // + using IsRelocatable = std::true_type;
421 // };
422 //
423 // --- otherwise:
424 // + template <>
425 // + struct rust::IsRelocatable<MyType> : std::true_type {};
426 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700427
David Tolnay5b1d8632020-12-20 22:05:11 -0800428 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800429 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800430 writeln!(out, "static_assert(");
431 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
432 writeln!(
433 out,
434 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
435 id.trim_start_matches("::"),
436 trivial::as_what(&alias.name, reasons),
437 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700438}
439
David Tolnayb960ed22020-11-27 14:34:30 -0800440fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
441 out.set_namespace(&strct.name.namespace);
442 out.begin_block(Block::ExternC);
443
444 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800445 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800446 writeln!(
447 out,
448 "bool {}(const {1} &, const {1} &) noexcept;",
449 link_name, strct.name.cxx,
450 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800451
452 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800453 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800454 writeln!(
455 out,
456 "bool {}(const {1} &, const {1} &) noexcept;",
457 link_name, strct.name.cxx,
458 );
459 }
David Tolnayb960ed22020-11-27 14:34:30 -0800460 }
461
David Tolnay84389352020-11-27 17:12:10 -0800462 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800463 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800464 writeln!(
465 out,
466 "bool {}(const {1} &, const {1} &) noexcept;",
467 link_name, strct.name.cxx,
468 );
469
David Tolnaya05f9402020-11-27 17:44:05 -0800470 let link_name = mangle::operator(&strct.name, "le");
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
477 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800478 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800479 writeln!(
480 out,
481 "bool {}(const {1} &, const {1} &) noexcept;",
482 link_name, strct.name.cxx,
483 );
484
David Tolnaya05f9402020-11-27 17:44:05 -0800485 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800486 writeln!(
487 out,
488 "bool {}(const {1} &, const {1} &) noexcept;",
489 link_name, strct.name.cxx,
490 );
491 }
492 }
493
David Tolnay7da38202020-11-27 17:36:16 -0800494 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800495 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800496 let link_name = mangle::operator(&strct.name, "hash");
497 writeln!(
498 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800499 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800500 link_name, strct.name.cxx,
501 );
502 }
503
David Tolnayb960ed22020-11-27 14:34:30 -0800504 out.end_block(Block::ExternC);
505}
506
507fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
508 if out.header {
509 return;
510 }
511
512 out.set_namespace(&strct.name.namespace);
513
514 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800515 out.next_section();
516 writeln!(
517 out,
518 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
519 strct.name.cxx,
520 );
David Tolnaya05f9402020-11-27 17:44:05 -0800521 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800522 writeln!(out, " return {}(*this, rhs);", link_name);
523 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800524
David Tolnayb960ed22020-11-27 14:34:30 -0800525 out.next_section();
526 writeln!(
527 out,
528 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
529 strct.name.cxx,
530 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800531 if derive::contains(&strct.derives, Trait::Eq) {
532 writeln!(out, " return !(*this == rhs);");
533 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800534 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800535 writeln!(out, " return {}(*this, rhs);", link_name);
536 }
David Tolnayb960ed22020-11-27 14:34:30 -0800537 writeln!(out, "}}");
538 }
David Tolnay84389352020-11-27 17:12:10 -0800539
540 if derive::contains(&strct.derives, Trait::PartialOrd) {
541 out.next_section();
542 writeln!(
543 out,
544 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
545 strct.name.cxx,
546 );
David Tolnaya05f9402020-11-27 17:44:05 -0800547 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800548 writeln!(out, " return {}(*this, rhs);", link_name);
549 writeln!(out, "}}");
550
551 out.next_section();
552 writeln!(
553 out,
554 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
555 strct.name.cxx,
556 );
David Tolnaya05f9402020-11-27 17:44:05 -0800557 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800558 writeln!(out, " return {}(*this, rhs);", link_name);
559 writeln!(out, "}}");
560
561 out.next_section();
562 writeln!(
563 out,
564 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
565 strct.name.cxx,
566 );
567 if derive::contains(&strct.derives, Trait::Ord) {
568 writeln!(out, " return !(*this <= rhs);");
569 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800570 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800571 writeln!(out, " return {}(*this, rhs);", link_name);
572 }
573 writeln!(out, "}}");
574
575 out.next_section();
576 writeln!(
577 out,
578 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
579 strct.name.cxx,
580 );
581 if derive::contains(&strct.derives, Trait::Ord) {
582 writeln!(out, " return !(*this < rhs);");
583 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800584 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800585 writeln!(out, " return {}(*this, rhs);", link_name);
586 }
587 writeln!(out, "}}");
588 }
David Tolnayb960ed22020-11-27 14:34:30 -0800589}
590
David Tolnay358bc4b2020-12-26 22:37:57 -0800591fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
592 out.set_namespace(&ety.name.namespace);
593 out.begin_block(Block::ExternC);
594
595 let link_name = mangle::operator(&ety.name, "sizeof");
596 writeln!(out, "::std::size_t {}() noexcept;", link_name);
597
598 let link_name = mangle::operator(&ety.name, "alignof");
599 writeln!(out, "::std::size_t {}() noexcept;", link_name);
600
601 out.end_block(Block::ExternC);
602}
603
604fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
605 if out.header {
606 return;
607 }
608
609 out.set_namespace(&ety.name.namespace);
610
611 out.next_section();
612 let link_name = mangle::operator(&ety.name, "sizeof");
613 writeln!(
614 out,
615 "::std::size_t {}::layout::size() noexcept {{",
616 ety.name.cxx,
617 );
618 writeln!(out, " return {}();", link_name);
619 writeln!(out, "}}");
620
621 out.next_section();
622 let link_name = mangle::operator(&ety.name, "alignof");
623 writeln!(
624 out,
625 "::std::size_t {}::layout::align() noexcept {{",
626 ety.name.cxx,
627 );
628 writeln!(out, " return {}();", link_name);
629 writeln!(out, "}}");
630}
631
David Tolnay1eadd262020-12-29 16:42:08 -0800632fn begin_function_definition(out: &mut OutFile) {
633 if let Some(annotation) = &out.opt.cxx_impl_annotations {
634 write!(out, "{} ", annotation);
635 }
636}
637
David Tolnay0b9b9f82020-11-01 20:41:00 -0800638fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800639 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800640 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800641 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800642 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700643 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700644 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700645 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700646 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700647 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700648 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700649 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700650 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700651 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800652 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700653 write!(out, "const ");
654 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700655 write!(
656 out,
657 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800658 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700659 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700660 }
David Tolnay7db73692019-10-20 14:51:12 -0400661 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700662 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400663 write!(out, ", ");
664 }
David Tolnaya46a2372020-03-06 10:03:48 -0800665 if arg.ty == RustString {
666 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700667 } else if let Type::RustVec(_) = arg.ty {
668 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800669 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700670 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400671 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700672 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400673 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700674 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400675 write!(out, ", ");
676 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700677 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400678 write!(out, "*return$");
679 }
680 writeln!(out, ") noexcept {{");
681 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700682 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700683 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800684 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700685 Some(receiver) => write!(
686 out,
687 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700688 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800689 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700690 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700691 }
David Tolnay7db73692019-10-20 14:51:12 -0400692 for (i, arg) in efn.args.iter().enumerate() {
693 if i > 0 {
694 write!(out, ", ");
695 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700696 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400697 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700698 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700699 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800700 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700701 write!(out, " const");
702 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700703 }
704 write!(out, " = ");
705 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800706 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700707 Some(receiver) => write!(
708 out,
709 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700710 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800711 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700712 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700713 }
714 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400715 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700716 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700717 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700718 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700719 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700720 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700721 writeln!(out, " [&] {{");
722 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700723 }
David Tolnay7db73692019-10-20 14:51:12 -0400724 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700725 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400726 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700727 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400728 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700729 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400730 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700731 }
732 match &efn.ret {
733 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700734 Some(Type::Str(_)) if !indirect_return => {
735 out.builtin.rust_str_repr = true;
736 write!(out, "::rust::impl<::rust::Str>::repr(");
737 }
David Tolnay73b72642020-11-25 17:44:05 -0800738 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700739 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800740 write!(out, "::rust::impl<");
741 write_type(out, ty);
742 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700743 }
David Tolnay99642622020-03-25 13:07:35 -0700744 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400745 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700746 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800747 None => write!(out, "{}$(", efn.name.rust),
748 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700749 }
David Tolnay7db73692019-10-20 14:51:12 -0400750 for (i, arg) in efn.args.iter().enumerate() {
751 if i > 0 {
752 write!(out, ", ");
753 }
754 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700755 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400756 write!(out, "::from_raw({})", arg.ident);
757 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700758 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400759 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700760 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700761 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700762 write!(
763 out,
764 "::rust::impl<::rust::Str>::new_unchecked({})",
765 arg.ident,
766 );
David Tolnaya46a2372020-03-06 10:03:48 -0800767 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700768 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800769 write!(
770 out,
771 "::rust::String(::rust::unsafe_bitcopy, *{})",
772 arg.ident,
773 );
David Tolnay313b10e2020-04-25 16:30:51 -0700774 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700775 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700776 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700777 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay73b72642020-11-25 17:44:05 -0800778 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800779 write_type(out, &arg.ty);
780 write!(out, "(static_cast<");
781 if slice.mutability.is_none() {
782 write!(out, "const ");
783 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800784 write_type_space(out, &slice.inner);
785 write!(out, "*>({0}.ptr), {0}.len)", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700786 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700787 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800788 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400789 } else {
790 write!(out, "{}", arg.ident);
791 }
792 }
793 write!(out, ")");
794 match &efn.ret {
795 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
796 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800797 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400798 _ => {}
799 }
800 if indirect_return {
801 write!(out, ")");
802 }
803 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700804 if efn.throws {
805 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800806 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700807 writeln!(out, " throw$.ptr = nullptr;");
808 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700809 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700810 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700811 writeln!(
812 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800813 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700814 );
David Tolnay5d121442020-03-17 22:14:40 -0700815 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700816 writeln!(out, " return throw$;");
817 }
David Tolnay7db73692019-10-20 14:51:12 -0400818 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700819 for arg in &efn.args {
820 if let Type::Fn(f) = &arg.ty {
821 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700822 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700823 }
824 }
David Tolnayca563ee2020-11-01 20:12:27 -0800825 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700826}
827
828fn write_function_pointer_trampoline(
829 out: &mut OutFile,
830 efn: &ExternFn,
831 var: &Ident,
832 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700833) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700834 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700835 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700836 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700837
838 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700839 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
840 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400841}
842
David Tolnay0b9b9f82020-11-01 20:41:00 -0800843fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800844 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800845 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700846 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700847 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700848 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800849 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700850}
851
852fn write_rust_function_decl_impl(
853 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700854 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700855 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700856 indirect_call: bool,
857) {
David Tolnay04770742020-11-01 13:50:50 -0800858 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700859 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700860 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700861 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700862 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700863 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700864 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700865 write!(out, "{}(", link_name);
866 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700867 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800868 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700869 write!(out, "const ");
870 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700871 write!(
872 out,
873 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800874 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700875 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700876 needs_comma = true;
877 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700878 for arg in &sig.args {
879 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400880 write!(out, ", ");
881 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700882 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700883 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400884 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700885 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700886 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400887 write!(out, ", ");
888 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700889 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400890 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700891 needs_comma = true;
892 }
893 if indirect_call {
894 if needs_comma {
895 write!(out, ", ");
896 }
897 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400898 }
899 writeln!(out, ") noexcept;");
900}
901
David Tolnay0b9b9f82020-11-01 20:41:00 -0800902fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800903 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400904 for line in efn.doc.to_string().lines() {
905 writeln!(out, "//{}", line);
906 }
David Tolnaya73853b2020-04-20 01:19:56 -0700907 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800908 None => efn.name.cxx.to_string(),
909 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700910 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700911 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700912 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700913 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700914}
915
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700916fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700917 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700918 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700919 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700920 indirect_call: bool,
921) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700922 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700923 write!(out, "{}(", local_name);
924 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400925 if i > 0 {
926 write!(out, ", ");
927 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700928 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400929 write!(out, "{}", arg.ident);
930 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700931 if indirect_call {
932 if !sig.args.is_empty() {
933 write!(out, ", ");
934 }
935 write!(out, "void *extern$");
936 }
David Tolnay1e548172020-03-16 13:37:09 -0700937 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700938 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800939 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700940 write!(out, " const");
941 }
942 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700943 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700944 write!(out, " noexcept");
945 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700946}
947
948fn write_rust_function_shim_impl(
949 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700950 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700951 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700952 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700953 indirect_call: bool,
954) {
955 if out.header && sig.receiver.is_some() {
956 // We've already defined this inside the struct.
957 return;
958 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800959 if !out.header {
960 begin_function_definition(out);
961 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700962 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400963 if out.header {
964 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700965 return;
David Tolnay7db73692019-10-20 14:51:12 -0400966 }
David Tolnay439cde22020-04-20 00:46:25 -0700967 writeln!(out, " {{");
968 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700969 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700970 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700971 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700972 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700973 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700974 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
975 }
976 }
977 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700978 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700979 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700980 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700981 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700982 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700983 writeln!(out, "> return$;");
984 write!(out, " ");
985 } else if let Some(ret) = &sig.ret {
986 write!(out, "return ");
987 match ret {
988 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700989 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700990 write!(out, "::from_raw(");
991 }
992 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700993 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700994 write!(out, "(");
995 }
996 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700997 Type::Str(_) => {
998 out.builtin.rust_str_new_unchecked = true;
999 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
1000 }
David Tolnay73b72642020-11-25 17:44:05 -08001001 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001002 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001003 write!(out, "::rust::impl<");
1004 write_type(out, ret);
1005 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001006 }
David Tolnay439cde22020-04-20 00:46:25 -07001007 _ => {}
1008 }
1009 }
1010 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -07001011 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001012 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001013 }
1014 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001015 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001016 if sig.receiver.is_some() {
1017 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001018 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001019 }
David Tolnay9d840362020-11-10 08:50:40 -08001020 for arg in &sig.args {
1021 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001022 write!(out, ", ");
1023 }
1024 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -07001025 Type::Str(_) => {
1026 out.builtin.rust_str_repr = true;
1027 write!(out, "::rust::impl<::rust::Str>::repr(");
1028 }
David Tolnay73b72642020-11-25 17:44:05 -08001029 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001030 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001031 write!(out, "::rust::impl<");
1032 write_type(out, &arg.ty);
1033 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001034 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001035 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -07001036 _ => {}
1037 }
1038 write!(out, "{}", arg.ident);
1039 match &arg.ty {
1040 Type::RustBox(_) => write!(out, ".into_raw()"),
1041 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -08001042 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001043 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001044 _ => {}
1045 }
David Tolnay9d840362020-11-10 08:50:40 -08001046 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001047 }
1048 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001049 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001050 write!(out, ", ");
1051 }
1052 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001053 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001054 }
1055 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001056 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001057 write!(out, ", ");
1058 }
1059 write!(out, "extern$");
1060 }
1061 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001062 if !indirect_return {
1063 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -08001064 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001065 write!(out, ")");
1066 }
David Tolnay439cde22020-04-20 00:46:25 -07001067 }
1068 }
1069 writeln!(out, ";");
1070 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001071 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001072 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001073 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001074 writeln!(out, " }}");
1075 }
1076 if indirect_return {
1077 out.include.utility = true;
1078 writeln!(out, " return ::std::move(return$.value);");
1079 }
1080 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001081}
1082
David Tolnaya7c2ea12020-10-30 21:32:53 -07001083fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001084 match ty {
1085 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001086 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001087 }
1088}
1089
David Tolnay75dca2e2020-03-25 20:17:52 -07001090fn indirect_return(sig: &Signature, types: &Types) -> bool {
1091 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001092 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001093 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001094}
1095
David Tolnaya7c2ea12020-10-30 21:32:53 -07001096fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001097 match ty {
1098 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001099 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001100 write!(out, "*");
1101 }
1102 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001103 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001104 write!(out, "const ");
1105 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001106 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001107 write!(out, " *");
1108 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001109 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001110 }
1111}
1112
David Tolnaya7c2ea12020-10-30 21:32:53 -07001113fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1114 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001115 match ty {
1116 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001117 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001118 _ => write_space_after_type(out, ty),
1119 }
1120}
1121
David Tolnaya7c2ea12020-10-30 21:32:53 -07001122fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001123 match ty {
1124 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001125 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001126 write!(out, "*");
1127 }
David Tolnay4a441222020-01-25 16:24:27 -08001128 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001129 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001130 write!(out, "const ");
1131 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001132 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001133 write!(out, " *");
1134 }
David Tolnay73b72642020-11-25 17:44:05 -08001135 Some(Type::Str(_)) | Some(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 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1140 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001141 }
1142}
1143
David Tolnaya7c2ea12020-10-30 21:32:53 -07001144fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001145 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001146 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001147 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001148 write!(out, "*");
1149 }
David Tolnay73b72642020-11-25 17:44:05 -08001150 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001151 out.builtin.ptr_len = true;
1152 write!(out, "::rust::repr::PtrLen ");
1153 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001154 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001155 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001156 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001157 write!(out, "*");
1158 }
1159 write!(out, "{}", arg.ident);
1160}
1161
David Tolnaya7c2ea12020-10-30 21:32:53 -07001162fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001163 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001164 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001165 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001166 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -04001167 },
1168 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001169 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001170 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001171 write!(out, ">");
1172 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001173 Type::RustVec(ty) => {
1174 write!(out, "::rust::Vec<");
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::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001179 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001180 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001181 write!(out, ">");
1182 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001183 Type::SharedPtr(ptr) => {
1184 write!(out, "::std::shared_ptr<");
1185 write_type(out, &ptr.inner);
1186 write!(out, ">");
1187 }
David Tolnay215e77f2020-12-28 17:09:48 -08001188 Type::WeakPtr(ptr) => {
1189 write!(out, "::std::weak_ptr<");
1190 write_type(out, &ptr.inner);
1191 write!(out, ">");
1192 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001193 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001194 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001195 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001196 write!(out, ">");
1197 }
David Tolnay7db73692019-10-20 14:51:12 -04001198 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001199 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001200 write!(out, "const ");
1201 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001202 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001203 write!(out, " &");
1204 }
1205 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001206 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001207 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001208 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001209 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001210 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001211 write!(out, "const ");
1212 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001213 write_type(out, &slice.inner);
1214 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001215 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001216 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001217 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001218 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001219 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001220 None => write!(out, "void"),
1221 }
1222 write!(out, "(");
1223 for (i, arg) in f.args.iter().enumerate() {
1224 if i > 0 {
1225 write!(out, ", ");
1226 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001227 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001228 }
1229 write!(out, ")>");
1230 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001231 Type::Array(a) => {
1232 write!(out, "::std::array<");
1233 write_type(out, &a.inner);
1234 write!(out, ", {}>", &a.len);
1235 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001236 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001237 }
1238}
1239
David Tolnayf6a89f22020-05-10 23:39:27 -07001240fn write_atom(out: &mut OutFile, atom: Atom) {
1241 match atom {
1242 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001243 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001244 U8 => write!(out, "::std::uint8_t"),
1245 U16 => write!(out, "::std::uint16_t"),
1246 U32 => write!(out, "::std::uint32_t"),
1247 U64 => write!(out, "::std::uint64_t"),
1248 Usize => write!(out, "::std::size_t"),
1249 I8 => write!(out, "::std::int8_t"),
1250 I16 => write!(out, "::std::int16_t"),
1251 I32 => write!(out, "::std::int32_t"),
1252 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001253 Isize => write!(out, "::rust::isize"),
1254 F32 => write!(out, "float"),
1255 F64 => write!(out, "double"),
1256 CxxString => write!(out, "::std::string"),
1257 RustString => write!(out, "::rust::String"),
1258 }
1259}
1260
David Tolnaya7c2ea12020-10-30 21:32:53 -07001261fn write_type_space(out: &mut OutFile, ty: &Type) {
1262 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001263 write_space_after_type(out, ty);
1264}
1265
1266fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001267 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001268 Type::Ident(_)
1269 | Type::RustBox(_)
1270 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001271 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001272 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001273 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001274 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001275 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001276 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001277 | Type::Fn(_)
1278 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001279 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001280 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001281 }
1282}
1283
David Tolnay1bdb4712020-11-25 07:27:54 -08001284#[derive(Copy, Clone)]
1285enum UniquePtr<'a> {
David Tolnay75ea17c2020-12-06 21:08:34 -08001286 Ident(&'a RustName),
1287 CxxVector(&'a RustName),
David Tolnay1bdb4712020-11-25 07:27:54 -08001288}
1289
1290trait ToTypename {
1291 fn to_typename(&self, types: &Types) -> String;
1292}
1293
David Tolnay75ea17c2020-12-06 21:08:34 -08001294impl ToTypename for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001295 fn to_typename(&self, types: &Types) -> String {
1296 types.resolve(self).to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001297 }
1298}
1299
David Tolnay1bdb4712020-11-25 07:27:54 -08001300impl<'a> ToTypename for UniquePtr<'a> {
1301 fn to_typename(&self, types: &Types) -> String {
1302 match self {
1303 UniquePtr::Ident(ident) => ident.to_typename(types),
1304 UniquePtr::CxxVector(element) => {
1305 format!("::std::vector<{}>", element.to_typename(types))
1306 }
1307 }
1308 }
1309}
1310
1311trait ToMangled {
1312 fn to_mangled(&self, types: &Types) -> Symbol;
1313}
1314
David Tolnay75ea17c2020-12-06 21:08:34 -08001315impl ToMangled for RustName {
David Tolnay1bdb4712020-11-25 07:27:54 -08001316 fn to_mangled(&self, types: &Types) -> Symbol {
1317 self.to_symbol(types)
1318 }
1319}
1320
1321impl<'a> ToMangled for UniquePtr<'a> {
1322 fn to_mangled(&self, types: &Types) -> Symbol {
1323 match self {
1324 UniquePtr::Ident(ident) => ident.to_mangled(types),
1325 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1326 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001327 }
1328}
1329
David Tolnaya7c2ea12020-10-30 21:32:53 -07001330fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001331 if out.header {
1332 return;
1333 }
1334
1335 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001336 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001337 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -07001338 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001339 if let Type::RustBox(ptr) = ty {
1340 if let Type::Ident(inner) = &ptr.inner {
1341 if Atom::from(&inner.rust).is_none()
1342 && (!out.types.aliases.contains_key(&inner.rust)
1343 || out.types.explicit_impls.contains(ty))
1344 {
1345 out.next_section();
1346 write_rust_box_extern(out, &out.types.resolve(&inner));
1347 }
David Tolnay7db73692019-10-20 14:51:12 -04001348 }
David Tolnayf33bc242020-12-04 18:27:02 -08001349 } else if let Type::RustVec(vec) = ty {
1350 if let Type::Ident(inner) = &vec.inner {
1351 if Atom::from(&inner.rust).is_none()
1352 && (!out.types.aliases.contains_key(&inner.rust)
1353 || out.types.explicit_impls.contains(ty))
1354 {
David Tolnay6787be62020-04-25 11:01:02 -07001355 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001356 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001357 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001358 }
David Tolnay7db73692019-10-20 14:51:12 -04001359 } else if let Type::UniquePtr(ptr) = ty {
1360 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001361 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001362 && (!out.types.aliases.contains_key(&inner.rust)
1363 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001364 {
David Tolnay7db73692019-10-20 14:51:12 -04001365 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001366 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001367 }
1368 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001369 } else if let Type::SharedPtr(ptr) = ty {
1370 if let Type::Ident(inner) = &ptr.inner {
1371 if Atom::from(&inner.rust).is_none()
David Tolnayb7453812020-12-01 21:46:55 -08001372 && (!out.types.aliases.contains_key(&inner.rust)
David Tolnayb3b24a12020-12-01 15:27:43 -08001373 || out.types.explicit_impls.contains(ty))
1374 {
1375 out.next_section();
1376 write_shared_ptr(out, inner);
1377 }
1378 }
David Tolnay215e77f2020-12-28 17:09:48 -08001379 } else if let Type::WeakPtr(ptr) = ty {
1380 if let Type::Ident(inner) = &ptr.inner {
1381 if Atom::from(&inner.rust).is_none()
1382 && (!out.types.aliases.contains_key(&inner.rust)
1383 || out.types.explicit_impls.contains(ty))
1384 {
1385 out.next_section();
1386 write_weak_ptr(out, inner);
1387 }
1388 }
David Tolnayf33bc242020-12-04 18:27:02 -08001389 } else if let Type::CxxVector(vector) = ty {
1390 if let Type::Ident(inner) = &vector.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001391 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001392 && (!out.types.aliases.contains_key(&inner.rust)
1393 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001394 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001395 out.next_section();
David Tolnay1bdb4712020-11-25 07:27:54 -08001396 write_cxx_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001397 }
1398 }
1399 }
1400 }
David Tolnay0c033e32020-11-01 15:15:48 -08001401 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001402
David Tolnay0c033e32020-11-01 15:15:48 -08001403 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001404 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnaya7c2ea12020-10-30 21:32:53 -07001405 for ty in out.types {
David Tolnayf33bc242020-12-04 18:27:02 -08001406 if let Type::RustBox(ptr) = ty {
1407 if let Type::Ident(inner) = &ptr.inner {
1408 if Atom::from(&inner.rust).is_none()
1409 && (!out.types.aliases.contains_key(&inner.rust)
1410 || out.types.explicit_impls.contains(ty))
1411 {
1412 write_rust_box_impl(out, &out.types.resolve(&inner));
1413 }
David Tolnay7db73692019-10-20 14:51:12 -04001414 }
David Tolnayf33bc242020-12-04 18:27:02 -08001415 } else if let Type::RustVec(vec) = ty {
1416 if let Type::Ident(inner) = &vec.inner {
1417 if Atom::from(&inner.rust).is_none()
1418 && (!out.types.aliases.contains_key(&inner.rust)
1419 || out.types.explicit_impls.contains(ty))
1420 {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001421 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001422 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001423 }
David Tolnay7db73692019-10-20 14:51:12 -04001424 }
1425 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001426 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001427 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001428}
1429
David Tolnay8faec772020-11-02 00:18:19 -08001430fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001431 let inner = ident.to_fully_qualified();
1432 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001433
David Tolnay7db73692019-10-20 14:51:12 -04001434 writeln!(
1435 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001436 "{} *cxxbridge1$box${}$alloc() noexcept;",
1437 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001438 );
1439 writeln!(
1440 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001441 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1442 instance, inner,
1443 );
1444 writeln!(
1445 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001446 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001447 instance, inner,
1448 );
David Tolnay7db73692019-10-20 14:51:12 -04001449}
1450
David Tolnay75ea17c2020-12-06 21:08:34 -08001451fn write_rust_vec_extern(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001452 let inner = element.to_typename(out.types);
1453 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001454
David Tolnay12320e12020-12-09 23:09:36 -08001455 out.include.cstddef = true;
1456
Myron Ahneba35cf2020-02-05 19:41:51 +07001457 writeln!(
1458 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001459 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001460 instance, inner,
1461 );
1462 writeln!(
1463 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001464 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001465 instance, inner,
1466 );
1467 writeln!(
1468 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001469 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001470 instance, inner,
1471 );
David Tolnay219c0792020-04-24 20:31:37 -07001472 writeln!(
1473 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001474 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001475 instance, inner,
1476 );
1477 writeln!(
1478 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001479 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001480 inner, instance,
1481 );
David Tolnay503d0192020-04-24 22:18:56 -07001482 writeln!(
1483 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001484 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001485 instance, inner,
1486 );
1487 writeln!(
1488 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001489 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001490 instance, inner,
1491 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001492}
1493
David Tolnay8faec772020-11-02 00:18:19 -08001494fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001495 let inner = ident.to_fully_qualified();
1496 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001497
1498 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001499 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001500 writeln!(
1501 out,
1502 "{} *Box<{}>::allocation::alloc() noexcept {{",
1503 inner, inner,
1504 );
David Tolnayc4b34222020-12-12 13:06:26 -08001505 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001506 writeln!(out, "}}");
1507
1508 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001509 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001510 writeln!(
1511 out,
David Tolnaye5703162020-12-12 16:26:35 -08001512 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001513 inner, inner,
1514 );
1515 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1516 writeln!(out, "}}");
1517
1518 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001519 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001520 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001521 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001522 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001523}
1524
David Tolnay75ea17c2020-12-06 21:08:34 -08001525fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001526 let inner = element.to_typename(out.types);
1527 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001528
David Tolnay12320e12020-12-09 23:09:36 -08001529 out.include.cstddef = true;
1530
Myron Ahneba35cf2020-02-05 19:41:51 +07001531 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001532 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001533 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001534 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001535 writeln!(out, "}}");
1536
1537 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001538 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001539 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001540 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001541 writeln!(out, "}}");
1542
1543 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001544 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001545 writeln!(
1546 out,
1547 "::std::size_t Vec<{}>::size() const noexcept {{",
1548 inner,
1549 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001550 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001551 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001552
1553 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001554 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001555 writeln!(
1556 out,
1557 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1558 inner,
1559 );
David Tolnay381d9532020-12-11 13:59:45 -08001560 writeln!(
1561 out,
1562 " return cxxbridge1$rust_vec${}$capacity(this);",
1563 instance,
1564 );
David Tolnaydc62d712020-12-11 13:51:53 -08001565 writeln!(out, "}}");
1566
1567 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001568 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001569 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001570 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001571 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001572
1573 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001574 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001575 writeln!(
1576 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001577 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001578 inner,
1579 );
1580 writeln!(
1581 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001582 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001583 instance,
1584 );
1585 writeln!(out, "}}");
1586
1587 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001588 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001589 writeln!(
1590 out,
1591 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1592 inner,
1593 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001594 writeln!(
1595 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001596 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001597 instance,
1598 );
1599 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001600}
1601
David Tolnay75ea17c2020-12-06 21:08:34 -08001602fn write_unique_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001603 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001604 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001605}
1606
1607// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001608fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001609 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001610 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001611 let inner = ty.to_typename(out.types);
1612 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001613
David Tolnay63da4d32020-04-25 09:41:12 -07001614 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001615 // Some aliases are to opaque types; some are to trivial types. We can't
1616 // know at code generation time, so we generate both C++ and Rust side
1617 // bindings for a "new" method anyway. But the Rust code can't be called
1618 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001619 UniquePtr::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001620 out.types.structs.contains_key(&ident.rust)
David Tolnay15609ab2020-11-25 07:14:12 -08001621 || out.types.enums.contains_key(&ident.rust)
David Tolnaya7c2ea12020-10-30 21:32:53 -07001622 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001623 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001624 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001625 };
1626
David Tolnay53462762020-11-25 07:08:10 -08001627 let conditional_delete = match ty {
1628 UniquePtr::Ident(ident) => {
1629 !out.types.structs.contains_key(&ident.rust)
1630 && !out.types.enums.contains_key(&ident.rust)
1631 }
1632 UniquePtr::CxxVector(_) => false,
1633 };
1634
1635 if conditional_delete {
1636 out.builtin.is_complete = true;
1637 let definition = match ty {
1638 UniquePtr::Ident(ty) => &out.types.resolve(ty).cxx,
1639 UniquePtr::CxxVector(_) => unreachable!(),
1640 };
1641 writeln!(
1642 out,
David Tolnay75068632020-12-26 22:15:17 -08001643 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001644 inner, definition,
1645 );
1646 }
David Tolnay7db73692019-10-20 14:51:12 -04001647 writeln!(
1648 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001649 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001650 inner,
1651 );
1652 writeln!(
1653 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001654 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001655 inner,
1656 );
1657 writeln!(
1658 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001659 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001660 instance, inner,
1661 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001662 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001663 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001664 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001665 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001666 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001667 out,
David Tolnay0b881402020-12-01 21:05:08 -08001668 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1669 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001670 );
David Tolnay63da4d32020-04-25 09:41:12 -07001671 writeln!(
1672 out,
David Tolnay0b881402020-12-01 21:05:08 -08001673 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1674 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001675 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001676 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001677 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001678 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001679 }
David Tolnay7db73692019-10-20 14:51:12 -04001680 writeln!(
1681 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001682 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001683 instance, inner, inner,
1684 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001685 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001686 writeln!(out, "}}");
1687 writeln!(
1688 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001689 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001690 inner, instance, inner,
1691 );
1692 writeln!(out, " return ptr.get();");
1693 writeln!(out, "}}");
1694 writeln!(
1695 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001696 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001697 inner, instance, inner,
1698 );
1699 writeln!(out, " return ptr.release();");
1700 writeln!(out, "}}");
1701 writeln!(
1702 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001703 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001704 instance, inner,
1705 );
David Tolnay53462762020-11-25 07:08:10 -08001706 if conditional_delete {
1707 out.builtin.deleter_if = true;
1708 writeln!(
1709 out,
David Tolnay75068632020-12-26 22:15:17 -08001710 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001711 inner,
1712 );
1713 } else {
1714 writeln!(out, " ptr->~unique_ptr();");
1715 }
David Tolnay7db73692019-10-20 14:51:12 -04001716 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001717}
Myron Ahneba35cf2020-02-05 19:41:51 +07001718
David Tolnay75ea17c2020-12-06 21:08:34 -08001719fn write_shared_ptr(out: &mut OutFile, ident: &RustName) {
David Tolnayb3b24a12020-12-01 15:27:43 -08001720 let resolved = out.types.resolve(ident);
1721 let inner = resolved.to_fully_qualified();
1722 let instance = ident.to_symbol(out.types);
1723
David Tolnayb3b24a12020-12-01 15:27:43 -08001724 out.include.new = true;
1725 out.include.utility = true;
1726
1727 // Some aliases are to opaque types; some are to trivial types. We can't
1728 // know at code generation time, so we generate both C++ and Rust side
1729 // bindings for a "new" method anyway. But the Rust code can't be called for
1730 // Opaque types because the 'new' method is not implemented.
1731 let can_construct_from_value = out.types.structs.contains_key(&ident.rust)
1732 || out.types.enums.contains_key(&ident.rust)
1733 || out.types.aliases.contains_key(&ident.rust);
1734
David Tolnayb3b24a12020-12-01 15:27:43 -08001735 writeln!(
1736 out,
1737 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1738 inner,
1739 );
1740 writeln!(
1741 out,
1742 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1743 inner,
1744 );
1745 writeln!(
1746 out,
1747 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1748 instance, inner,
1749 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001750 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001751 writeln!(out, "}}");
1752 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001753 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001754 writeln!(
1755 out,
David Tolnay0b881402020-12-01 21:05:08 -08001756 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1757 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001758 );
1759 writeln!(
1760 out,
David Tolnay0b881402020-12-01 21:05:08 -08001761 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1762 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001763 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001764 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001765 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001766 writeln!(out, "}}");
1767 }
1768 writeln!(
1769 out,
1770 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1771 instance, inner, inner,
1772 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001773 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001774 writeln!(out, "}}");
1775 writeln!(
1776 out,
1777 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1778 inner, instance, inner,
1779 );
1780 writeln!(out, " return self.get();");
1781 writeln!(out, "}}");
1782 writeln!(
1783 out,
1784 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1785 instance, inner,
1786 );
1787 writeln!(out, " self->~shared_ptr();");
1788 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001789}
1790
David Tolnay215e77f2020-12-28 17:09:48 -08001791fn write_weak_ptr(out: &mut OutFile, ident: &RustName) {
1792 let resolved = out.types.resolve(ident);
1793 let inner = resolved.to_fully_qualified();
1794 let instance = ident.to_symbol(out.types);
1795
1796 out.include.new = true;
1797 out.include.utility = true;
1798
1799 writeln!(
1800 out,
1801 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1802 inner,
1803 );
1804 writeln!(
1805 out,
1806 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1807 inner,
1808 );
1809 writeln!(
1810 out,
1811 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1812 instance, inner,
1813 );
1814 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1815 writeln!(out, "}}");
1816 writeln!(
1817 out,
1818 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1819 instance, inner, inner,
1820 );
1821 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1822 writeln!(out, "}}");
1823 writeln!(
1824 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001825 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1826 instance, inner, inner,
1827 );
1828 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1829 writeln!(out, "}}");
1830 writeln!(
1831 out,
David Tolnay7a487852020-12-28 18:02:25 -08001832 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1833 instance, inner, inner,
1834 );
1835 writeln!(
1836 out,
1837 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1838 inner,
1839 );
1840 writeln!(out, "}}");
1841 writeln!(
1842 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001843 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1844 instance, inner,
1845 );
1846 writeln!(out, " self->~weak_ptr();");
1847 writeln!(out, "}}");
1848}
1849
David Tolnay75ea17c2020-12-06 21:08:34 -08001850fn write_cxx_vector(out: &mut OutFile, element: &RustName) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001851 let inner = element.to_typename(out.types);
1852 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001853
David Tolnay12320e12020-12-09 23:09:36 -08001854 out.include.cstddef = true;
1855
Myron Ahneba35cf2020-02-05 19:41:51 +07001856 writeln!(
1857 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001858 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001859 instance, inner,
1860 );
1861 writeln!(out, " return s.size();");
1862 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001863 writeln!(
1864 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001865 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001866 inner, instance, inner,
1867 );
David Tolnay767e00d2020-12-21 17:12:27 -08001868 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001869 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001870
David Tolnay70820672020-12-07 11:15:14 -08001871 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001872 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001873}