blob: 414ea4670a4ba3ac9b471278bc2de80da7bef74f [file] [log] [blame]
David Tolnay0c033e32020-11-01 15:15:48 -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 Tolnay58cd0c72020-11-01 23:55:21 -08004use crate::gen::{builtin, include, toposort, 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;
Adrian Taylorc8713432020-10-21 18:20:55 -07007use crate::syntax::{
David Tolnay8faec772020-11-02 00:18:19 -08008 mangle, Api, Enum, ExternFn, ExternType, Pair, ResolvableName, Signature, Struct, Type, Types,
9 Var,
Adrian Taylorc8713432020-10-21 18:20:55 -070010};
David Tolnay7db73692019-10-20 14:51:12 -040011use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -070012use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040013
David Tolnayac7188c2020-11-01 16:58:16 -080014pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
David Tolnaye1476af2020-11-01 13:47:25 -080015 let mut out_file = OutFile::new(header, opt, types);
David Tolnay7db73692019-10-20 14:51:12 -040016 let out = &mut out_file;
17
David Tolnaydfb82d72020-11-02 00:10:10 -080018 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070019 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040020
David Tolnay7b0e5102020-11-01 23:22:12 -080021 write_forward_declarations(out, apis);
David Tolnaye1109d92020-11-01 20:51:56 -080022 write_data_structures(out, apis);
23 write_functions(out, apis);
David Tolnay169bb472020-11-01 21:04:24 -080024 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040025
David Tolnay3374d8d2020-10-31 22:18:45 -070026 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070027 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070028
David Tolnayac7188c2020-11-01 16:58:16 -080029 out_file.content()
Adrian Taylorc8713432020-10-21 18:20:55 -070030}
31
David Tolnay7b0e5102020-11-01 23:22:12 -080032fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
33 let needs_forward_declaration = |api: &&Api| match api {
David Tolnay4bfca112020-11-01 23:59:11 -080034 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
David Tolnay17a934c2020-11-02 00:40:04 -080035 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
David Tolnay7b0e5102020-11-01 23:22:12 -080036 _ => false,
37 };
Adrian Taylorc8713432020-10-21 18:20:55 -070038
David Tolnay7b0e5102020-11-01 23:22:12 -080039 let apis_by_namespace =
40 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
41
David Tolnayd920be52020-11-01 23:34:30 -080042 write(out, &apis_by_namespace, 0);
David Tolnay7b0e5102020-11-01 23:22:12 -080043
David Tolnayd920be52020-11-01 23:34:30 -080044 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
David Tolnay7b0e5102020-11-01 23:22:12 -080045 let apis = ns_entries.direct_content();
46
47 for api in apis {
David Tolnayd920be52020-11-01 23:34:30 -080048 write!(out, "{:1$}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080049 match api {
David Tolnay17a934c2020-11-02 00:40:04 -080050 Api::Struct(strct) => write_struct_decl(out, &strct.name.cxx),
David Tolnay0e3ee8e2020-11-01 23:46:52 -080051 Api::Enum(enm) => write_enum_decl(out, enm),
David Tolnay17a934c2020-11-02 00:40:04 -080052 Api::CxxType(ety) => write_struct_using(out, &ety.name),
53 Api::RustType(ety) => write_struct_decl(out, &ety.name.cxx),
David Tolnay7b0e5102020-11-01 23:22:12 -080054 _ => unreachable!(),
55 }
David Tolnay7db73692019-10-20 14:51:12 -040056 }
David Tolnay7db73692019-10-20 14:51:12 -040057
David Tolnay7b0e5102020-11-01 23:22:12 -080058 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
David Tolnayd920be52020-11-01 23:34:30 -080059 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
60 write(out, nested_ns_entries, indent + 2);
61 writeln!(out, "{:1$}}}", "", indent);
David Tolnay7b0e5102020-11-01 23:22:12 -080062 }
Adrian Taylorf9213622020-10-31 22:25:42 -070063 }
64}
65
David Tolnaye1109d92020-11-01 20:51:56 -080066fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayf94bef12020-04-17 14:46:42 -070067 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070068 for api in apis {
David Tolnayf94bef12020-04-17 14:46:42 -070069 if let Api::RustFunction(efn) = api {
70 if let Some(receiver) = &efn.sig.receiver {
71 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070072 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070073 .or_insert_with(Vec::new)
74 .push(efn);
75 }
76 }
77 }
Joel Galenson968738f2020-04-15 14:19:33 -070078
David Tolnay9622b462020-11-02 21:34:42 -080079 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070080 match api {
Joel Galensonc03402a2020-04-23 17:31:09 -070081 Api::Enum(enm) => {
82 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -080083 if out.types.cxx.contains(&enm.name.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070084 check_enum(out, enm);
85 } else {
86 write_enum(out, enm);
87 }
Joel Galensonc03402a2020-04-23 17:31:09 -070088 }
David Tolnayc1fe0052020-04-17 15:15:06 -070089 Api::RustType(ety) => {
David Tolnay17a934c2020-11-02 00:40:04 -080090 if let Some(methods) = methods_for_type.get(&ety.name.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -070091 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070092 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070093 }
David Tolnayc1fe0052020-04-17 15:15:06 -070094 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070095 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040096 }
97 }
98
David Tolnay9622b462020-11-02 21:34:42 -080099 for strct in toposort::sort(apis, out.types) {
100 out.next_section();
101 if !out.types.cxx.contains(&strct.name.rust) {
102 write_struct(out, strct);
103 }
104 }
105
David Tolnayfabca772020-10-03 21:25:41 -0700106 out.next_section();
107 for api in apis {
108 if let Api::TypeAlias(ety) = api {
David Tolnay17a934c2020-11-02 00:40:04 -0800109 if out.types.required_trivial.contains_key(&ety.name.rust) {
110 check_trivial_extern_type(out, &ety.name)
David Tolnayfabca772020-10-03 21:25:41 -0700111 }
112 }
113 }
David Tolnay1b0339c2020-11-01 20:37:50 -0800114}
115
David Tolnaye1109d92020-11-01 20:51:56 -0800116fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
David Tolnayce5a91f2020-10-31 22:42:08 -0700117 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400118 for api in apis {
David Tolnay04770742020-11-01 13:50:50 -0800119 match api {
120 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
121 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
122 _ => {}
123 }
David Tolnay7db73692019-10-20 14:51:12 -0400124 }
David Tolnay7db73692019-10-20 14:51:12 -0400125 }
126
127 for api in apis {
128 if let Api::RustFunction(efn) = api {
129 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700130 write_rust_function_shim(out, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400131 }
132 }
David Tolnay7db73692019-10-20 14:51:12 -0400133}
134
David Tolnaydfb82d72020-11-02 00:10:10 -0800135fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
136 for api in apis {
137 if let Api::Include(include) = api {
138 out.include.insert(include);
139 }
140 }
141
David Tolnaya7c2ea12020-10-30 21:32:53 -0700142 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400143 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700144 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700145 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
146 | Some(I64) => out.include.cstdint = true,
147 Some(Usize) => out.include.cstddef = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800148 Some(Isize) => out.builtin.rust_isize = true,
David Tolnay89e386d2020-10-03 19:02:19 -0700149 Some(CxxString) => out.include.string = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800150 Some(RustString) => out.builtin.rust_string = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700151 Some(Bool) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700152 },
David Tolnaydcfa8e92020-11-02 09:50:06 -0800153 Type::RustBox(_) => out.builtin.rust_box = true,
154 Type::RustVec(_) => out.builtin.rust_vec = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700155 Type::UniquePtr(_) => out.include.memory = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800156 Type::Str(_) => out.builtin.rust_str = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700157 Type::CxxVector(_) => out.include.vector = true,
David Tolnaydcfa8e92020-11-02 09:50:06 -0800158 Type::Fn(_) => out.builtin.rust_fn = true,
159 Type::Slice(_) => out.builtin.rust_slice = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700160 Type::SliceRefU8(_) => {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700161 out.include.cstdint = true;
David Tolnayb9da1462020-10-31 21:03:41 -0700162 out.builtin.rust_slice = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700163 }
David Tolnay11bd7ff2020-11-01 19:44:58 -0800164 Type::Ref(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -0400165 }
166 }
David Tolnayec66d112020-10-31 21:00:26 -0700167}
David Tolnayf51447e2020-03-06 14:14:27 -0800168
David Tolnay0b9b9f82020-11-01 20:41:00 -0800169fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
David Tolnay17a934c2020-11-02 00:40:04 -0800170 out.set_namespace(&strct.name.namespace);
171 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700172 writeln!(out, "#ifndef {}", guard);
173 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400174 for line in strct.doc.to_string().lines() {
175 writeln!(out, "//{}", line);
176 }
David Tolnay17a934c2020-11-02 00:40:04 -0800177 writeln!(out, "struct {} final {{", strct.name.cxx);
David Tolnay7db73692019-10-20 14:51:12 -0400178 for field in &strct.fields {
179 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700180 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400181 writeln!(out, "{};", field.ident);
182 }
183 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700184 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400185}
186
187fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
188 writeln!(out, "struct {};", ident);
189}
190
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800191fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800192 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnay0e3ee8e2020-11-01 23:46:52 -0800193 write_atom(out, enm.repr);
194 writeln!(out, ";");
195}
196
David Tolnay8faec772020-11-02 00:18:19 -0800197fn write_struct_using(out: &mut OutFile, ident: &Pair) {
198 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
David Tolnay8861bee2020-01-20 18:39:24 -0800199}
200
David Tolnay0b9b9f82020-11-01 20:41:00 -0800201fn write_struct_with_methods<'a>(
202 out: &mut OutFile<'a>,
203 ety: &'a ExternType,
204 methods: &[&ExternFn],
205) {
David Tolnay17a934c2020-11-02 00:40:04 -0800206 out.set_namespace(&ety.name.namespace);
207 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700208 writeln!(out, "#ifndef {}", guard);
209 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700210 for line in ety.doc.to_string().lines() {
211 writeln!(out, "//{}", line);
212 }
David Tolnay17a934c2020-11-02 00:40:04 -0800213 writeln!(out, "struct {} final {{", ety.name.cxx);
214 writeln!(out, " {}() = delete;", ety.name.cxx);
Adrian Taylorc8713432020-10-21 18:20:55 -0700215 writeln!(
216 out,
217 " {}(const {} &) = delete;",
David Tolnay17a934c2020-11-02 00:40:04 -0800218 ety.name.cxx, ety.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700219 );
Joel Galenson968738f2020-04-15 14:19:33 -0700220 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700221 write!(out, " ");
222 let sig = &method.sig;
David Tolnay17a934c2020-11-02 00:40:04 -0800223 let local_name = method.name.cxx.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700224 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700225 writeln!(out, ";");
226 }
227 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700228 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700229}
230
David Tolnay0b9b9f82020-11-01 20:41:00 -0800231fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800232 out.set_namespace(&enm.name.namespace);
233 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.name.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700234 writeln!(out, "#ifndef {}", guard);
235 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700236 for line in enm.doc.to_string().lines() {
237 writeln!(out, "//{}", line);
238 }
David Tolnay17a934c2020-11-02 00:40:04 -0800239 write!(out, "enum class {} : ", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700240 write_atom(out, enm.repr);
241 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700242 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700243 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700244 }
245 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700246 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700247}
248
David Tolnay0b9b9f82020-11-01 20:41:00 -0800249fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800250 out.set_namespace(&enm.name.namespace);
251 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
David Tolnayf6a89f22020-05-10 23:39:27 -0700252 write_atom(out, enm.repr);
253 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700254 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700255 write!(out, "static_assert(static_cast<");
256 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700257 writeln!(
258 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700259 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
David Tolnay17a934c2020-11-02 00:40:04 -0800260 enm.name.cxx, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700261 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700262 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700263}
264
David Tolnay8faec772020-11-02 00:18:19 -0800265fn check_trivial_extern_type(out: &mut OutFile, id: &Pair) {
David Tolnay174bd952020-11-02 09:23:12 -0800266 // NOTE: The following static assertion is just nice-to-have and not
David Tolnayfd0034e2020-10-04 13:15:34 -0700267 // necessary for soundness. That's because triviality is always declared by
268 // the user in the form of an unsafe impl of cxx::ExternType:
269 //
270 // unsafe impl ExternType for MyType {
271 // type Id = cxx::type_id!("...");
272 // type Kind = cxx::kind::Trivial;
273 // }
274 //
275 // Since the user went on the record with their unsafe impl to unsafely
276 // claim they KNOW that the type is trivial, it's fine for that to be on
David Tolnay174bd952020-11-02 09:23:12 -0800277 // them if that were wrong. However, in practice correctly reasoning about
278 // the relocatability of C++ types is challenging, particularly if the type
279 // definition were to change over time, so for now we add this check.
David Tolnayfd0034e2020-10-04 13:15:34 -0700280 //
David Tolnay174bd952020-11-02 09:23:12 -0800281 // There may be legitimate reasons to opt out of this assertion for support
282 // of types that the programmer knows are soundly Rust-movable despite not
283 // being recognized as such by the C++ type system due to a move constructor
284 // or destructor. To opt out of the relocatability check, they need to do
285 // one of the following things in any header used by `include!` in their
286 // bridge.
287 //
288 // --- if they define the type:
289 // struct MyType {
290 // ...
291 // + using IsRelocatable = std::true_type;
292 // };
293 //
294 // --- otherwise:
295 // + template <>
296 // + struct rust::IsRelocatable<MyType> : std::true_type {};
297 //
David Tolnayfd0034e2020-10-04 13:15:34 -0700298
David Tolnay174bd952020-11-02 09:23:12 -0800299 let id = id.to_fully_qualified();
300 out.builtin.relocatable = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700301 writeln!(out, "static_assert(");
David Tolnay174bd952020-11-02 09:23:12 -0800302 writeln!(out, " ::rust::IsRelocatable<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700303 writeln!(
304 out,
David Tolnay174bd952020-11-02 09:23:12 -0800305 " \"type {} marked as Trivial in Rust is not trivially move constructible and trivially destructible in C++\");",
David Tolnay7426cc12020-10-03 19:04:04 -0700306 id,
307 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700308}
309
David Tolnay0b9b9f82020-11-01 20:41:00 -0800310fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay04770742020-11-01 13:50:50 -0800311 out.next_section();
David Tolnay17a934c2020-11-02 00:40:04 -0800312 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800313 out.begin_block(Block::ExternC);
David Tolnaye1476af2020-11-01 13:47:25 -0800314 if let Some(annotation) = &out.opt.cxx_impl_annotations {
David Tolnaycc1ae762020-10-31 15:53:50 -0700315 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700316 }
David Tolnayebef4a22020-03-17 15:33:47 -0700317 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700318 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700319 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700320 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700321 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700322 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700323 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700324 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700325 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700326 if receiver.mutability.is_none() {
327 write!(out, "const ");
328 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700329 write!(
330 out,
331 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800332 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700333 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700334 }
David Tolnay7db73692019-10-20 14:51:12 -0400335 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700336 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400337 write!(out, ", ");
338 }
David Tolnaya46a2372020-03-06 10:03:48 -0800339 if arg.ty == RustString {
340 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700341 } else if let Type::RustVec(_) = arg.ty {
342 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800343 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700344 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400345 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700346 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400347 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700348 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400349 write!(out, ", ");
350 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700351 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400352 write!(out, "*return$");
353 }
354 writeln!(out, ") noexcept {{");
355 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700356 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700357 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800358 None => write!(out, "(*{}$)(", efn.name.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700359 Some(receiver) => write!(
360 out,
361 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700362 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800363 efn.name.rust,
Adrian Taylorc8713432020-10-21 18:20:55 -0700364 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700365 }
David Tolnay7db73692019-10-20 14:51:12 -0400366 for (i, arg) in efn.args.iter().enumerate() {
367 if i > 0 {
368 write!(out, ", ");
369 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700370 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400371 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700372 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700373 if let Some(receiver) = &efn.receiver {
374 if receiver.mutability.is_none() {
375 write!(out, " const");
376 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700377 }
378 write!(out, " = ");
379 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800380 None => write!(out, "{}", efn.name.to_fully_qualified()),
Adrian Taylorc8713432020-10-21 18:20:55 -0700381 Some(receiver) => write!(
382 out,
383 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700384 out.types.resolve(&receiver.ty).to_fully_qualified(),
David Tolnay17a934c2020-11-02 00:40:04 -0800385 efn.name.cxx,
Adrian Taylorc8713432020-10-21 18:20:55 -0700386 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700387 }
388 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400389 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700390 if efn.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700391 out.builtin.ptr_len = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700392 out.builtin.trycatch = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700393 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700394 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700395 writeln!(out, " [&] {{");
396 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700397 }
David Tolnay7db73692019-10-20 14:51:12 -0400398 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700399 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400400 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700401 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400402 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700403 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400404 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700405 }
406 match &efn.ret {
407 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay74d6d512020-10-31 22:22:03 -0700408 Some(Type::Str(_)) if !indirect_return => {
409 out.builtin.rust_str_repr = true;
410 write!(out, "::rust::impl<::rust::Str>::repr(");
411 }
David Tolnayeb952ba2020-04-14 15:02:24 -0700412 Some(Type::SliceRefU8(_)) if !indirect_return => {
David Tolnay36aa9e02020-10-31 23:08:21 -0700413 out.builtin.rust_slice_repr = true;
414 write!(out, "::rust::impl<::rust::Slice<uint8_t>>::repr(")
David Tolnayeb952ba2020-04-14 15:02:24 -0700415 }
David Tolnay99642622020-03-25 13:07:35 -0700416 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400417 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700418 match &efn.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800419 None => write!(out, "{}$(", efn.name.rust),
420 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700421 }
David Tolnay7db73692019-10-20 14:51:12 -0400422 for (i, arg) in efn.args.iter().enumerate() {
423 if i > 0 {
424 write!(out, ", ");
425 }
426 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700427 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400428 write!(out, "::from_raw({})", arg.ident);
429 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700430 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400431 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700432 } else if let Type::Str(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700433 out.builtin.rust_str_new_unchecked = true;
David Tolnay0356d332020-10-31 19:46:41 -0700434 write!(
435 out,
436 "::rust::impl<::rust::Str>::new_unchecked({})",
437 arg.ident,
438 );
David Tolnaya46a2372020-03-06 10:03:48 -0800439 } else if arg.ty == RustString {
David Tolnay74d6d512020-10-31 22:22:03 -0700440 out.builtin.unsafe_bitcopy = true;
David Tolnaycc3767f2020-03-06 10:41:51 -0800441 write!(
442 out,
443 "::rust::String(::rust::unsafe_bitcopy, *{})",
444 arg.ident,
445 );
David Tolnay313b10e2020-04-25 16:30:51 -0700446 } else if let Type::RustVec(_) = arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700447 out.builtin.unsafe_bitcopy = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700448 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700449 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay36aa9e02020-10-31 23:08:21 -0700450 } else if let Type::SliceRefU8(_) = arg.ty {
451 write!(
452 out,
453 "::rust::Slice<uint8_t>(static_cast<const uint8_t *>({0}.ptr), {0}.len)",
454 arg.ident,
455 );
David Tolnaya7c2ea12020-10-30 21:32:53 -0700456 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700457 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800458 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400459 } else {
460 write!(out, "{}", arg.ident);
461 }
462 }
463 write!(out, ")");
464 match &efn.ret {
465 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
466 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700467 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400468 _ => {}
469 }
470 if indirect_return {
471 write!(out, ")");
472 }
473 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700474 if efn.throws {
475 out.include.cstring = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800476 out.builtin.exception = true;
David Tolnay5d121442020-03-17 22:14:40 -0700477 writeln!(out, " throw$.ptr = nullptr;");
478 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700479 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700480 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700481 writeln!(
482 out,
David Tolnay1f010c62020-11-01 20:27:46 -0800483 " throw$.ptr = ::cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700484 );
David Tolnay5d121442020-03-17 22:14:40 -0700485 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700486 writeln!(out, " return throw$;");
487 }
David Tolnay7db73692019-10-20 14:51:12 -0400488 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700489 for arg in &efn.args {
490 if let Type::Fn(f) = &arg.ty {
491 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700492 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700493 }
494 }
David Tolnayca563ee2020-11-01 20:12:27 -0800495 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700496}
497
498fn write_function_pointer_trampoline(
499 out: &mut OutFile,
500 efn: &ExternFn,
501 var: &Ident,
502 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700503) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700504 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700505 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700506 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700507
508 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700509 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
510 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400511}
512
David Tolnay0b9b9f82020-11-01 20:41:00 -0800513fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800514 out.set_namespace(&efn.name.namespace);
David Tolnayca563ee2020-11-01 20:12:27 -0800515 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700516 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700517 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700518 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnayca563ee2020-11-01 20:12:27 -0800519 out.end_block(Block::ExternC);
David Tolnay75dca2e2020-03-25 20:17:52 -0700520}
521
522fn write_rust_function_decl_impl(
523 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700524 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700525 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700526 indirect_call: bool,
527) {
David Tolnay04770742020-11-01 13:50:50 -0800528 out.next_section();
David Tolnay75dca2e2020-03-25 20:17:52 -0700529 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700530 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700531 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700532 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700533 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700534 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700535 write!(out, "{}(", link_name);
536 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700537 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700538 if receiver.mutability.is_none() {
539 write!(out, "const ");
540 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700541 write!(
542 out,
543 "{} &self",
David Tolnay8faec772020-11-02 00:18:19 -0800544 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700545 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700546 needs_comma = true;
547 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700548 for arg in &sig.args {
549 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400550 write!(out, ", ");
551 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700552 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700553 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400554 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700555 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700556 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400557 write!(out, ", ");
558 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700559 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400560 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700561 needs_comma = true;
562 }
563 if indirect_call {
564 if needs_comma {
565 write!(out, ", ");
566 }
567 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400568 }
569 writeln!(out, ") noexcept;");
570}
571
David Tolnay0b9b9f82020-11-01 20:41:00 -0800572fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
David Tolnay17a934c2020-11-02 00:40:04 -0800573 out.set_namespace(&efn.name.namespace);
David Tolnay7db73692019-10-20 14:51:12 -0400574 for line in efn.doc.to_string().lines() {
575 writeln!(out, "//{}", line);
576 }
David Tolnaya73853b2020-04-20 01:19:56 -0700577 let local_name = match &efn.sig.receiver {
David Tolnay17a934c2020-11-02 00:40:04 -0800578 None => efn.name.cxx.to_string(),
579 Some(receiver) => format!("{}::{}", out.types.resolve(&receiver.ty).cxx, efn.name.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700580 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700581 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700582 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700583 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700584}
585
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700586fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700587 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700588 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700589 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700590 indirect_call: bool,
591) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700592 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700593 write!(out, "{}(", local_name);
594 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400595 if i > 0 {
596 write!(out, ", ");
597 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700598 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400599 write!(out, "{}", arg.ident);
600 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700601 if indirect_call {
602 if !sig.args.is_empty() {
603 write!(out, ", ");
604 }
605 write!(out, "void *extern$");
606 }
David Tolnay1e548172020-03-16 13:37:09 -0700607 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700608 if let Some(receiver) = &sig.receiver {
609 if receiver.mutability.is_none() {
610 write!(out, " const");
611 }
612 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700613 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700614 write!(out, " noexcept");
615 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700616}
617
618fn write_rust_function_shim_impl(
619 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700620 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700621 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700622 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700623 indirect_call: bool,
624) {
625 if out.header && sig.receiver.is_some() {
626 // We've already defined this inside the struct.
627 return;
628 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700629 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400630 if out.header {
631 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700632 return;
David Tolnay7db73692019-10-20 14:51:12 -0400633 }
David Tolnay439cde22020-04-20 00:46:25 -0700634 writeln!(out, " {{");
635 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700636 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700637 out.include.utility = true;
David Tolnay74d6d512020-10-31 22:22:03 -0700638 out.builtin.manually_drop = true;
David Tolnay439cde22020-04-20 00:46:25 -0700639 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700640 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700641 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
642 }
643 }
644 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700645 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700646 if indirect_return {
David Tolnay74d6d512020-10-31 22:22:03 -0700647 out.builtin.maybe_uninit = true;
David Tolnay439cde22020-04-20 00:46:25 -0700648 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700649 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700650 writeln!(out, "> return$;");
651 write!(out, " ");
652 } else if let Some(ret) = &sig.ret {
653 write!(out, "return ");
654 match ret {
655 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700656 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700657 write!(out, "::from_raw(");
658 }
659 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700660 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700661 write!(out, "(");
662 }
663 Type::Ref(_) => write!(out, "*"),
David Tolnay74d6d512020-10-31 22:22:03 -0700664 Type::Str(_) => {
665 out.builtin.rust_str_new_unchecked = true;
666 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
667 }
David Tolnay36aa9e02020-10-31 23:08:21 -0700668 Type::SliceRefU8(_) => {
669 out.builtin.rust_slice_new = true;
670 write!(out, "::rust::impl<::rust::Slice<uint8_t>>::slice(");
671 }
David Tolnay439cde22020-04-20 00:46:25 -0700672 _ => {}
673 }
674 }
675 if sig.throws {
David Tolnay919085c2020-10-31 22:32:22 -0700676 out.builtin.ptr_len = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700677 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700678 }
679 write!(out, "{}(", invoke);
680 if sig.receiver.is_some() {
681 write!(out, "*this");
682 }
683 for (i, arg) in sig.args.iter().enumerate() {
684 if i > 0 || sig.receiver.is_some() {
685 write!(out, ", ");
686 }
687 match &arg.ty {
David Tolnay74d6d512020-10-31 22:22:03 -0700688 Type::Str(_) => {
689 out.builtin.rust_str_repr = true;
690 write!(out, "::rust::impl<::rust::Str>::repr(");
691 }
David Tolnay36aa9e02020-10-31 23:08:21 -0700692 Type::SliceRefU8(_) => {
693 out.builtin.rust_slice_repr = true;
694 write!(out, "::rust::impl<::rust::Slice<uint8_t>>::repr(");
695 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700696 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700697 _ => {}
698 }
699 write!(out, "{}", arg.ident);
700 match &arg.ty {
701 Type::RustBox(_) => write!(out, ".into_raw()"),
702 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700703 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700704 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700705 _ => {}
706 }
707 }
708 if indirect_return {
709 if !sig.args.is_empty() {
710 write!(out, ", ");
711 }
712 write!(out, "&return$.value");
713 }
714 if indirect_call {
715 if !sig.args.is_empty() || indirect_return {
716 write!(out, ", ");
717 }
718 write!(out, "extern$");
719 }
720 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400721 if !indirect_return {
722 if let Some(ret) = &sig.ret {
David Tolnay36aa9e02020-10-31 23:08:21 -0700723 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRefU8(_) = ret
724 {
David Tolnay22602b42020-09-21 18:04:05 -0400725 write!(out, ")");
726 }
David Tolnay439cde22020-04-20 00:46:25 -0700727 }
728 }
729 writeln!(out, ";");
730 if sig.throws {
David Tolnay74d6d512020-10-31 22:22:03 -0700731 out.builtin.rust_error = true;
David Tolnayd68dfa82020-10-31 16:01:24 -0700732 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700733 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700734 writeln!(out, " }}");
735 }
736 if indirect_return {
737 out.include.utility = true;
738 writeln!(out, " return ::std::move(return$.value);");
739 }
740 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400741}
742
David Tolnaya7c2ea12020-10-30 21:32:53 -0700743fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400744 match ty {
745 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700746 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400747 }
748}
749
David Tolnay75dca2e2020-03-25 20:17:52 -0700750fn indirect_return(sig: &Signature, types: &Types) -> bool {
751 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700752 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700753 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700754}
755
David Tolnaya7c2ea12020-10-30 21:32:53 -0700756fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700757 match ty {
758 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700759 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700760 write!(out, "*");
761 }
762 Type::Ref(ty) => {
763 if ty.mutability.is_none() {
764 write!(out, "const ");
765 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700766 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700767 write!(out, " *");
768 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700769 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700770 }
771}
772
David Tolnaya7c2ea12020-10-30 21:32:53 -0700773fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
774 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700775 match ty {
776 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700777 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700778 _ => write_space_after_type(out, ty),
779 }
780}
781
David Tolnaya7c2ea12020-10-30 21:32:53 -0700782fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400783 match ty {
784 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700785 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400786 write!(out, "*");
787 }
David Tolnay4a441222020-01-25 16:24:27 -0800788 Some(Type::Ref(ty)) => {
789 if ty.mutability.is_none() {
790 write!(out, "const ");
791 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700792 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -0800793 write!(out, " *");
794 }
David Tolnay36aa9e02020-10-31 23:08:21 -0700795 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) => {
David Tolnay919085c2020-10-31 22:32:22 -0700796 out.builtin.ptr_len = true;
797 write!(out, "::rust::repr::PtrLen ");
798 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700799 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
800 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400801 }
802}
803
David Tolnaya7c2ea12020-10-30 21:32:53 -0700804fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -0400805 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700806 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700807 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400808 write!(out, "*");
809 }
David Tolnay36aa9e02020-10-31 23:08:21 -0700810 Type::Str(_) | Type::SliceRefU8(_) => {
David Tolnay919085c2020-10-31 22:32:22 -0700811 out.builtin.ptr_len = true;
812 write!(out, "::rust::repr::PtrLen ");
813 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700814 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -0400815 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700816 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -0400817 write!(out, "*");
818 }
819 write!(out, "{}", arg.ident);
820}
821
David Tolnaya7c2ea12020-10-30 21:32:53 -0700822fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400823 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700824 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700825 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700826 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -0400827 },
828 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800829 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700830 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400831 write!(out, ">");
832 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700833 Type::RustVec(ty) => {
834 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700835 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +0700836 write!(out, ">");
837 }
David Tolnay7db73692019-10-20 14:51:12 -0400838 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800839 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700840 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400841 write!(out, ">");
842 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700843 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700844 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700845 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +0700846 write!(out, ">");
847 }
David Tolnay7db73692019-10-20 14:51:12 -0400848 Type::Ref(r) => {
849 if r.mutability.is_none() {
850 write!(out, "const ");
851 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700852 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400853 write!(out, " &");
854 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700855 Type::Slice(_) => {
856 // For now, only U8 slices are supported, which are covered separately below
857 unreachable!()
858 }
David Tolnay7db73692019-10-20 14:51:12 -0400859 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800860 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400861 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700862 Type::SliceRefU8(_) => {
863 write!(out, "::rust::Slice<uint8_t>");
864 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700865 Type::Fn(f) => {
866 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
867 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700868 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -0700869 None => write!(out, "void"),
870 }
871 write!(out, "(");
872 for (i, arg) in f.args.iter().enumerate() {
873 if i > 0 {
874 write!(out, ", ");
875 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700876 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -0700877 }
878 write!(out, ")>");
879 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700880 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400881 }
882}
883
David Tolnayf6a89f22020-05-10 23:39:27 -0700884fn write_atom(out: &mut OutFile, atom: Atom) {
885 match atom {
886 Bool => write!(out, "bool"),
887 U8 => write!(out, "uint8_t"),
888 U16 => write!(out, "uint16_t"),
889 U32 => write!(out, "uint32_t"),
890 U64 => write!(out, "uint64_t"),
891 Usize => write!(out, "size_t"),
892 I8 => write!(out, "int8_t"),
893 I16 => write!(out, "int16_t"),
894 I32 => write!(out, "int32_t"),
895 I64 => write!(out, "int64_t"),
896 Isize => write!(out, "::rust::isize"),
897 F32 => write!(out, "float"),
898 F64 => write!(out, "double"),
899 CxxString => write!(out, "::std::string"),
900 RustString => write!(out, "::rust::String"),
901 }
902}
903
David Tolnaya7c2ea12020-10-30 21:32:53 -0700904fn write_type_space(out: &mut OutFile, ty: &Type) {
905 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700906 write_space_after_type(out, ty);
907}
908
909fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400910 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700911 Type::Ident(_)
912 | Type::RustBox(_)
913 | Type::UniquePtr(_)
914 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700915 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700916 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700917 | Type::SliceRefU8(_)
918 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400919 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700920 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400921 }
922}
923
David Tolnaycd08c442020-04-25 10:16:33 -0700924// Only called for legal referent types of unique_ptr and element types of
925// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -0700926fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -0700927 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700928 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
929 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -0700930 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700931 }
932}
933
David Tolnayacdf20a2020-04-25 12:40:53 -0700934// Only called for legal referent types of unique_ptr and element types of
935// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -0700936fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -0700937 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700938 Type::Ident(ident) => ident.to_symbol(types),
939 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -0700940 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700941 }
942}
943
David Tolnaya7c2ea12020-10-30 21:32:53 -0700944fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay169bb472020-11-01 21:04:24 -0800945 if out.header {
946 return;
947 }
948
949 out.next_section();
David Tolnay078c90f2020-11-01 13:31:08 -0800950 out.set_namespace(Default::default());
David Tolnay0c033e32020-11-01 15:15:48 -0800951 out.begin_block(Block::ExternC);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700952 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400953 if let Type::RustBox(ty) = ty {
954 if let Type::Ident(inner) = &ty.inner {
955 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700956 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -0400957 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700958 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700959 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700960 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -0700961 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700962 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -0700963 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700964 }
David Tolnay7db73692019-10-20 14:51:12 -0400965 } else if let Type::UniquePtr(ptr) = ty {
966 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700967 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -0700968 && (!out.types.aliases.contains_key(&inner.rust)
969 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -0700970 {
David Tolnay7db73692019-10-20 14:51:12 -0400971 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700972 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +0700973 }
974 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700975 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +0700976 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700977 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -0700978 && (!out.types.aliases.contains_key(&inner.rust)
979 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -0700980 {
Myron Ahneba35cf2020-02-05 19:41:51 +0700981 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700982 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400983 }
984 }
985 }
986 }
David Tolnay0c033e32020-11-01 15:15:48 -0800987 out.end_block(Block::ExternC);
David Tolnay7db73692019-10-20 14:51:12 -0400988
David Tolnay0c033e32020-11-01 15:15:48 -0800989 out.begin_block(Block::Namespace("rust"));
990 out.begin_block(Block::InlineNamespace("cxxbridge05"));
David Tolnaya7c2ea12020-10-30 21:32:53 -0700991 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400992 if let Type::RustBox(ty) = ty {
993 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700994 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -0400995 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700996 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700997 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700998 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700999 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001000 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001001 }
David Tolnay7db73692019-10-20 14:51:12 -04001002 }
1003 }
David Tolnay0c033e32020-11-01 15:15:48 -08001004 out.end_block(Block::InlineNamespace("cxxbridge05"));
1005 out.end_block(Block::Namespace("rust"));
David Tolnay7db73692019-10-20 14:51:12 -04001006}
1007
David Tolnay8faec772020-11-02 00:18:19 -08001008fn write_rust_box_extern(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001009 let inner = ident.to_fully_qualified();
1010 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001011
David Tolnay8f16ae72020-10-08 18:21:13 -07001012 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1013 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001014 writeln!(
1015 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001016 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001017 instance, inner,
1018 );
1019 writeln!(
1020 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001021 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001022 instance, inner,
1023 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001024 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001025}
1026
David Tolnaya7c2ea12020-10-30 21:32:53 -07001027fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001028 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001029 let inner = to_typename(&element, out.types);
1030 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001031
David Tolnay8f16ae72020-10-08 18:21:13 -07001032 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1033 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001034 writeln!(
1035 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001036 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001037 instance, inner,
1038 );
1039 writeln!(
1040 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001041 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001042 instance, inner,
1043 );
1044 writeln!(
1045 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001046 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001047 instance, inner,
1048 );
David Tolnay219c0792020-04-24 20:31:37 -07001049 writeln!(
1050 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001051 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001052 inner, instance,
1053 );
David Tolnay503d0192020-04-24 22:18:56 -07001054 writeln!(
1055 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001056 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001057 instance,
1058 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001059 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001060}
1061
David Tolnay8faec772020-11-02 00:18:19 -08001062fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
Adrian Taylorc8713432020-10-21 18:20:55 -07001063 let inner = ident.to_fully_qualified();
1064 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001065
1066 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001067 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001068 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001069 writeln!(out, "}}");
1070
1071 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001072 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001073 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001074 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001075}
1076
David Tolnaya7c2ea12020-10-30 21:32:53 -07001077fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001078 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001079 let inner = to_typename(&element, out.types);
1080 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001081
Myron Ahneba35cf2020-02-05 19:41:51 +07001082 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001083 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001084 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001085 writeln!(out, "}}");
1086
1087 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001088 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1089 writeln!(
1090 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001091 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001092 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001093 );
1094 writeln!(out, "}}");
1095
1096 writeln!(out, "template <>");
1097 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001098 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001099 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001100
1101 writeln!(out, "template <>");
1102 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1103 writeln!(
1104 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001105 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001106 instance,
1107 );
1108 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001109
1110 writeln!(out, "template <>");
1111 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001112 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001113 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001114}
1115
David Tolnaya7c2ea12020-10-30 21:32:53 -07001116fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001117 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001118 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001119
David Tolnay8f16ae72020-10-08 18:21:13 -07001120 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1121 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001122
David Tolnaya7c2ea12020-10-30 21:32:53 -07001123 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001124
David Tolnay8f16ae72020-10-08 18:21:13 -07001125 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001126}
1127
1128// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001129fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001130 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001131 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001132 let inner = to_typename(ty, out.types);
1133 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001134
David Tolnay63da4d32020-04-25 09:41:12 -07001135 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001136 // Some aliases are to opaque types; some are to trivial types. We can't
1137 // know at code generation time, so we generate both C++ and Rust side
1138 // bindings for a "new" method anyway. But the Rust code can't be called
1139 // for Opaque types because the 'new' method is not implemented.
1140 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001141 out.types.structs.contains_key(&ident.rust)
1142 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001143 }
David Tolnay63da4d32020-04-25 09:41:12 -07001144 _ => false,
1145 };
1146
David Tolnay7db73692019-10-20 14:51:12 -04001147 writeln!(
1148 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001149 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001150 inner,
1151 );
1152 writeln!(
1153 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001154 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001155 inner,
1156 );
1157 writeln!(
1158 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001159 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001160 instance, inner,
1161 );
David Tolnay7e219b82020-03-01 13:14:51 -08001162 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001163 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001164 if can_construct_from_value {
1165 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001166 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001167 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001168 instance, inner, inner,
1169 );
David Tolnay63da4d32020-04-25 09:41:12 -07001170 writeln!(
1171 out,
1172 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1173 inner, inner,
1174 );
1175 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001176 }
David Tolnay7db73692019-10-20 14:51:12 -04001177 writeln!(
1178 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001179 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001180 instance, inner, inner,
1181 );
David Tolnay7e219b82020-03-01 13:14:51 -08001182 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001183 writeln!(out, "}}");
1184 writeln!(
1185 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001186 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001187 inner, instance, inner,
1188 );
1189 writeln!(out, " return ptr.get();");
1190 writeln!(out, "}}");
1191 writeln!(
1192 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001193 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001194 inner, instance, inner,
1195 );
1196 writeln!(out, " return ptr.release();");
1197 writeln!(out, "}}");
1198 writeln!(
1199 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001200 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001201 instance, inner,
1202 );
1203 writeln!(out, " ptr->~unique_ptr();");
1204 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001205}
Myron Ahneba35cf2020-02-05 19:41:51 +07001206
David Tolnaya7c2ea12020-10-30 21:32:53 -07001207fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001208 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001209 let inner = to_typename(&element, out.types);
1210 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001211
David Tolnay8f16ae72020-10-08 18:21:13 -07001212 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1213 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001214 writeln!(
1215 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001216 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001217 instance, inner,
1218 );
1219 writeln!(out, " return s.size();");
1220 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001221 writeln!(
1222 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001223 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001224 inner, instance, inner,
1225 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001226 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001227 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001228
David Tolnaya7c2ea12020-10-30 21:32:53 -07001229 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001230
David Tolnay8f16ae72020-10-08 18:21:13 -07001231 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001232}