blob: 73a4b8c80437a94884e4278600fb0acb6162913d [file] [log] [blame]
David Tolnayb725d5a2020-12-20 20:27:10 -08001use crate::gen::block::Block;
David Tolnayf7b81fb2020-11-01 22:39:04 -08002use crate::gen::nested::NamespaceEntries;
David Tolnay8810a542020-10-31 21:39:22 -07003use crate::gen::out::OutFile;
David Tolnay942cffd2020-11-03 18:27:10 -08004use crate::gen::{builtin, include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04005use crate::syntax::atom::Atom::{self, *};
David Tolnay3abed472020-12-31 23:34:53 -08006use crate::syntax::instantiate::ImplKey;
David Tolnaye352c1e2020-12-31 16:41:05 -08007use crate::syntax::map::UnorderedMap as Map;
8use crate::syntax::set::UnorderedSet;
David Tolnay891061b2020-04-19 22:42:33 -07009use crate::syntax::symbol::Symbol;
David Tolnay5b1d8632020-12-20 22:05:11 -080010use crate::syntax::trivial::{self, TrivialReason};
Adrian Taylorc8713432020-10-21 18:20:55 -070011use crate::syntax::{
David Tolnay3abed472020-12-31 23:34:53 -080012 derive, mangle, Api, Enum, ExternFn, ExternType, Pair, Signature, Struct, Trait, Type,
13 TypeAlias, Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070014};
David Tolnay7db73692019-10-20 14:51:12 -040015use proc_macro2::Ident;
16
David Tolnayac7188c2020-11-01 16:58:16 -080017pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080018 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040019 let out = &mut out_file;
20
David Tolnaydfb82d72020-11-02 00:10:10 -080021 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070022 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023
David Tolnay7b0e5102020-11-01 23:22:12 -080024 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080025 write_data_structures(out, apis);
26 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080027 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040028
David Tolnay3374d8d2020-10-31 22:18:45 -070029 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070030 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070031
David Tolnayac7188c2020-11-01 16:58:16 -080032 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070033}
34
David Tolnay7b0e5102020-11-01 23:22:12 -080035fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
36 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080037 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080038 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080039 _ => false,
40 };
Adrian Taylorc8713432020-10-21 18:20:55 -070041
David Tolnay7b0e5102020-11-01 23:22:12 -080042 let apis_by_namespace =
43 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
44
David Tolnayd920be52020-11-01 23:34:30 -080045 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080046
David Tolnayd920be52020-11-01 23:34:30 -080047 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080048 let apis = ns_entries.direct_content();
49
50 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080051 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080052 match api {
David Tolnayed6ba4a2021-01-01 14:59:40 -080053 Api::Struct(strct) => write_struct_decl(out, &strct.name),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080054 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080055 Api::CxxType(ety) => write_struct_using(out, &ety.name),
David Tolnayed6ba4a2021-01-01 14:59:40 -080056 Api::RustType(ety) => write_struct_decl(out, &ety.name),
David Tolnay7b0e5102020-11-01 23:22:12 -080057 _ => unreachable!(),
58 }
David Tolnay7db73692019-10-20 14:51:12 -040059 }
David Tolnay7db73692019-10-20 14:51:12 -040060
David Tolnay7b0e5102020-11-01 23:22:12 -080061 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080062 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
63 write(out, nested_ns_entries, indent + 2);
64 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080065 }
Adrian Taylorf9213622020-10-31 22:25:42 -070066 }
67}
68
David Tolnaye1109d92020-11-01 20:51:56 -080069fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnaye352c1e2020-12-31 16:41:05 -080070 let mut methods_for_type = Map::new();
David Tolnay630af882020-10-31 22:03:47 -070071 for api in apis {
David Tolnay102c7ea2020-11-08 18:58:09 -080072 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnayf94bef12020-04-17 14:46:42 -070073 if let Some(receiver) = &efn.sig.receiver {
74 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070075 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070076 .or_insert_with(Vec::new)
77 .push(efn);
78 }
79 }
80 }
Joel Galenson968738f2020-04-15 14:19:33 -070081
David Tolnaye352c1e2020-12-31 16:41:05 -080082 let mut structs_written = UnorderedSet::new();
David Tolnay5439fa12020-11-03 18:45:01 -080083 let mut toposorted_structs = out.types.toposorted_structs.iter();
David Tolnay9622b462020-11-02 21:34:42 -080084 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070085 match api {
David Tolnay5439fa12020-11-03 18:45:01 -080086 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
87 for next in &mut toposorted_structs {
88 if !out.types.cxx.contains(&strct.name.rust) {
89 out.next_section();
David Tolnay102c7ea2020-11-08 18:58:09 -080090 let methods = methods_for_type
91 .get(&strct.name.rust)
92 .map(Vec::as_slice)
93 .unwrap_or_default();
94 write_struct(out, next, methods);
David Tolnay5439fa12020-11-03 18:45:01 -080095 }
96 structs_written.insert(&next.name.rust);
97 if next.name.rust == strct.name.rust {
98 break;
99 }
100 }
101 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700102 Api::Enum(enm) => {
103 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800104 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -0700105 check_enum(out, enm);
106 } else {
107 write_enum(out, enm);
108 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700109 }
David Tolnayc1fe0052020-04-17 15:15:06 -0700110 Api::RustType(ety) => {
David Tolnay8d067de2020-12-26 16:18:23 -0800111 out.next_section();
112 let methods = methods_for_type
113 .get(&ety.name.rust)
114 .map(Vec::as_slice)
115 .unwrap_or_default();
116 write_opaque_type(out, ety, methods);
David Tolnayc1fe0052020-04-17 15:15:06 -0700117 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700118 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400119 }
120 }
121
David Tolnayfabca772020-10-03 21:25:41 -0700122 out.next_section();
123 for api in apis {
124 if let Api::TypeAlias(ety) = api {
Adrian Taylorf831a5a2020-12-07 16:49:19 -0800125 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
David Tolnay5b1d8632020-12-20 22:05:11 -0800126 check_trivial_extern_type(out, ety, reasons)
David Tolnayfabca772020-10-03 21:25:41 -0700127 }
128 }
129 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800130}
131
David Tolnaye1109d92020-11-01 20:51:56 -0800132fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700133 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400134 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800135 match api {
David Tolnayb960ed22020-11-27 14:34:30 -0800136 Api::Struct(strct) => write_struct_operator_decls(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800137 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
David Tolnay04770742020-11-01 13:50:50 -0800138 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
139 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
140 _ => {}
141 }
David Tolnay7db73692019-10-20 14:51:12 -0400142 }
David Tolnay7da38202020-11-27 17:36:16 -0800143
144 write_std_specializations(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400145 }
146
147 for api in apis {
David Tolnayb960ed22020-11-27 14:34:30 -0800148 match api {
149 Api::Struct(strct) => write_struct_operators(out, strct),
David Tolnay358bc4b2020-12-26 22:37:57 -0800150 Api::RustType(ety) => write_opaque_type_layout(out, ety),
David Tolnayb960ed22020-11-27 14:34:30 -0800151 Api::RustFunction(efn) => {
152 out.next_section();
153 write_rust_function_shim(out, efn);
154 }
155 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400156 }
157 }
David Tolnay7db73692019-10-20 14:51:12 -0400158}
159
David Tolnay7da38202020-11-27 17:36:16 -0800160fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
161 out.set_namespace(Default::default());
162 out.begin_block(Block::Namespace("std"));
163
164 for api in apis {
165 if let Api::Struct(strct) = api {
166 if derive::contains(&strct.derives, Trait::Hash) {
167 out.next_section();
David Tolnay12320e12020-12-09 23:09:36 -0800168 out.include.cstddef = true;
David Tolnay08a03db2020-12-09 23:04:36 -0800169 out.include.functional = true;
David Tolnay7da38202020-11-27 17:36:16 -0800170 let qualified = strct.name.to_fully_qualified();
171 writeln!(out, "template <> struct hash<{}> {{", qualified);
172 writeln!(
173 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800174 " ::std::size_t operator()(const {} &self) const noexcept {{",
David Tolnay7da38202020-11-27 17:36:16 -0800175 qualified,
176 );
177 let link_name = mangle::operator(&strct.name, "hash");
178 write!(out, " return ::");
179 for name in &strct.name.namespace {
180 write!(out, "{}::", name);
181 }
182 writeln!(out, "{}(self);", link_name);
183 writeln!(out, " }}");
184 writeln!(out, "}};");
185 }
186 }
187 }
188
189 out.end_block(Block::Namespace("std"));
190}
191
David Tolnaydfb82d72020-11-02 00:10:10 -0800192fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
193 for api in apis {
194 if let Api::Include(include) = api {
195 out.include.insert(include);
196 }
197 }
198
David Tolnaya7c2ea12020-10-30 21:32:53 -0700199 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400200 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700201 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700202 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
203 | Some(I64) => out.include.cstdint = true,
204 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800205 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700206 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800207 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb3873dc2020-11-25 19:47:49 -0800208 Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700209 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800210 Type::RustBox(_) => out.builtin.rust_box = true,
211 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700212 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay215e77f2020-12-28 17:09:48 -0800213 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800214 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700215 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800216 Type::Fn(_) => out.builtin.rust_fn = true,
David Tolnay5515a9e2020-11-25 19:07:54 -0800217 Type::SliceRef(_) => out.builtin.rust_slice = true,
David Tolnaye8b1bb42020-11-24 20:37:37 -0800218 Type::Array(_) => out.include.array = true,
David Tolnay11bd7ff2020-11-01 19:44:58 -0800219 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400220 }
221 }
David Tolnayec66d112020-10-31 21:00:26 -0700222}
David Tolnayf51447e2020-03-06 14:14:27 -0800223
David Tolnay102c7ea2020-11-08 18:58:09 -0800224fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
David Tolnayb960ed22020-11-27 14:34:30 -0800225 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
David Tolnay84389352020-11-27 17:12:10 -0800226 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
David Tolnayb960ed22020-11-27 14:34:30 -0800227
David Tolnay17a934c2020-11-02 00:40:04 -0800228 out.set_namespace(&strct.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800229 let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700230 writeln!(out, "#ifndef {}", guard);
231 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400232 for line in strct.doc.to_string().lines() {
233 writeln!(out, "//{}", line);
234 }
David Tolnay17a934c2020-11-02 00:40:04 -0800235 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay84389352020-11-27 17:12:10 -0800236
David Tolnay7db73692019-10-20 14:51:12 -0400237 for field in &strct.fields {
David Tolnay5573e522020-12-31 11:07:38 -0800238 for line in field.doc.to_string().lines() {
239 writeln!(out, " //{}", line);
240 }
David Tolnay7db73692019-10-20 14:51:12 -0400241 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700242 write_type_space(out, &field.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800243 writeln!(out, "{};", field.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400244 }
David Tolnay84389352020-11-27 17:12:10 -0800245
David Tolnay5554ebd2020-12-10 11:34:41 -0800246 writeln!(out);
David Tolnay84389352020-11-27 17:12:10 -0800247
David Tolnay102c7ea2020-11-08 18:58:09 -0800248 for method in methods {
249 write!(out, " ");
250 let sig = &method.sig;
251 let local_name = method.name.cxx.to_string();
252 write_rust_function_shim_decl(out, &local_name, sig, false);
253 writeln!(out, ";");
254 }
David Tolnay84389352020-11-27 17:12:10 -0800255
David Tolnayb960ed22020-11-27 14:34:30 -0800256 if operator_eq {
257 writeln!(
258 out,
259 " bool operator==(const {} &) const noexcept;",
260 strct.name.cxx,
261 );
262 writeln!(
263 out,
264 " bool operator!=(const {} &) const noexcept;",
265 strct.name.cxx,
266 );
267 }
David Tolnay84389352020-11-27 17:12:10 -0800268
269 if operator_ord {
270 writeln!(
271 out,
272 " bool operator<(const {} &) const noexcept;",
273 strct.name.cxx,
274 );
275 writeln!(
276 out,
277 " bool operator<=(const {} &) const noexcept;",
278 strct.name.cxx,
279 );
280 writeln!(
281 out,
282 " bool operator>(const {} &) const noexcept;",
283 strct.name.cxx,
284 );
285 writeln!(
286 out,
287 " bool operator>=(const {} &) const noexcept;",
288 strct.name.cxx,
289 );
290 }
291
David Tolnay5554ebd2020-12-10 11:34:41 -0800292 out.include.type_traits = true;
293 writeln!(out, " using IsRelocatable = ::std::true_type;");
294
David Tolnay7db73692019-10-20 14:51:12 -0400295 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700296 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400297}
298
David Tolnayed6ba4a2021-01-01 14:59:40 -0800299fn write_struct_decl(out: &mut OutFile, ident: &Pair) {
300 writeln!(out, "struct {};", ident.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400301}
302
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800303fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800304 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800305 write_atom(out, enm.repr);
306 writeln!(out, ";");
307}
308
David Tolnay8faec772020-11-02 00:18:19 -0800309fn write_struct_using(out: &mut OutFile, ident: &Pair) {
310 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800311}
312
David Tolnay8d067de2020-12-26 16:18:23 -0800313fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
David Tolnay17a934c2020-11-02 00:40:04 -0800314 out.set_namespace(&ety.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800315 let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700316 writeln!(out, "#ifndef {}", guard);
317 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700318 for line in ety.doc.to_string().lines() {
319 writeln!(out, "//{}", line);
320 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800321
David Tolnay365fc7c2020-11-25 16:08:13 -0800322 out.builtin.opaque = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800323 writeln!(
David Tolnay7c06b862020-11-25 16:59:09 -0800324 out,
325 "struct {} final : public ::rust::Opaque {{",
326 ety.name.cxx,
327 );
David Tolnay6ba262f2020-12-26 22:23:50 -0800328
Joel Galenson968738f2020-04-15 14:19:33 -0700329 for method in methods {
David Tolnay6ba262f2020-12-26 22:23:50 -0800330 write!(out, " ");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700331 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800332 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700333 write_rust_function_shim_decl(out, &local_name, sig, false);
David Tolnay6ba262f2020-12-26 22:23:50 -0800334 writeln!(out, ";");
David Tolnay8d067de2020-12-26 16:18:23 -0800335 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800336
David Tolnay8d067de2020-12-26 16:18:23 -0800337 if !methods.is_empty() {
338 writeln!(out);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700339 }
David Tolnay6ba262f2020-12-26 22:23:50 -0800340
David Tolnayee6ecfc2020-12-26 21:54:37 -0800341 out.builtin.layout = true;
David Tolnay6ba262f2020-12-26 22:23:50 -0800342 out.include.cstddef = true;
343 writeln!(out, "private:");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800344 writeln!(out, " friend ::rust::layout;");
David Tolnay6ba262f2020-12-26 22:23:50 -0800345 writeln!(out, " struct layout {{");
346 writeln!(out, " static ::std::size_t size() noexcept;");
347 writeln!(out, " static ::std::size_t align() noexcept;");
348 writeln!(out, " }};");
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700349 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700350 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700351}
352
David Tolnay0b9b9f82020-11-01 20:41:00 -0800353fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800354 out.set_namespace(&enm.name.namespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800355 let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700356 writeln!(out, "#ifndef {}", guard);
357 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700358 for line in enm.doc.to_string().lines() {
359 writeln!(out, "//{}", line);
360 }
David Tolnay17a934c2020-11-02 00:40:04 -0800361 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700362 write_atom(out, enm.repr);
363 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700364 for variant in &enm.variants {
David Tolnay4486f722020-12-30 00:01:26 -0800365 for line in variant.doc.to_string().lines() {
366 writeln!(out, " //{}", line);
367 }
David Tolnaye6f62142020-12-21 16:00:41 -0800368 writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700369 }
370 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700371 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700372}
373
David Tolnay0b9b9f82020-11-01 20:41:00 -0800374fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800375 out.set_namespace(&enm.name.namespace);
David Tolnay06711bc2020-11-19 19:25:14 -0800376 out.include.type_traits = true;
377 writeln!(
378 out,
379 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
380 enm.name.cxx,
381 );
David Tolnay17a934c2020-11-02 00:40:04 -0800382 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700383 write_atom(out, enm.repr);
384 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700385 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700386 write!(out, "static_assert(static_cast<");
387 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700388 writeln!(
389 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700390 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnaye6f62142020-12-21 16:00:41 -0800391 enm.name.cxx, variant.name.cxx, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700392 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700393 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700394}
395
David Tolnay5b1d8632020-12-20 22:05:11 -0800396fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
David Tolnay174bd952020-11-02 09:23:12 -0800397 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700398 // necessary for soundness. That's because triviality is always declared by
399 // the user in the form of an unsafe impl of cxx::ExternType:
400 //
401 // unsafe impl ExternType for MyType {
402 // type Id = cxx::type_id!("...");
403 // type Kind = cxx::kind::Trivial;
404 // }
405 //
406 // Since the user went on the record with their unsafe impl to unsafely
407 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800408 // them if that were wrong. However, in practice correctly reasoning about
409 // the relocatability of C++ types is challenging, particularly if the type
410 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700411 //
David Tolnay174bd952020-11-02 09:23:12 -0800412 // There may be legitimate reasons to opt out of this assertion for support
413 // of types that the programmer knows are soundly Rust-movable despite not
414 // being recognized as such by the C++ type system due to a move constructor
415 // or destructor. To opt out of the relocatability check, they need to do
416 // one of the following things in any header used by `include!` in their
417 // bridge.
418 //
419 // --- if they define the type:
420 // struct MyType {
421 // ...
422 // + using IsRelocatable = std::true_type;
423 // };
424 //
425 // --- otherwise:
426 // + template <>
427 // + struct rust::IsRelocatable<MyType> : std::true_type {};
428 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700429
David Tolnay5b1d8632020-12-20 22:05:11 -0800430 let id = alias.name.to_fully_qualified();
David Tolnay174bd952020-11-02 09:23:12 -0800431 out.builtin.relocatable = true;
David Tolnay5b1d8632020-12-20 22:05:11 -0800432 writeln!(out, "static_assert(");
433 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
434 writeln!(
435 out,
436 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
437 id.trim_start_matches("::"),
438 trivial::as_what(&alias.name, reasons),
439 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700440}
441
David Tolnayb960ed22020-11-27 14:34:30 -0800442fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
443 out.set_namespace(&strct.name.namespace);
444 out.begin_block(Block::ExternC);
445
446 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800447 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800448 writeln!(
449 out,
450 "bool {}(const {1} &, const {1} &) noexcept;",
451 link_name, strct.name.cxx,
452 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800453
454 if !derive::contains(&strct.derives, Trait::Eq) {
David Tolnaya05f9402020-11-27 17:44:05 -0800455 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800456 writeln!(
457 out,
458 "bool {}(const {1} &, const {1} &) noexcept;",
459 link_name, strct.name.cxx,
460 );
461 }
David Tolnayb960ed22020-11-27 14:34:30 -0800462 }
463
David Tolnay84389352020-11-27 17:12:10 -0800464 if derive::contains(&strct.derives, Trait::PartialOrd) {
David Tolnaya05f9402020-11-27 17:44:05 -0800465 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800466 writeln!(
467 out,
468 "bool {}(const {1} &, const {1} &) noexcept;",
469 link_name, strct.name.cxx,
470 );
471
David Tolnaya05f9402020-11-27 17:44:05 -0800472 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800473 writeln!(
474 out,
475 "bool {}(const {1} &, const {1} &) noexcept;",
476 link_name, strct.name.cxx,
477 );
478
479 if !derive::contains(&strct.derives, Trait::Ord) {
David Tolnaya05f9402020-11-27 17:44:05 -0800480 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800481 writeln!(
482 out,
483 "bool {}(const {1} &, const {1} &) noexcept;",
484 link_name, strct.name.cxx,
485 );
486
David Tolnaya05f9402020-11-27 17:44:05 -0800487 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800488 writeln!(
489 out,
490 "bool {}(const {1} &, const {1} &) noexcept;",
491 link_name, strct.name.cxx,
492 );
493 }
494 }
495
David Tolnay7da38202020-11-27 17:36:16 -0800496 if derive::contains(&strct.derives, Trait::Hash) {
David Tolnay12320e12020-12-09 23:09:36 -0800497 out.include.cstddef = true;
David Tolnay7da38202020-11-27 17:36:16 -0800498 let link_name = mangle::operator(&strct.name, "hash");
499 writeln!(
500 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800501 "::std::size_t {}(const {} &) noexcept;",
David Tolnay7da38202020-11-27 17:36:16 -0800502 link_name, strct.name.cxx,
503 );
504 }
505
David Tolnayb960ed22020-11-27 14:34:30 -0800506 out.end_block(Block::ExternC);
507}
508
509fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
510 if out.header {
511 return;
512 }
513
514 out.set_namespace(&strct.name.namespace);
515
516 if derive::contains(&strct.derives, Trait::PartialEq) {
David Tolnayb960ed22020-11-27 14:34:30 -0800517 out.next_section();
518 writeln!(
519 out,
520 "bool {0}::operator==(const {0} &rhs) const noexcept {{",
521 strct.name.cxx,
522 );
David Tolnaya05f9402020-11-27 17:44:05 -0800523 let link_name = mangle::operator(&strct.name, "eq");
David Tolnayb960ed22020-11-27 14:34:30 -0800524 writeln!(out, " return {}(*this, rhs);", link_name);
525 writeln!(out, "}}");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800526
David Tolnayb960ed22020-11-27 14:34:30 -0800527 out.next_section();
528 writeln!(
529 out,
530 "bool {0}::operator!=(const {0} &rhs) const noexcept {{",
531 strct.name.cxx,
532 );
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800533 if derive::contains(&strct.derives, Trait::Eq) {
534 writeln!(out, " return !(*this == rhs);");
535 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800536 let link_name = mangle::operator(&strct.name, "ne");
David Tolnaya6f3b6f2020-11-27 16:47:30 -0800537 writeln!(out, " return {}(*this, rhs);", link_name);
538 }
David Tolnayb960ed22020-11-27 14:34:30 -0800539 writeln!(out, "}}");
540 }
David Tolnay84389352020-11-27 17:12:10 -0800541
542 if derive::contains(&strct.derives, Trait::PartialOrd) {
543 out.next_section();
544 writeln!(
545 out,
546 "bool {0}::operator<(const {0} &rhs) const noexcept {{",
547 strct.name.cxx,
548 );
David Tolnaya05f9402020-11-27 17:44:05 -0800549 let link_name = mangle::operator(&strct.name, "lt");
David Tolnay84389352020-11-27 17:12:10 -0800550 writeln!(out, " return {}(*this, rhs);", link_name);
551 writeln!(out, "}}");
552
553 out.next_section();
554 writeln!(
555 out,
556 "bool {0}::operator<=(const {0} &rhs) const noexcept {{",
557 strct.name.cxx,
558 );
David Tolnaya05f9402020-11-27 17:44:05 -0800559 let link_name = mangle::operator(&strct.name, "le");
David Tolnay84389352020-11-27 17:12:10 -0800560 writeln!(out, " return {}(*this, rhs);", link_name);
561 writeln!(out, "}}");
562
563 out.next_section();
564 writeln!(
565 out,
566 "bool {0}::operator>(const {0} &rhs) const noexcept {{",
567 strct.name.cxx,
568 );
569 if derive::contains(&strct.derives, Trait::Ord) {
570 writeln!(out, " return !(*this <= rhs);");
571 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800572 let link_name = mangle::operator(&strct.name, "gt");
David Tolnay84389352020-11-27 17:12:10 -0800573 writeln!(out, " return {}(*this, rhs);", link_name);
574 }
575 writeln!(out, "}}");
576
577 out.next_section();
578 writeln!(
579 out,
580 "bool {0}::operator>=(const {0} &rhs) const noexcept {{",
581 strct.name.cxx,
582 );
583 if derive::contains(&strct.derives, Trait::Ord) {
584 writeln!(out, " return !(*this < rhs);");
585 } else {
David Tolnaya05f9402020-11-27 17:44:05 -0800586 let link_name = mangle::operator(&strct.name, "ge");
David Tolnay84389352020-11-27 17:12:10 -0800587 writeln!(out, " return {}(*this, rhs);", link_name);
588 }
589 writeln!(out, "}}");
590 }
David Tolnayb960ed22020-11-27 14:34:30 -0800591}
592
David Tolnay358bc4b2020-12-26 22:37:57 -0800593fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
594 out.set_namespace(&ety.name.namespace);
595 out.begin_block(Block::ExternC);
596
597 let link_name = mangle::operator(&ety.name, "sizeof");
598 writeln!(out, "::std::size_t {}() noexcept;", link_name);
599
600 let link_name = mangle::operator(&ety.name, "alignof");
601 writeln!(out, "::std::size_t {}() noexcept;", link_name);
602
603 out.end_block(Block::ExternC);
604}
605
606fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
607 if out.header {
608 return;
609 }
610
611 out.set_namespace(&ety.name.namespace);
612
613 out.next_section();
614 let link_name = mangle::operator(&ety.name, "sizeof");
615 writeln!(
616 out,
617 "::std::size_t {}::layout::size() noexcept {{",
618 ety.name.cxx,
619 );
620 writeln!(out, " return {}();", link_name);
621 writeln!(out, "}}");
622
623 out.next_section();
624 let link_name = mangle::operator(&ety.name, "alignof");
625 writeln!(
626 out,
627 "::std::size_t {}::layout::align() noexcept {{",
628 ety.name.cxx,
629 );
630 writeln!(out, " return {}();", link_name);
631 writeln!(out, "}}");
632}
633
David Tolnay1eadd262020-12-29 16:42:08 -0800634fn begin_function_definition(out: &mut OutFile) {
635 if let Some(annotation) = &out.opt.cxx_impl_annotations {
636 write!(out, "{} ", annotation);
637 }
638}
639
David Tolnay0b9b9f82020-11-01 20:41:00 -0800640fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800641 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800642 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800643 out.begin_block(Block::ExternC);
David Tolnay1eadd262020-12-29 16:42:08 -0800644 begin_function_definition(out);
David Tolnayebef4a22020-03-17 15:33:47 -0700645 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700646 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700647 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700648 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700649 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700650 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700651 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700652 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700653 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800654 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700655 write!(out, "const ");
656 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700657 write!(
658 out,
659 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800660 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700661 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700662 }
David Tolnay7db73692019-10-20 14:51:12 -0400663 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700664 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400665 write!(out, ", ");
666 }
David Tolnaya46a2372020-03-06 10:03:48 -0800667 if arg.ty == RustString {
668 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700669 } else if let Type::RustVec(_) = arg.ty {
670 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800671 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700672 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400673 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700674 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400675 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700676 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400677 write!(out, ", ");
678 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700679 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400680 write!(out, "*return$");
681 }
682 writeln!(out, ") noexcept {{");
683 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700684 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700685 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800686 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700687 Some(receiver) => write!(
688 out,
689 "({}::*{}$)(",
David Tolnay1e5fe232021-01-01 18:11:40 -0800690 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800691 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700692 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700693 }
David Tolnay7db73692019-10-20 14:51:12 -0400694 for (i, arg) in efn.args.iter().enumerate() {
695 if i > 0 {
696 write!(out, ", ");
697 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700698 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400699 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700700 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700701 if let Some(receiver) = &efn.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800702 if !receiver.mutable {
David Tolnay4e7123f2020-04-19 21:11:37 -0700703 write!(out, " const");
704 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700705 }
706 write!(out, " = ");
707 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800708 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700709 Some(receiver) => write!(
710 out,
711 "&{}::{}",
David Tolnay1e5fe232021-01-01 18:11:40 -0800712 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800713 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700714 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700715 }
716 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400717 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700718 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700719 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700720 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700721 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700722 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700723 writeln!(out, " [&] {{");
724 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700725 }
David Tolnay7db73692019-10-20 14:51:12 -0400726 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700727 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400728 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700729 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400730 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700731 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400732 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700733 }
734 match &efn.ret {
735 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700736 Some(Type::Str(_)) if !indirect_return => {
737 out.builtin.rust_str_repr = true;
738 write!(out, "::rust::impl<::rust::Str>::repr(");
739 }
David Tolnay73b72642020-11-25 17:44:05 -0800740 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700741 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -0800742 write!(out, "::rust::impl<");
743 write_type(out, ty);
744 write!(out, ">::repr(");
David Tolnayeb952ba2020-04-14 15:02:24 -0700745 }
David Tolnay99642622020-03-25 13:07:35 -0700746 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400747 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700748 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800749 None => write!(out, "{}$(", efn.name.rust),
750 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700751 }
David Tolnay7db73692019-10-20 14:51:12 -0400752 for (i, arg) in efn.args.iter().enumerate() {
753 if i > 0 {
754 write!(out, ", ");
755 }
756 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700757 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800758 write!(out, "::from_raw({})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400759 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700760 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800761 write!(out, "({})", arg.name.cxx);
David Tolnay0356d332020-10-31 19:46:41 -0700762 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700763 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700764 write!(
765 out,
766 "::rust::impl<::rust::Str>::new_unchecked({})",
David Tolnay84ed6ad2021-01-01 15:30:14 -0800767 arg.name.cxx,
David Tolnay0356d332020-10-31 19:46:41 -0700768 );
David Tolnaya46a2372020-03-06 10:03:48 -0800769 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700770 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800771 write!(
772 out,
773 "::rust::String(::rust::unsafe_bitcopy, *{})",
David Tolnay84ed6ad2021-01-01 15:30:14 -0800774 arg.name.cxx,
David Tolnaycc3767f2020-03-06 10:41:51 -0800775 );
David Tolnay313b10e2020-04-25 16:30:51 -0700776 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700777 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700778 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800779 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.name.cxx);
David Tolnay73b72642020-11-25 17:44:05 -0800780 } else if let Type::SliceRef(slice) = &arg.ty {
David Tolnayc5629f02020-11-23 18:32:46 -0800781 write_type(out, &arg.ty);
782 write!(out, "(static_cast<");
783 if slice.mutability.is_none() {
784 write!(out, "const ");
785 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800786 write_type_space(out, &slice.inner);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800787 write!(out, "*>({0}.ptr), {0}.len)", arg.name.cxx);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700788 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700789 out.include.utility = true;
David Tolnay84ed6ad2021-01-01 15:30:14 -0800790 write!(out, "::std::move(*{})", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400791 } else {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800792 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400793 }
794 }
795 write!(out, ")");
796 match &efn.ret {
797 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
798 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -0800799 Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400800 _ => {}
801 }
802 if indirect_return {
803 write!(out, ")");
804 }
805 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700806 if efn.throws {
807 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800808 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700809 writeln!(out, " throw$.ptr = nullptr;");
810 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700811 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700812 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700813 writeln!(
814 out,
David Tolnayc5629f02020-11-23 18:32:46 -0800815 " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
David Tolnayebef4a22020-03-17 15:33:47 -0700816 );
David Tolnay5d121442020-03-17 22:14:40 -0700817 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700818 writeln!(out, " return throw$;");
819 }
David Tolnay7db73692019-10-20 14:51:12 -0400820 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700821 for arg in &efn.args {
822 if let Type::Fn(f) = &arg.ty {
David Tolnay84ed6ad2021-01-01 15:30:14 -0800823 let var = &arg.name;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700824 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700825 }
826 }
David Tolnayca563ee2020-11-01 20:12:27 -0800827 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700828}
829
David Tolnay84ed6ad2021-01-01 15:30:14 -0800830fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700831 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700832 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700833 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700834
835 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700836 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
837 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400838}
839
David Tolnay0b9b9f82020-11-01 20:41:00 -0800840fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800841 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800842 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700843 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700844 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700845 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800846 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700847}
848
849fn write_rust_function_decl_impl(
850 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700851 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700852 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 indirect_call: bool,
854) {
David Tolnay04770742020-11-01 13:50:50 -0800855 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700856 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700857 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700858 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700859 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700860 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700861 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700862 write!(out, "{}(", link_name);
863 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700864 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800865 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700866 write!(out, "const ");
867 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700868 write!(
869 out,
870 "{} &self",
David Tolnay1e5fe232021-01-01 18:11:40 -0800871 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700872 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700873 needs_comma = true;
874 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700875 for arg in &sig.args {
876 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400877 write!(out, ", ");
878 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700879 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700880 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400881 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700882 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700883 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400884 write!(out, ", ");
885 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700886 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400887 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700888 needs_comma = true;
889 }
890 if indirect_call {
891 if needs_comma {
892 write!(out, ", ");
893 }
894 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400895 }
896 writeln!(out, ") noexcept;");
897}
898
David Tolnay0b9b9f82020-11-01 20:41:00 -0800899fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800900 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400901 for line in efn.doc.to_string().lines() {
902 writeln!(out, "//{}", line);
903 }
David Tolnaya73853b2020-04-20 01:19:56 -0700904 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800905 None => efn.name.cxx.to_string(),
David Tolnay1e5fe232021-01-01 18:11:40 -0800906 Some(receiver) => format!(
907 "{}::{}",
908 out.types.resolve(&receiver.ty).name.cxx,
909 efn.name.cxx,
910 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700911 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700912 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700913 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700914 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700915}
916
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700917fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700918 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700919 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700920 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700921 indirect_call: bool,
922) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700923 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700924 write!(out, "{}(", local_name);
925 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400926 if i > 0 {
927 write!(out, ", ");
928 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700929 write_type_space(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800930 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400931 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700932 if indirect_call {
933 if !sig.args.is_empty() {
934 write!(out, ", ");
935 }
936 write!(out, "void *extern$");
937 }
David Tolnay1e548172020-03-16 13:37:09 -0700938 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700939 if let Some(receiver) = &sig.receiver {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800940 if !receiver.mutable {
David Tolnay86710612020-04-20 00:30:32 -0700941 write!(out, " const");
942 }
943 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700944 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700945 write!(out, " noexcept");
946 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700947}
948
949fn write_rust_function_shim_impl(
950 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700951 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700952 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700953 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700954 indirect_call: bool,
955) {
956 if out.header && sig.receiver.is_some() {
957 // We've already defined this inside the struct.
958 return;
959 }
David Tolnayb478fcb2020-12-29 16:48:02 -0800960 if !out.header {
961 begin_function_definition(out);
962 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700963 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400964 if out.header {
965 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700966 return;
David Tolnay7db73692019-10-20 14:51:12 -0400967 }
David Tolnay439cde22020-04-20 00:46:25 -0700968 writeln!(out, " {{");
969 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700970 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700971 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700972 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700973 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700974 write_type(out, &arg.ty);
David Tolnay84ed6ad2021-01-01 15:30:14 -0800975 writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -0700976 }
977 }
978 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700979 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700980 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700981 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700982 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700983 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700984 writeln!(out, "> return$;");
985 write!(out, " ");
986 } else if let Some(ret) = &sig.ret {
987 write!(out, "return ");
988 match ret {
989 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700990 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700991 write!(out, "::from_raw(");
992 }
993 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700994 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700995 write!(out, "(");
996 }
997 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700998 Type::Str(_) => {
999 out.builtin.rust_str_new_unchecked = true;
1000 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
1001 }
David Tolnay73b72642020-11-25 17:44:05 -08001002 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001003 out.builtin.rust_slice_new = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001004 write!(out, "::rust::impl<");
1005 write_type(out, ret);
1006 write!(out, ">::slice(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001007 }
David Tolnay439cde22020-04-20 00:46:25 -07001008 _ => {}
1009 }
1010 }
1011 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -07001012 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001013 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -07001014 }
1015 write!(out, "{}(", invoke);
David Tolnay9d840362020-11-10 08:50:40 -08001016 let mut needs_comma = false;
David Tolnay439cde22020-04-20 00:46:25 -07001017 if sig.receiver.is_some() {
1018 write!(out, "*this");
David Tolnay9d840362020-11-10 08:50:40 -08001019 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001020 }
David Tolnay9d840362020-11-10 08:50:40 -08001021 for arg in &sig.args {
1022 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001023 write!(out, ", ");
1024 }
1025 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -07001026 Type::Str(_) => {
1027 out.builtin.rust_str_repr = true;
1028 write!(out, "::rust::impl<::rust::Str>::repr(");
1029 }
David Tolnay73b72642020-11-25 17:44:05 -08001030 Type::SliceRef(_) => {
David Tolnay36aa9e02020-10-31 23:08:21 -07001031 out.builtin.rust_slice_repr = true;
David Tolnayc5629f02020-11-23 18:32:46 -08001032 write!(out, "::rust::impl<");
1033 write_type(out, &arg.ty);
1034 write!(out, ">::repr(");
David Tolnay36aa9e02020-10-31 23:08:21 -07001035 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001036 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -07001037 _ => {}
1038 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001039 write!(out, "{}", arg.name.cxx);
David Tolnay439cde22020-04-20 00:46:25 -07001040 match &arg.ty {
1041 Type::RustBox(_) => write!(out, ".into_raw()"),
1042 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay73b72642020-11-25 17:44:05 -08001043 Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001044 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -07001045 _ => {}
1046 }
David Tolnay9d840362020-11-10 08:50:40 -08001047 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001048 }
1049 if indirect_return {
David Tolnay9d840362020-11-10 08:50:40 -08001050 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001051 write!(out, ", ");
1052 }
1053 write!(out, "&return$.value");
David Tolnay9d840362020-11-10 08:50:40 -08001054 needs_comma = true;
David Tolnay439cde22020-04-20 00:46:25 -07001055 }
1056 if indirect_call {
David Tolnay9d840362020-11-10 08:50:40 -08001057 if needs_comma {
David Tolnay439cde22020-04-20 00:46:25 -07001058 write!(out, ", ");
1059 }
1060 write!(out, "extern$");
1061 }
1062 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -04001063 if !indirect_return {
1064 if let Some(ret) = &sig.ret {
David Tolnay73b72642020-11-25 17:44:05 -08001065 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -04001066 write!(out, ")");
1067 }
David Tolnay439cde22020-04-20 00:46:25 -07001068 }
1069 }
1070 writeln!(out, ";");
1071 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -07001072 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -07001073 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -07001074 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -07001075 writeln!(out, " }}");
1076 }
1077 if indirect_return {
1078 out.include.utility = true;
1079 writeln!(out, " return ::std::move(return$.value);");
1080 }
1081 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001082}
1083
David Tolnaya7c2ea12020-10-30 21:32:53 -07001084fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001085 match ty {
1086 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -07001087 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001088 }
1089}
1090
David Tolnay75dca2e2020-03-25 20:17:52 -07001091fn indirect_return(sig: &Signature, types: &Types) -> bool {
1092 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -07001093 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -07001094 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -07001095}
1096
David Tolnaya7c2ea12020-10-30 21:32:53 -07001097fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -07001098 match ty {
1099 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001100 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001101 write!(out, "*");
1102 }
1103 Type::Ref(ty) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001104 if !ty.mutable {
David Tolnay99642622020-03-25 13:07:35 -07001105 write!(out, "const ");
1106 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001107 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -07001108 write!(out, " *");
1109 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001110 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -07001111 }
1112}
1113
David Tolnaya7c2ea12020-10-30 21:32:53 -07001114fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1115 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001116 match ty {
1117 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
David Tolnay73b72642020-11-25 17:44:05 -08001118 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -07001119 _ => write_space_after_type(out, ty),
1120 }
1121}
1122
David Tolnaya7c2ea12020-10-30 21:32:53 -07001123fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -04001124 match ty {
1125 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001126 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001127 write!(out, "*");
1128 }
David Tolnay4a441222020-01-25 16:24:27 -08001129 Some(Type::Ref(ty)) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001130 if !ty.mutable {
David Tolnay4a441222020-01-25 16:24:27 -08001131 write!(out, "const ");
1132 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001133 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -08001134 write!(out, " *");
1135 }
David Tolnay73b72642020-11-25 17:44:05 -08001136 Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -07001137 out.builtin.ptr_len = true;
1138 write!(out, "::rust::repr::PtrLen ");
1139 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001140 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
1141 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -04001142 }
1143}
1144
David Tolnaya7c2ea12020-10-30 21:32:53 -07001145fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -04001146 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -07001147 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001148 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001149 write!(out, "*");
1150 }
David Tolnay73b72642020-11-25 17:44:05 -08001151 Type::Str(_) | Type::SliceRef(_) => {
David Tolnay919085c2020-10-31 22:32:22 -07001152 out.builtin.ptr_len = true;
1153 write!(out, "::rust::repr::PtrLen ");
1154 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001155 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -04001156 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001157 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -04001158 write!(out, "*");
1159 }
David Tolnay84ed6ad2021-01-01 15:30:14 -08001160 write!(out, "{}", arg.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -04001161}
1162
David Tolnaya7c2ea12020-10-30 21:32:53 -07001163fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001164 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001165 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -07001166 Some(atom) => write_atom(out, atom),
David Tolnay1e5fe232021-01-01 18:11:40 -08001167 None => write!(
1168 out,
1169 "{}",
1170 out.types.resolve(ident).name.to_fully_qualified(),
1171 ),
David Tolnay7db73692019-10-20 14:51:12 -04001172 },
1173 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -08001174 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001175 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001176 write!(out, ">");
1177 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001178 Type::RustVec(ty) => {
1179 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001180 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001181 write!(out, ">");
1182 }
David Tolnay7db73692019-10-20 14:51:12 -04001183 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001184 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001185 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001186 write!(out, ">");
1187 }
David Tolnayb3b24a12020-12-01 15:27:43 -08001188 Type::SharedPtr(ptr) => {
1189 write!(out, "::std::shared_ptr<");
1190 write_type(out, &ptr.inner);
1191 write!(out, ">");
1192 }
David Tolnay215e77f2020-12-28 17:09:48 -08001193 Type::WeakPtr(ptr) => {
1194 write!(out, "::std::weak_ptr<");
1195 write_type(out, &ptr.inner);
1196 write!(out, ">");
1197 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001198 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001199 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001200 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001201 write!(out, ">");
1202 }
David Tolnay7db73692019-10-20 14:51:12 -04001203 Type::Ref(r) => {
David Tolnay9c4ac2e2020-11-15 21:14:03 -08001204 if !r.mutable {
David Tolnay7db73692019-10-20 14:51:12 -04001205 write!(out, "const ");
1206 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001207 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001208 write!(out, " &");
1209 }
1210 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001211 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001212 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001213 Type::SliceRef(slice) => {
David Tolnayc5629f02020-11-23 18:32:46 -08001214 write!(out, "::rust::Slice<");
David Tolnay5515a9e2020-11-25 19:07:54 -08001215 if slice.mutability.is_none() {
David Tolnayc5629f02020-11-23 18:32:46 -08001216 write!(out, "const ");
1217 }
David Tolnay5515a9e2020-11-25 19:07:54 -08001218 write_type(out, &slice.inner);
1219 write!(out, ">");
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001220 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001221 Type::Fn(f) => {
David Tolnayf031c322020-11-29 19:41:33 -08001222 write!(out, "::rust::Fn<");
David Tolnay75dca2e2020-03-25 20:17:52 -07001223 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001224 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001225 None => write!(out, "void"),
1226 }
1227 write!(out, "(");
1228 for (i, arg) in f.args.iter().enumerate() {
1229 if i > 0 {
1230 write!(out, ", ");
1231 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001232 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001233 }
1234 write!(out, ")>");
1235 }
Xiangpeng Hao78762352020-11-12 10:24:18 +08001236 Type::Array(a) => {
1237 write!(out, "::std::array<");
1238 write_type(out, &a.inner);
1239 write!(out, ", {}>", &a.len);
1240 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001241 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001242 }
1243}
1244
David Tolnayf6a89f22020-05-10 23:39:27 -07001245fn write_atom(out: &mut OutFile, atom: Atom) {
1246 match atom {
1247 Bool => write!(out, "bool"),
David Tolnayb3873dc2020-11-25 19:47:49 -08001248 Char => write!(out, "char"),
David Tolnaybe3cbf72020-12-12 22:12:07 -08001249 U8 => write!(out, "::std::uint8_t"),
1250 U16 => write!(out, "::std::uint16_t"),
1251 U32 => write!(out, "::std::uint32_t"),
1252 U64 => write!(out, "::std::uint64_t"),
1253 Usize => write!(out, "::std::size_t"),
1254 I8 => write!(out, "::std::int8_t"),
1255 I16 => write!(out, "::std::int16_t"),
1256 I32 => write!(out, "::std::int32_t"),
1257 I64 => write!(out, "::std::int64_t"),
David Tolnayf6a89f22020-05-10 23:39:27 -07001258 Isize => write!(out, "::rust::isize"),
1259 F32 => write!(out, "float"),
1260 F64 => write!(out, "double"),
1261 CxxString => write!(out, "::std::string"),
1262 RustString => write!(out, "::rust::String"),
1263 }
1264}
1265
David Tolnaya7c2ea12020-10-30 21:32:53 -07001266fn write_type_space(out: &mut OutFile, ty: &Type) {
1267 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001268 write_space_after_type(out, ty);
1269}
1270
1271fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001272 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001273 Type::Ident(_)
1274 | Type::RustBox(_)
1275 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -08001276 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -08001277 | Type::WeakPtr(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001278 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001279 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001280 | Type::RustVec(_)
David Tolnay73b72642020-11-25 17:44:05 -08001281 | Type::SliceRef(_)
Xiangpeng Hao78762352020-11-12 10:24:18 +08001282 | Type::Fn(_)
1283 | Type::Array(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001284 Type::Ref(_) => {}
David Tolnaye0dca7b2020-11-25 17:18:57 -08001285 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001286 }
1287}
1288
David Tolnay1bdb4712020-11-25 07:27:54 -08001289#[derive(Copy, Clone)]
1290enum UniquePtr<'a> {
David Tolnay3abed472020-12-31 23:34:53 -08001291 Ident(&'a Ident),
1292 CxxVector(&'a Ident),
David Tolnay1bdb4712020-11-25 07:27:54 -08001293}
1294
1295trait ToTypename {
1296 fn to_typename(&self, types: &Types) -> String;
1297}
1298
David Tolnay3abed472020-12-31 23:34:53 -08001299impl ToTypename for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001300 fn to_typename(&self, types: &Types) -> String {
David Tolnay1e5fe232021-01-01 18:11:40 -08001301 types.resolve(self).name.to_fully_qualified()
David Tolnay2eca4a02020-04-24 19:50:51 -07001302 }
1303}
1304
David Tolnay1bdb4712020-11-25 07:27:54 -08001305impl<'a> ToTypename for UniquePtr<'a> {
1306 fn to_typename(&self, types: &Types) -> String {
1307 match self {
1308 UniquePtr::Ident(ident) => ident.to_typename(types),
1309 UniquePtr::CxxVector(element) => {
1310 format!("::std::vector<{}>", element.to_typename(types))
1311 }
1312 }
1313 }
1314}
1315
1316trait ToMangled {
1317 fn to_mangled(&self, types: &Types) -> Symbol;
1318}
1319
David Tolnay3abed472020-12-31 23:34:53 -08001320impl ToMangled for Ident {
David Tolnay1bdb4712020-11-25 07:27:54 -08001321 fn to_mangled(&self, types: &Types) -> Symbol {
David Tolnay1e5fe232021-01-01 18:11:40 -08001322 types.resolve(self).name.to_symbol()
David Tolnay1bdb4712020-11-25 07:27:54 -08001323 }
1324}
1325
1326impl<'a> ToMangled for UniquePtr<'a> {
1327 fn to_mangled(&self, types: &Types) -> Symbol {
1328 match self {
1329 UniquePtr::Ident(ident) => ident.to_mangled(types),
1330 UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"),
1331 }
David Tolnaybae50ef2020-04-25 12:38:41 -07001332 }
1333}
1334
David Tolnaya7c2ea12020-10-30 21:32:53 -07001335fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -08001336 if out.header {
1337 return;
1338 }
1339
1340 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -08001341 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -08001342 out.begin_block(Block::ExternC);
David Tolnay3abed472020-12-31 23:34:53 -08001343 for impl_key in out.types.impls.keys() {
1344 out.next_section();
1345 match impl_key {
1346 ImplKey::RustBox(ident) => write_rust_box_extern(out, ident),
1347 ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident),
1348 ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident),
1349 ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident),
1350 ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident),
1351 ImplKey::CxxVector(ident) => write_cxx_vector(out, ident),
David Tolnay7db73692019-10-20 14:51:12 -04001352 }
1353 }
David Tolnay0c033e32020-11-01 15:15:48 -08001354 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -04001355
David Tolnay0c033e32020-11-01 15:15:48 -08001356 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -08001357 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3abed472020-12-31 23:34:53 -08001358 for impl_key in out.types.impls.keys() {
1359 match impl_key {
1360 ImplKey::RustBox(ident) => write_rust_box_impl(out, ident),
1361 ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident),
1362 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -04001363 }
1364 }
David Tolnay0f0162f2020-11-16 23:43:37 -08001365 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay0c033e32020-11-01 15:15:48 -08001366 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001367}
1368
David Tolnay3abed472020-12-31 23:34:53 -08001369fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1370 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001371 let inner = resolve.name.to_fully_qualified();
1372 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001373
David Tolnay7db73692019-10-20 14:51:12 -04001374 writeln!(
1375 out,
David Tolnayc4b34222020-12-12 13:06:26 -08001376 "{} *cxxbridge1$box${}$alloc() noexcept;",
1377 inner, instance,
David Tolnay7db73692019-10-20 14:51:12 -04001378 );
1379 writeln!(
1380 out,
David Tolnay25b3c1d2020-12-12 15:50:10 -08001381 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1382 instance, inner,
1383 );
1384 writeln!(
1385 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001386 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001387 instance, inner,
1388 );
David Tolnay7db73692019-10-20 14:51:12 -04001389}
1390
David Tolnay3abed472020-12-31 23:34:53 -08001391fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001392 let inner = element.to_typename(out.types);
1393 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001394
David Tolnay12320e12020-12-09 23:09:36 -08001395 out.include.cstddef = true;
1396
Myron Ahneba35cf2020-02-05 19:41:51 +07001397 writeln!(
1398 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001399 "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001400 instance, inner,
1401 );
1402 writeln!(
1403 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001404 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001405 instance, inner,
1406 );
1407 writeln!(
1408 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001409 "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001410 instance, inner,
1411 );
David Tolnay219c0792020-04-24 20:31:37 -07001412 writeln!(
1413 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001414 "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnaydc62d712020-12-11 13:51:53 -08001415 instance, inner,
1416 );
1417 writeln!(
1418 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001419 "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001420 inner, instance,
1421 );
David Tolnay503d0192020-04-24 22:18:56 -07001422 writeln!(
1423 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001424 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001425 instance, inner,
1426 );
1427 writeln!(
1428 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001429 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001430 instance, inner,
1431 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001432}
1433
David Tolnay3abed472020-12-31 23:34:53 -08001434fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1435 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001436 let inner = resolve.name.to_fully_qualified();
1437 let instance = resolve.name.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001438
1439 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001440 begin_function_definition(out);
David Tolnaye5703162020-12-12 16:26:35 -08001441 writeln!(
1442 out,
1443 "{} *Box<{}>::allocation::alloc() noexcept {{",
1444 inner, inner,
1445 );
David Tolnayc4b34222020-12-12 13:06:26 -08001446 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001447 writeln!(out, "}}");
1448
1449 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001450 begin_function_definition(out);
David Tolnay25b3c1d2020-12-12 15:50:10 -08001451 writeln!(
1452 out,
David Tolnaye5703162020-12-12 16:26:35 -08001453 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
David Tolnay25b3c1d2020-12-12 15:50:10 -08001454 inner, inner,
1455 );
1456 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1457 writeln!(out, "}}");
1458
1459 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001460 begin_function_definition(out);
David Tolnay324437a2020-03-01 13:02:24 -08001461 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001462 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001463 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001464}
1465
David Tolnay3abed472020-12-31 23:34:53 -08001466fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001467 let inner = element.to_typename(out.types);
1468 let instance = element.to_mangled(out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001469
David Tolnay12320e12020-12-09 23:09:36 -08001470 out.include.cstddef = true;
1471
Myron Ahneba35cf2020-02-05 19:41:51 +07001472 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001473 begin_function_definition(out);
David Tolnayf97c2d52020-04-25 16:37:48 -07001474 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001475 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001476 writeln!(out, "}}");
1477
1478 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001479 begin_function_definition(out);
Myron Ahneba35cf2020-02-05 19:41:51 +07001480 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001481 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001482 writeln!(out, "}}");
1483
1484 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001485 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001486 writeln!(
1487 out,
1488 "::std::size_t Vec<{}>::size() const noexcept {{",
1489 inner,
1490 );
David Tolnay0f0162f2020-11-16 23:43:37 -08001491 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001492 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001493
1494 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001495 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001496 writeln!(
1497 out,
1498 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1499 inner,
1500 );
David Tolnay381d9532020-12-11 13:59:45 -08001501 writeln!(
1502 out,
1503 " return cxxbridge1$rust_vec${}$capacity(this);",
1504 instance,
1505 );
David Tolnaydc62d712020-12-11 13:51:53 -08001506 writeln!(out, "}}");
1507
1508 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001509 begin_function_definition(out);
David Tolnay219c0792020-04-24 20:31:37 -07001510 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
David Tolnay0f0162f2020-11-16 23:43:37 -08001511 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
David Tolnay219c0792020-04-24 20:31:37 -07001512 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001513
1514 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001515 begin_function_definition(out);
David Tolnayfb6b73c2020-11-10 14:32:16 -08001516 writeln!(
1517 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001518 "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001519 inner,
1520 );
1521 writeln!(
1522 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001523 " return cxxbridge1$rust_vec${}$reserve_total(this, cap);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001524 instance,
1525 );
1526 writeln!(out, "}}");
1527
1528 writeln!(out, "template <>");
David Tolnayb478fcb2020-12-29 16:48:02 -08001529 begin_function_definition(out);
David Tolnaybe3cbf72020-12-12 22:12:07 -08001530 writeln!(
1531 out,
1532 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1533 inner,
1534 );
David Tolnayfb6b73c2020-11-10 14:32:16 -08001535 writeln!(
1536 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001537 " return cxxbridge1$rust_vec${}$set_len(this, len);",
David Tolnayfb6b73c2020-11-10 14:32:16 -08001538 instance,
1539 );
1540 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001541}
1542
David Tolnay3abed472020-12-31 23:34:53 -08001543fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001544 let ty = UniquePtr::Ident(ident);
David Tolnay1bdb4712020-11-25 07:27:54 -08001545 write_unique_ptr_common(out, ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001546}
1547
1548// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnay1bdb4712020-11-25 07:27:54 -08001549fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001550 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001551 out.include.utility = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001552 let inner = ty.to_typename(out.types);
1553 let instance = ty.to_mangled(out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001554
David Tolnay63da4d32020-04-25 09:41:12 -07001555 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001556 // Some aliases are to opaque types; some are to trivial types. We can't
1557 // know at code generation time, so we generate both C++ and Rust side
1558 // bindings for a "new" method anyway. But the Rust code can't be called
1559 // for Opaque types because the 'new' method is not implemented.
David Tolnay1bdb4712020-11-25 07:27:54 -08001560 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001561 out.types.structs.contains_key(ident)
1562 || out.types.enums.contains_key(ident)
1563 || out.types.aliases.contains_key(ident)
David Tolnayca0f9da2020-10-16 13:16:17 -07001564 }
David Tolnay1bdb4712020-11-25 07:27:54 -08001565 UniquePtr::CxxVector(_) => false,
David Tolnay63da4d32020-04-25 09:41:12 -07001566 };
1567
David Tolnay53462762020-11-25 07:08:10 -08001568 let conditional_delete = match ty {
1569 UniquePtr::Ident(ident) => {
David Tolnay3abed472020-12-31 23:34:53 -08001570 !out.types.structs.contains_key(ident) && !out.types.enums.contains_key(ident)
David Tolnay53462762020-11-25 07:08:10 -08001571 }
1572 UniquePtr::CxxVector(_) => false,
1573 };
1574
1575 if conditional_delete {
1576 out.builtin.is_complete = true;
1577 let definition = match ty {
David Tolnay1e5fe232021-01-01 18:11:40 -08001578 UniquePtr::Ident(ty) => &out.types.resolve(ty).name.cxx,
David Tolnay53462762020-11-25 07:08:10 -08001579 UniquePtr::CxxVector(_) => unreachable!(),
1580 };
1581 writeln!(
1582 out,
David Tolnay75068632020-12-26 22:15:17 -08001583 "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");",
David Tolnay53462762020-11-25 07:08:10 -08001584 inner, definition,
1585 );
1586 }
David Tolnay7db73692019-10-20 14:51:12 -04001587 writeln!(
1588 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001589 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001590 inner,
1591 );
1592 writeln!(
1593 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001594 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001595 inner,
1596 );
1597 writeln!(
1598 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001599 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001600 instance, inner,
1601 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001602 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001603 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001604 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001605 out.builtin.maybe_uninit = true;
David Tolnay63da4d32020-04-25 09:41:12 -07001606 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001607 out,
David Tolnay0b881402020-12-01 21:05:08 -08001608 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1609 inner, instance, inner,
David Tolnay53838912020-04-09 20:56:44 -07001610 );
David Tolnay63da4d32020-04-25 09:41:12 -07001611 writeln!(
1612 out,
David Tolnay0b881402020-12-01 21:05:08 -08001613 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1614 inner, inner, inner,
David Tolnay63da4d32020-04-25 09:41:12 -07001615 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001616 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001617 writeln!(out, " return uninit;");
David Tolnay63da4d32020-04-25 09:41:12 -07001618 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001619 }
David Tolnay7db73692019-10-20 14:51:12 -04001620 writeln!(
1621 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001622 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001623 instance, inner, inner,
1624 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001625 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001626 writeln!(out, "}}");
1627 writeln!(
1628 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001629 "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001630 inner, instance, inner,
1631 );
1632 writeln!(out, " return ptr.get();");
1633 writeln!(out, "}}");
1634 writeln!(
1635 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001636 "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001637 inner, instance, inner,
1638 );
1639 writeln!(out, " return ptr.release();");
1640 writeln!(out, "}}");
1641 writeln!(
1642 out,
David Tolnay0f0162f2020-11-16 23:43:37 -08001643 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001644 instance, inner,
1645 );
David Tolnay53462762020-11-25 07:08:10 -08001646 if conditional_delete {
1647 out.builtin.deleter_if = true;
1648 writeln!(
1649 out,
David Tolnay75068632020-12-26 22:15:17 -08001650 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
David Tolnay53462762020-11-25 07:08:10 -08001651 inner,
1652 );
1653 } else {
1654 writeln!(out, " ptr->~unique_ptr();");
1655 }
David Tolnay7db73692019-10-20 14:51:12 -04001656 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001657}
Myron Ahneba35cf2020-02-05 19:41:51 +07001658
David Tolnay3abed472020-12-31 23:34:53 -08001659fn write_shared_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001660 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001661 let inner = resolve.name.to_fully_qualified();
1662 let instance = resolve.name.to_symbol();
David Tolnayb3b24a12020-12-01 15:27:43 -08001663
David Tolnayb3b24a12020-12-01 15:27:43 -08001664 out.include.new = true;
1665 out.include.utility = true;
1666
1667 // Some aliases are to opaque types; some are to trivial types. We can't
1668 // know at code generation time, so we generate both C++ and Rust side
1669 // bindings for a "new" method anyway. But the Rust code can't be called for
1670 // Opaque types because the 'new' method is not implemented.
David Tolnay3abed472020-12-31 23:34:53 -08001671 let can_construct_from_value = out.types.structs.contains_key(ident)
1672 || out.types.enums.contains_key(ident)
1673 || out.types.aliases.contains_key(ident);
David Tolnayb3b24a12020-12-01 15:27:43 -08001674
David Tolnayb3b24a12020-12-01 15:27:43 -08001675 writeln!(
1676 out,
1677 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1678 inner,
1679 );
1680 writeln!(
1681 out,
1682 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1683 inner,
1684 );
1685 writeln!(
1686 out,
1687 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1688 instance, inner,
1689 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001690 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001691 writeln!(out, "}}");
1692 if can_construct_from_value {
David Tolnay0b881402020-12-01 21:05:08 -08001693 out.builtin.maybe_uninit = true;
David Tolnayb3b24a12020-12-01 15:27:43 -08001694 writeln!(
1695 out,
David Tolnay0b881402020-12-01 21:05:08 -08001696 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1697 inner, instance, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001698 );
1699 writeln!(
1700 out,
David Tolnay0b881402020-12-01 21:05:08 -08001701 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1702 inner, inner, inner,
David Tolnayb3b24a12020-12-01 15:27:43 -08001703 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001704 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
David Tolnay0b881402020-12-01 21:05:08 -08001705 writeln!(out, " return uninit;");
David Tolnayb3b24a12020-12-01 15:27:43 -08001706 writeln!(out, "}}");
1707 }
1708 writeln!(
1709 out,
1710 "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{",
1711 instance, inner, inner,
1712 );
David Tolnay718ca0f2020-12-09 23:01:11 -08001713 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
David Tolnayb3b24a12020-12-01 15:27:43 -08001714 writeln!(out, "}}");
1715 writeln!(
1716 out,
1717 "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{",
1718 inner, instance, inner,
1719 );
1720 writeln!(out, " return self.get();");
1721 writeln!(out, "}}");
1722 writeln!(
1723 out,
1724 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
1725 instance, inner,
1726 );
1727 writeln!(out, " self->~shared_ptr();");
1728 writeln!(out, "}}");
David Tolnayb3b24a12020-12-01 15:27:43 -08001729}
1730
David Tolnay3abed472020-12-31 23:34:53 -08001731fn write_weak_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay95bc57f2021-01-01 13:29:44 -08001732 let resolve = out.types.resolve(ident);
David Tolnay1e5fe232021-01-01 18:11:40 -08001733 let inner = resolve.name.to_fully_qualified();
1734 let instance = resolve.name.to_symbol();
David Tolnay215e77f2020-12-28 17:09:48 -08001735
1736 out.include.new = true;
1737 out.include.utility = true;
1738
1739 writeln!(
1740 out,
1741 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
1742 inner,
1743 );
1744 writeln!(
1745 out,
1746 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
1747 inner,
1748 );
1749 writeln!(
1750 out,
1751 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
1752 instance, inner,
1753 );
1754 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
1755 writeln!(out, "}}");
1756 writeln!(
1757 out,
1758 "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{",
1759 instance, inner, inner,
1760 );
1761 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
1762 writeln!(out, "}}");
1763 writeln!(
1764 out,
David Tolnay85b6bc42020-12-28 17:47:51 -08001765 "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{",
1766 instance, inner, inner,
1767 );
1768 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
1769 writeln!(out, "}}");
1770 writeln!(
1771 out,
David Tolnay7a487852020-12-28 18:02:25 -08001772 "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{",
1773 instance, inner, inner,
1774 );
1775 writeln!(
1776 out,
1777 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
1778 inner,
1779 );
1780 writeln!(out, "}}");
1781 writeln!(
1782 out,
David Tolnay215e77f2020-12-28 17:09:48 -08001783 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
1784 instance, inner,
1785 );
1786 writeln!(out, " self->~weak_ptr();");
1787 writeln!(out, "}}");
1788}
1789
David Tolnay3abed472020-12-31 23:34:53 -08001790fn write_cxx_vector(out: &mut OutFile, element: &Ident) {
David Tolnay1bdb4712020-11-25 07:27:54 -08001791 let inner = element.to_typename(out.types);
1792 let instance = element.to_mangled(out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001793
David Tolnay12320e12020-12-09 23:09:36 -08001794 out.include.cstddef = true;
1795
Myron Ahneba35cf2020-02-05 19:41:51 +07001796 writeln!(
1797 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -08001798 "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001799 instance, inner,
1800 );
1801 writeln!(out, " return s.size();");
1802 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001803 writeln!(
1804 out,
David Tolnay767e00d2020-12-21 17:12:27 -08001805 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001806 inner, instance, inner,
1807 );
David Tolnay767e00d2020-12-21 17:12:27 -08001808 writeln!(out, " return &(*s)[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001809 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001810
David Tolnay70820672020-12-07 11:15:14 -08001811 out.include.memory = true;
David Tolnay1bdb4712020-11-25 07:27:54 -08001812 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
Myron Ahneba35cf2020-02-05 19:41:51 +07001813}