blob: f1c7a11049de55d4c12d831d56ed82978bfb2103 [file] [log] [blame]
David Tolnay4aae7c02020-10-28 12:35:42 -07001use crate::gen::include::Include;
David Tolnay7db73692019-10-20 14:51:12 -04002use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07003use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04004use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07005use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07006use crate::syntax::symbol::Symbol;
David Tolnay4aae7c02020-10-28 12:35:42 -07007use crate::syntax::{mangle, Api, Enum, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04008use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -07009use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040010
David Tolnay33d30292020-03-18 18:02:02 -070011pub(super) fn gen(
David Tolnay2ec14632020-05-04 00:47:10 -070012 namespace: &Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070013 apis: &[Api],
14 types: &Types,
David Tolnaya5cca312020-08-29 23:40:04 -070015 opt: &Opt,
David Tolnay33d30292020-03-18 18:02:02 -070016 header: bool,
17) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040018 let mut out_file = OutFile::new(namespace.clone(), header);
19 let out = &mut out_file;
20
David Tolnay54702b92020-07-31 11:50:09 -070021 if header {
22 writeln!(out.front, "#pragma once");
23 }
24
David Tolnay4aae7c02020-10-28 12:35:42 -070025 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040026 for api in apis {
27 if let Api::Include(include) = api {
David Tolnay4aae7c02020-10-28 12:35:42 -070028 let path = include.path.clone();
29 let kind = include.kind;
30 out.include.insert(Include { path, kind });
David Tolnay7db73692019-10-20 14:51:12 -040031 }
32 }
33
34 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080035 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040036
David Tolnay7db73692019-10-20 14:51:12 -040037 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070038 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040039 writeln!(out, "namespace {} {{", name);
40 }
41
David Tolnay7db73692019-10-20 14:51:12 -040042 out.next_section();
43 for api in apis {
44 match api {
45 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080046 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
47 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070048 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040049 }
50 }
51
David Tolnayf94bef12020-04-17 14:46:42 -070052 let mut methods_for_type = HashMap::new();
53 for api in apis {
54 if let Api::RustFunction(efn) = api {
55 if let Some(receiver) = &efn.sig.receiver {
56 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070057 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070058 .or_insert_with(Vec::new)
59 .push(efn);
60 }
61 }
62 }
Joel Galenson968738f2020-04-15 14:19:33 -070063
David Tolnay7db73692019-10-20 14:51:12 -040064 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070065 match api {
66 Api::Struct(strct) => {
67 out.next_section();
David Tolnaya593d6e2020-08-29 19:48:08 -070068 if !types.cxx.contains(&strct.ident) {
69 write_struct(out, strct);
70 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070071 }
Joel Galensonc03402a2020-04-23 17:31:09 -070072 Api::Enum(enm) => {
73 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070074 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070075 check_enum(out, enm);
76 } else {
77 write_enum(out, enm);
78 }
Joel Galensonc03402a2020-04-23 17:31:09 -070079 }
David Tolnayc1fe0052020-04-17 15:15:06 -070080 Api::RustType(ety) => {
81 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070082 out.next_section();
83 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070084 }
David Tolnayc1fe0052020-04-17 15:15:06 -070085 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070086 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040087 }
88 }
89
David Tolnayfabca772020-10-03 21:25:41 -070090 out.next_section();
91 for api in apis {
92 if let Api::TypeAlias(ety) = api {
93 if types.required_trivial.contains_key(&ety.ident) {
94 check_trivial_extern_type(out, &ety.ident)
95 }
96 }
97 }
98
David Tolnay7db73692019-10-20 14:51:12 -040099 if !header {
100 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700101 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400102 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700103 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400104 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
105 Api::RustFunction(efn) => (efn, write_rust_function_decl),
106 _ => continue,
107 };
108 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700109 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400110 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800111 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400112 }
113
114 for api in apis {
115 if let Api::RustFunction(efn) = api {
116 out.next_section();
117 write_rust_function_shim(out, efn, types);
118 }
119 }
120
121 out.next_section();
122 for name in namespace.iter().rev() {
123 writeln!(out, "}} // namespace {}", name);
124 }
125
126 if !header {
127 out.next_section();
128 write_generic_instantiations(out, types);
129 }
130
David Tolnay54702b92020-07-31 11:50:09 -0700131 write!(out.front, "{}", out.include);
132
133 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400134}
135
136fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400137 for ty in types {
138 match ty {
David Tolnay89e386d2020-10-03 19:02:19 -0700139 Type::Ident(ident) => match Atom::from(ident) {
140 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
141 | Some(I64) => out.include.cstdint = true,
142 Some(Usize) => out.include.cstddef = true,
143 Some(CxxString) => out.include.string = true,
David Tolnayf57f7562020-10-04 19:56:26 -0700144 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700145 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800146 Type::RustBox(_) => out.include.type_traits = true,
147 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700148 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700149 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700150 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400151 }
152 }
David Tolnay7db73692019-10-20 14:51:12 -0400153}
154
David Tolnayf51447e2020-03-06 14:14:27 -0800155fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay16ab1462020-09-02 15:10:09 -0700156 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700157 let mut needs_rust_string = false;
158 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700159 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400160 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700161 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700162 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700163 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700164 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400165 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 match ty {
167 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700168 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700169 out.include.type_traits = true;
170 needs_rust_box = true;
171 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700172 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700173 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700174 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700175 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700176 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700177 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700178 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700179 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700180 Type::Str(_) => {
181 out.include.cstdint = true;
182 out.include.string = true;
183 needs_rust_str = true;
184 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700185 Type::Fn(_) => {
186 needs_rust_fn = true;
187 }
David Tolnay4770b472020-04-14 16:32:59 -0700188 Type::Slice(_) | Type::SliceRefU8(_) => {
189 needs_rust_slice = true;
190 }
David Tolnay7c295462020-04-25 12:45:07 -0700191 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400192 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700193 needs_rust_isize = true;
194 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700195 ty if ty == RustString => {
196 out.include.array = true;
197 out.include.cstdint = true;
198 out.include.string = true;
199 needs_rust_string = true;
200 }
201 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400202 }
203 }
204
David Tolnayb7a7cb62020-03-17 21:18:40 -0700205 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800206 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800207 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700208 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800209 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700210 match api {
211 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700212 if efn.throws {
213 needs_trycatch = true;
214 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700215 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700216 let bitcopy = match arg.ty {
217 Type::RustVec(_) => true,
218 _ => arg.ty == RustString,
219 };
220 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 needs_unsafe_bitcopy = true;
222 break;
223 }
David Tolnay09011c32020-03-06 14:40:28 -0800224 }
225 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226 Api::RustFunction(efn) if !out.header => {
227 if efn.throws {
228 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700229 out.include.string = true;
230 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200232 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 }
234 for arg in &efn.args {
235 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
236 needs_manually_drop = true;
237 break;
238 }
239 }
240 if let Some(ret) = &efn.ret {
241 if types.needs_indirect_abi(ret) {
242 needs_maybe_uninit = true;
243 }
David Tolnayf51447e2020-03-06 14:14:27 -0800244 }
245 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700246 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800247 }
248 }
249
David Tolnay750755e2020-03-01 13:04:08 -0800250 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700251 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800252
David Tolnay16ab1462020-09-02 15:10:09 -0700253 if needs_panic
254 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700255 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700256 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700257 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700258 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700259 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700260 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700261 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700262 || needs_unsafe_bitcopy
263 || needs_manually_drop
264 || needs_maybe_uninit
265 {
David Tolnay736cbca2020-03-11 16:49:18 -0700266 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800267 }
268
David Tolnay8f16ae72020-10-08 18:21:13 -0700269 include::write(out, needs_panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700270
David Tolnay99a95e62020-09-02 15:05:53 -0700271 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700272 out.next_section();
273 writeln!(out, "struct unsafe_bitcopy_t;");
274 }
275
David Tolnay8f16ae72020-10-08 18:21:13 -0700276 include::write(out, needs_rust_string, "CXXBRIDGE05_RUST_STRING");
277 include::write(out, needs_rust_str, "CXXBRIDGE05_RUST_STR");
278 include::write(out, needs_rust_slice, "CXXBRIDGE05_RUST_SLICE");
279 include::write(out, needs_rust_box, "CXXBRIDGE05_RUST_BOX");
280 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
281 include::write(out, needs_rust_vec, "CXXBRIDGE05_RUST_VEC");
282 include::write(out, needs_rust_fn, "CXXBRIDGE05_RUST_FN");
283 include::write(out, needs_rust_error, "CXXBRIDGE05_RUST_ERROR");
284 include::write(out, needs_rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800285
286 if needs_manually_drop {
287 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700288 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800289 writeln!(out, "template <typename T>");
290 writeln!(out, "union ManuallyDrop {{");
291 writeln!(out, " T value;");
292 writeln!(
293 out,
294 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
295 );
296 writeln!(out, " ~ManuallyDrop() {{}}");
297 writeln!(out, "}};");
298 }
299
David Tolnay09011c32020-03-06 14:40:28 -0800300 if needs_maybe_uninit {
301 out.next_section();
302 writeln!(out, "template <typename T>");
303 writeln!(out, "union MaybeUninit {{");
304 writeln!(out, " T value;");
305 writeln!(out, " MaybeUninit() {{}}");
306 writeln!(out, " ~MaybeUninit() {{}}");
307 writeln!(out, "}};");
308 }
309
David Tolnay8f16ae72020-10-08 18:21:13 -0700310 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700311
David Tolnay5d121442020-03-17 22:14:40 -0700312 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700313 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700314 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700315 out.include.type_traits = true;
316 out.include.utility = true;
317 writeln!(out, "class missing {{}};");
318 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700319 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700320 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700321 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700322 writeln!(
323 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700324 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700325 );
David Tolnay04722332020-03-18 11:31:54 -0700326 writeln!(out, " missing>::value>::type");
327 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700328 writeln!(out, " func();");
329 writeln!(out, "}} catch (const ::std::exception &e) {{");
330 writeln!(out, " fail(e.what());");
331 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700332 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700333 }
334
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800335 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400336}
337
338fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay8f16ae72020-10-08 18:21:13 -0700339 let guard = format!("CXXBRIDGE05_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700340 writeln!(out, "#ifndef {}", guard);
341 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400342 for line in strct.doc.to_string().lines() {
343 writeln!(out, "//{}", line);
344 }
345 writeln!(out, "struct {} final {{", strct.ident);
346 for field in &strct.fields {
347 write!(out, " ");
348 write_type_space(out, &field.ty);
349 writeln!(out, "{};", field.ident);
350 }
351 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700352 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400353}
354
355fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
356 writeln!(out, "struct {};", ident);
357}
358
David Tolnay8861bee2020-01-20 18:39:24 -0800359fn write_struct_using(out: &mut OutFile, ident: &Ident) {
360 writeln!(out, "using {} = {};", ident, ident);
361}
362
David Tolnayc1fe0052020-04-17 15:15:06 -0700363fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay8f16ae72020-10-08 18:21:13 -0700364 let guard = format!("CXXBRIDGE05_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700365 writeln!(out, "#ifndef {}", guard);
366 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700367 for line in ety.doc.to_string().lines() {
368 writeln!(out, "//{}", line);
369 }
370 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700371 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700372 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700373 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700374 write!(out, " ");
375 let sig = &method.sig;
David Tolnaya4641c72020-09-08 14:05:53 -0700376 let local_name = method.ident.cxx.to_string();
David Tolnaya73853b2020-04-20 01:19:56 -0700377 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700378 writeln!(out, ";");
379 }
380 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700381 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700382}
383
Joel Galensonc03402a2020-04-23 17:31:09 -0700384fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay8f16ae72020-10-08 18:21:13 -0700385 let guard = format!("CXXBRIDGE05_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700386 writeln!(out, "#ifndef {}", guard);
387 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700388 for line in enm.doc.to_string().lines() {
389 writeln!(out, "//{}", line);
390 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700391 write!(out, "enum class {} : ", enm.ident);
392 write_atom(out, enm.repr);
393 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700394 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700395 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700396 }
397 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700398 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700399}
400
Joel Galenson905eb2e2020-05-04 14:58:14 -0700401fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700402 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
403 write_atom(out, enm.repr);
404 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700405 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700406 write!(out, "static_assert(static_cast<");
407 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700408 writeln!(
409 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700410 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700411 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700412 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700413 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700414}
415
Adrian Taylor405e8742020-09-25 14:01:25 -0700416fn check_trivial_extern_type(out: &mut OutFile, id: &Ident) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700417 // NOTE: The following two static assertions are just nice-to-have and not
418 // necessary for soundness. That's because triviality is always declared by
419 // the user in the form of an unsafe impl of cxx::ExternType:
420 //
421 // unsafe impl ExternType for MyType {
422 // type Id = cxx::type_id!("...");
423 // type Kind = cxx::kind::Trivial;
424 // }
425 //
426 // Since the user went on the record with their unsafe impl to unsafely
427 // claim they KNOW that the type is trivial, it's fine for that to be on
428 // them if that were wrong.
429 //
430 // There may be a legitimate reason we'll want to remove these assertions
431 // for support of types that the programmer knows are Rust-movable despite
432 // not being recognized as such by the C++ type system due to a move
433 // constructor or destructor.
434
David Tolnayf57f7562020-10-04 19:56:26 -0700435 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700436 writeln!(out, "static_assert(");
437 writeln!(
438 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700439 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700440 id,
441 );
442 writeln!(
443 out,
444 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
445 id,
446 );
447 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700448 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700449 writeln!(
450 out,
451 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
452 id,
453 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700454}
455
David Tolnayebef4a22020-03-17 15:33:47 -0700456fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
457 let mut has_cxx_throws = false;
458 for api in apis {
459 if let Api::CxxFunction(efn) = api {
460 if efn.throws {
461 has_cxx_throws = true;
462 break;
463 }
464 }
465 }
466
467 if has_cxx_throws {
468 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700469 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700470 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700471 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700472 );
473 }
474}
475
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700476fn write_cxx_function_shim(
477 out: &mut OutFile,
478 efn: &ExternFn,
479 types: &Types,
480 impl_annotations: &Option<String>,
481) {
482 if !out.header {
483 if let Some(annotation) = impl_annotations {
484 write!(out, "{} ", annotation);
485 }
486 }
David Tolnayebef4a22020-03-17 15:33:47 -0700487 if efn.throws {
488 write!(out, "::rust::Str::Repr ");
489 } else {
David Tolnay99642622020-03-25 13:07:35 -0700490 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700491 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700492 let mangled = mangle::extern_fn(&out.namespace, efn);
493 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700494 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700495 if receiver.mutability.is_none() {
496 write!(out, "const ");
497 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700498 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700499 }
David Tolnay7db73692019-10-20 14:51:12 -0400500 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700501 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400502 write!(out, ", ");
503 }
David Tolnaya46a2372020-03-06 10:03:48 -0800504 if arg.ty == RustString {
505 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700506 } else if let Type::RustVec(_) = arg.ty {
507 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800508 }
David Tolnay7db73692019-10-20 14:51:12 -0400509 write_extern_arg(out, arg, types);
510 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700511 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400512 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700513 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400514 write!(out, ", ");
515 }
David Tolnay99642622020-03-25 13:07:35 -0700516 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400517 write!(out, "*return$");
518 }
519 writeln!(out, ") noexcept {{");
520 write!(out, " ");
521 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700522 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700523 None => write!(out, "(*{}$)(", efn.ident.rust),
524 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700525 }
David Tolnay7db73692019-10-20 14:51:12 -0400526 for (i, arg) in efn.args.iter().enumerate() {
527 if i > 0 {
528 write!(out, ", ");
529 }
530 write_type(out, &arg.ty);
531 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700532 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700533 if let Some(receiver) = &efn.receiver {
534 if receiver.mutability.is_none() {
535 write!(out, " const");
536 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700537 }
538 write!(out, " = ");
539 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700540 None => write!(out, "{}", efn.ident.cxx),
541 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident.cxx),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700542 }
543 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400544 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700545 if efn.throws {
546 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700547 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700548 writeln!(out, " [&] {{");
549 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700550 }
David Tolnay7db73692019-10-20 14:51:12 -0400551 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700552 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400553 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700554 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400555 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700556 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400557 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700558 }
559 match &efn.ret {
560 Some(Type::Ref(_)) => write!(out, "&"),
561 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700562 Some(Type::SliceRefU8(_)) if !indirect_return => {
563 write!(out, "::rust::Slice<uint8_t>::Repr(")
564 }
David Tolnay99642622020-03-25 13:07:35 -0700565 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400566 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700567 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700568 None => write!(out, "{}$(", efn.ident.rust),
569 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700570 }
David Tolnay7db73692019-10-20 14:51:12 -0400571 for (i, arg) in efn.args.iter().enumerate() {
572 if i > 0 {
573 write!(out, ", ");
574 }
575 if let Type::RustBox(_) = &arg.ty {
576 write_type(out, &arg.ty);
577 write!(out, "::from_raw({})", arg.ident);
578 } else if let Type::UniquePtr(_) = &arg.ty {
579 write_type(out, &arg.ty);
580 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800581 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800582 write!(
583 out,
584 "::rust::String(::rust::unsafe_bitcopy, *{})",
585 arg.ident,
586 );
David Tolnay313b10e2020-04-25 16:30:51 -0700587 } else if let Type::RustVec(_) = arg.ty {
588 write_type(out, &arg.ty);
589 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400590 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700591 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800592 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400593 } else {
594 write!(out, "{}", arg.ident);
595 }
596 }
597 write!(out, ")");
598 match &efn.ret {
599 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
600 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700601 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400602 _ => {}
603 }
604 if indirect_return {
605 write!(out, ")");
606 }
607 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700608 if efn.throws {
609 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700610 writeln!(out, " throw$.ptr = nullptr;");
611 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700612 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700613 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700614 writeln!(
615 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700616 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700617 );
David Tolnay5d121442020-03-17 22:14:40 -0700618 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700619 writeln!(out, " return throw$;");
620 }
David Tolnay7db73692019-10-20 14:51:12 -0400621 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700622 for arg in &efn.args {
623 if let Type::Fn(f) = &arg.ty {
624 let var = &arg.ident;
625 write_function_pointer_trampoline(out, efn, var, f, types);
626 }
627 }
628}
629
630fn write_function_pointer_trampoline(
631 out: &mut OutFile,
632 efn: &ExternFn,
633 var: &Ident,
634 f: &Signature,
635 types: &Types,
636) {
637 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700638 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700639 let indirect_call = true;
640 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
641
642 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700643 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400645}
646
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700647fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700648 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700649 let indirect_call = false;
650 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
651}
652
653fn write_rust_function_decl_impl(
654 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700655 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 sig: &Signature,
657 types: &Types,
658 indirect_call: bool,
659) {
660 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700661 write!(out, "::rust::Str::Repr ");
662 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700663 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700664 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700665 write!(out, "{}(", link_name);
666 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700667 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700668 if receiver.mutability.is_none() {
669 write!(out, "const ");
670 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700671 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700672 needs_comma = true;
673 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700674 for arg in &sig.args {
675 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400676 write!(out, ", ");
677 }
678 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700679 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400680 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700681 if indirect_return(sig, types) {
682 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400683 write!(out, ", ");
684 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700685 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400686 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700687 needs_comma = true;
688 }
689 if indirect_call {
690 if needs_comma {
691 write!(out, ", ");
692 }
693 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400694 }
695 writeln!(out, ") noexcept;");
696}
697
698fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400699 for line in efn.doc.to_string().lines() {
700 writeln!(out, "//{}", line);
701 }
David Tolnaya73853b2020-04-20 01:19:56 -0700702 let local_name = match &efn.sig.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700703 None => efn.ident.cxx.to_string(),
704 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700705 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700706 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700707 let indirect_call = false;
708 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
709}
710
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700711fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700712 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700713 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700714 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700715 indirect_call: bool,
716) {
717 write_return_type(out, &sig.ret);
718 write!(out, "{}(", local_name);
719 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400720 if i > 0 {
721 write!(out, ", ");
722 }
723 write_type_space(out, &arg.ty);
724 write!(out, "{}", arg.ident);
725 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700726 if indirect_call {
727 if !sig.args.is_empty() {
728 write!(out, ", ");
729 }
730 write!(out, "void *extern$");
731 }
David Tolnay1e548172020-03-16 13:37:09 -0700732 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700733 if let Some(receiver) = &sig.receiver {
734 if receiver.mutability.is_none() {
735 write!(out, " const");
736 }
737 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700738 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700739 write!(out, " noexcept");
740 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700741}
742
743fn write_rust_function_shim_impl(
744 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700745 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700746 sig: &Signature,
747 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700748 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700749 indirect_call: bool,
750) {
751 if out.header && sig.receiver.is_some() {
752 // We've already defined this inside the struct.
753 return;
754 }
David Tolnaya73853b2020-04-20 01:19:56 -0700755 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400756 if out.header {
757 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700758 return;
David Tolnay7db73692019-10-20 14:51:12 -0400759 }
David Tolnay439cde22020-04-20 00:46:25 -0700760 writeln!(out, " {{");
761 for arg in &sig.args {
762 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
763 out.include.utility = true;
764 write!(out, " ::rust::ManuallyDrop<");
765 write_type(out, &arg.ty);
766 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
767 }
768 }
769 write!(out, " ");
770 let indirect_return = indirect_return(sig, types);
771 if indirect_return {
772 write!(out, "::rust::MaybeUninit<");
773 write_type(out, sig.ret.as_ref().unwrap());
774 writeln!(out, "> return$;");
775 write!(out, " ");
776 } else if let Some(ret) = &sig.ret {
777 write!(out, "return ");
778 match ret {
779 Type::RustBox(_) => {
780 write_type(out, ret);
781 write!(out, "::from_raw(");
782 }
783 Type::UniquePtr(_) => {
784 write_type(out, ret);
785 write!(out, "(");
786 }
787 Type::Ref(_) => write!(out, "*"),
788 _ => {}
789 }
790 }
791 if sig.throws {
792 write!(out, "::rust::Str::Repr error$ = ");
793 }
794 write!(out, "{}(", invoke);
795 if sig.receiver.is_some() {
796 write!(out, "*this");
797 }
798 for (i, arg) in sig.args.iter().enumerate() {
799 if i > 0 || sig.receiver.is_some() {
800 write!(out, ", ");
801 }
802 match &arg.ty {
803 Type::Str(_) => write!(out, "::rust::Str::Repr("),
804 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
805 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
806 _ => {}
807 }
808 write!(out, "{}", arg.ident);
809 match &arg.ty {
810 Type::RustBox(_) => write!(out, ".into_raw()"),
811 Type::UniquePtr(_) => write!(out, ".release()"),
812 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
813 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
814 _ => {}
815 }
816 }
817 if indirect_return {
818 if !sig.args.is_empty() {
819 write!(out, ", ");
820 }
821 write!(out, "&return$.value");
822 }
823 if indirect_call {
824 if !sig.args.is_empty() || indirect_return {
825 write!(out, ", ");
826 }
827 write!(out, "extern$");
828 }
829 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400830 if !indirect_return {
831 if let Some(ret) = &sig.ret {
832 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
833 write!(out, ")");
834 }
David Tolnay439cde22020-04-20 00:46:25 -0700835 }
836 }
837 writeln!(out, ";");
838 if sig.throws {
839 writeln!(out, " if (error$.ptr) {{");
840 writeln!(out, " throw ::rust::Error(error$);");
841 writeln!(out, " }}");
842 }
843 if indirect_return {
844 out.include.utility = true;
845 writeln!(out, " return ::std::move(return$.value);");
846 }
847 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400848}
849
850fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
851 match ty {
852 None => write!(out, "void "),
853 Some(ty) => write_type_space(out, ty),
854 }
855}
856
David Tolnay75dca2e2020-03-25 20:17:52 -0700857fn indirect_return(sig: &Signature, types: &Types) -> bool {
858 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700859 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700860 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700861}
862
David Tolnay99642622020-03-25 13:07:35 -0700863fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
864 match ty {
865 Type::RustBox(ty) | Type::UniquePtr(ty) => {
866 write_type_space(out, &ty.inner);
867 write!(out, "*");
868 }
869 Type::Ref(ty) => {
870 if ty.mutability.is_none() {
871 write!(out, "const ");
872 }
873 write_type(out, &ty.inner);
874 write!(out, " *");
875 }
876 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700877 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700878 _ => write_type(out, ty),
879 }
880}
881
882fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
883 write_indirect_return_type(out, ty);
884 match ty {
885 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700886 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700887 _ => write_space_after_type(out, ty),
888 }
889}
890
891fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400892 match ty {
893 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
894 write_type_space(out, &ty.inner);
895 write!(out, "*");
896 }
David Tolnay4a441222020-01-25 16:24:27 -0800897 Some(Type::Ref(ty)) => {
898 if ty.mutability.is_none() {
899 write!(out, "const ");
900 }
901 write_type(out, &ty.inner);
902 write!(out, " *");
903 }
David Tolnay750755e2020-03-01 13:04:08 -0800904 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700905 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400906 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
907 _ => write_return_type(out, ty),
908 }
909}
910
911fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
912 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700913 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400914 write_type_space(out, &ty.inner);
915 write!(out, "*");
916 }
David Tolnay750755e2020-03-01 13:04:08 -0800917 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700918 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400919 _ => write_type_space(out, &arg.ty),
920 }
921 if types.needs_indirect_abi(&arg.ty) {
922 write!(out, "*");
923 }
924 write!(out, "{}", arg.ident);
925}
926
927fn write_type(out: &mut OutFile, ty: &Type) {
928 match ty {
929 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700930 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400931 None => write!(out, "{}", ident),
932 },
933 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800934 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400935 write_type(out, &ty.inner);
936 write!(out, ">");
937 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700938 Type::RustVec(ty) => {
939 write!(out, "::rust::Vec<");
940 write_type(out, &ty.inner);
941 write!(out, ">");
942 }
David Tolnay7db73692019-10-20 14:51:12 -0400943 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800944 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400945 write_type(out, &ptr.inner);
946 write!(out, ">");
947 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700948 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700949 write!(out, "::std::vector<");
950 write_type(out, &ty.inner);
951 write!(out, ">");
952 }
David Tolnay7db73692019-10-20 14:51:12 -0400953 Type::Ref(r) => {
954 if r.mutability.is_none() {
955 write!(out, "const ");
956 }
957 write_type(out, &r.inner);
958 write!(out, " &");
959 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700960 Type::Slice(_) => {
961 // For now, only U8 slices are supported, which are covered separately below
962 unreachable!()
963 }
David Tolnay7db73692019-10-20 14:51:12 -0400964 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800965 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400966 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700967 Type::SliceRefU8(_) => {
968 write!(out, "::rust::Slice<uint8_t>");
969 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700970 Type::Fn(f) => {
971 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
972 match &f.ret {
973 Some(ret) => write_type(out, ret),
974 None => write!(out, "void"),
975 }
976 write!(out, "(");
977 for (i, arg) in f.args.iter().enumerate() {
978 if i > 0 {
979 write!(out, ", ");
980 }
981 write_type(out, &arg.ty);
982 }
983 write!(out, ")>");
984 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700985 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400986 }
987}
988
David Tolnayf6a89f22020-05-10 23:39:27 -0700989fn write_atom(out: &mut OutFile, atom: Atom) {
990 match atom {
991 Bool => write!(out, "bool"),
992 U8 => write!(out, "uint8_t"),
993 U16 => write!(out, "uint16_t"),
994 U32 => write!(out, "uint32_t"),
995 U64 => write!(out, "uint64_t"),
996 Usize => write!(out, "size_t"),
997 I8 => write!(out, "int8_t"),
998 I16 => write!(out, "int16_t"),
999 I32 => write!(out, "int32_t"),
1000 I64 => write!(out, "int64_t"),
1001 Isize => write!(out, "::rust::isize"),
1002 F32 => write!(out, "float"),
1003 F64 => write!(out, "double"),
1004 CxxString => write!(out, "::std::string"),
1005 RustString => write!(out, "::rust::String"),
1006 }
1007}
1008
David Tolnay7db73692019-10-20 14:51:12 -04001009fn write_type_space(out: &mut OutFile, ty: &Type) {
1010 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001011 write_space_after_type(out, ty);
1012}
1013
1014fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001015 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001016 Type::Ident(_)
1017 | Type::RustBox(_)
1018 | Type::UniquePtr(_)
1019 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001020 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001021 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001022 | Type::SliceRefU8(_)
1023 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001024 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001025 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001026 }
1027}
1028
David Tolnaycd08c442020-04-25 10:16:33 -07001029// Only called for legal referent types of unique_ptr and element types of
1030// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -07001031fn to_typename(namespace: &Namespace, ty: &Type) -> String {
1032 match ty {
1033 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -07001034 let mut path = String::new();
1035 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001036 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -07001037 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001038 }
David Tolnaycd08c442020-04-25 10:16:33 -07001039 path += &ident.to_string();
1040 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001041 }
1042 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001043 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001044 }
1045}
1046
David Tolnayacdf20a2020-04-25 12:40:53 -07001047// Only called for legal referent types of unique_ptr and element types of
1048// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001049fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1050 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001051 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001052 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001053 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001054 }
1055}
1056
David Tolnay7db73692019-10-20 14:51:12 -04001057fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001058 out.begin_block("extern \"C\"");
1059 for ty in types {
1060 if let Type::RustBox(ty) = ty {
1061 if let Type::Ident(inner) = &ty.inner {
1062 out.next_section();
1063 write_rust_box_extern(out, inner);
1064 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001065 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001066 if let Type::Ident(inner) = &ty.inner {
1067 if Atom::from(inner).is_none() {
1068 out.next_section();
1069 write_rust_vec_extern(out, inner);
1070 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001071 }
David Tolnay7db73692019-10-20 14:51:12 -04001072 } else if let Type::UniquePtr(ptr) = ty {
1073 if let Type::Ident(inner) = &ptr.inner {
David Tolnay7e69f892020-10-03 22:20:22 -07001074 if Atom::from(inner).is_none()
1075 && (!types.aliases.contains_key(inner) || types.explicit_impls.contains(ty))
1076 {
David Tolnay7db73692019-10-20 14:51:12 -04001077 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001078 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001079 }
1080 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001081 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001082 if let Type::Ident(inner) = &ptr.inner {
David Tolnay7e69f892020-10-03 22:20:22 -07001083 if Atom::from(inner).is_none()
1084 && (!types.aliases.contains_key(inner) || types.explicit_impls.contains(ty))
1085 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001086 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001087 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001088 }
1089 }
1090 }
1091 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001092 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001093
David Tolnay750755e2020-03-01 13:04:08 -08001094 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001095 out.begin_block("inline namespace cxxbridge05");
David Tolnay7db73692019-10-20 14:51:12 -04001096 for ty in types {
1097 if let Type::RustBox(ty) = ty {
1098 if let Type::Ident(inner) = &ty.inner {
1099 write_rust_box_impl(out, inner);
1100 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001101 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001102 if let Type::Ident(inner) = &ty.inner {
1103 if Atom::from(inner).is_none() {
1104 write_rust_vec_impl(out, inner);
1105 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001106 }
David Tolnay7db73692019-10-20 14:51:12 -04001107 }
1108 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001109 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001110 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001111}
1112
1113fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1114 let mut inner = String::new();
1115 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001116 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001117 inner += "::";
1118 }
1119 inner += &ident.to_string();
1120 let instance = inner.replace("::", "$");
1121
David Tolnay8f16ae72020-10-08 18:21:13 -07001122 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1123 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001124 writeln!(
1125 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001126 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001127 instance, inner,
1128 );
1129 writeln!(
1130 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001131 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001132 instance, inner,
1133 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001134 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001135}
1136
David Tolnay6787be62020-04-25 11:01:02 -07001137fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1138 let element = Type::Ident(element.clone());
1139 let inner = to_typename(&out.namespace, &element);
1140 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001141
David Tolnay8f16ae72020-10-08 18:21:13 -07001142 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1143 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001144 writeln!(
1145 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001146 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001147 instance, inner,
1148 );
1149 writeln!(
1150 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001151 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001152 instance, inner,
1153 );
1154 writeln!(
1155 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001156 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001157 instance, inner,
1158 );
David Tolnay219c0792020-04-24 20:31:37 -07001159 writeln!(
1160 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001161 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001162 inner, instance,
1163 );
David Tolnay503d0192020-04-24 22:18:56 -07001164 writeln!(
1165 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001166 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001167 instance,
1168 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001169 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001170}
1171
David Tolnay7db73692019-10-20 14:51:12 -04001172fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1173 let mut inner = String::new();
1174 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001175 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001176 inner += "::";
1177 }
1178 inner += &ident.to_string();
1179 let instance = inner.replace("::", "$");
1180
1181 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001182 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001183 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001184 writeln!(out, "}}");
1185
1186 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001187 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001188 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001189 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001190}
1191
David Tolnay6787be62020-04-25 11:01:02 -07001192fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1193 let element = Type::Ident(element.clone());
1194 let inner = to_typename(&out.namespace, &element);
1195 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001196
Myron Ahneba35cf2020-02-05 19:41:51 +07001197 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001198 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001199 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001200 writeln!(out, "}}");
1201
1202 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001203 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1204 writeln!(
1205 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001206 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001207 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001208 );
1209 writeln!(out, "}}");
1210
1211 writeln!(out, "template <>");
1212 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001213 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001214 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001215
1216 writeln!(out, "template <>");
1217 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1218 writeln!(
1219 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001220 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001221 instance,
1222 );
1223 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001224
1225 writeln!(out, "template <>");
1226 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001227 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001228 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001229}
1230
David Tolnay63da4d32020-04-25 09:41:12 -07001231fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1232 let ty = Type::Ident(ident.clone());
1233 let instance = to_mangled(&out.namespace, &ty);
1234
David Tolnay8f16ae72020-10-08 18:21:13 -07001235 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1236 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001237
1238 write_unique_ptr_common(out, &ty, types);
1239
David Tolnay8f16ae72020-10-08 18:21:13 -07001240 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001241}
1242
1243// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1244fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001245 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001246 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001247 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001248 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001249
David Tolnay63da4d32020-04-25 09:41:12 -07001250 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001251 // Some aliases are to opaque types; some are to trivial types. We can't
1252 // know at code generation time, so we generate both C++ and Rust side
1253 // bindings for a "new" method anyway. But the Rust code can't be called
1254 // for Opaque types because the 'new' method is not implemented.
1255 Type::Ident(ident) => {
1256 types.structs.contains_key(ident) || types.aliases.contains_key(ident)
1257 }
David Tolnay63da4d32020-04-25 09:41:12 -07001258 _ => false,
1259 };
1260
David Tolnay7db73692019-10-20 14:51:12 -04001261 writeln!(
1262 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001263 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001264 inner,
1265 );
1266 writeln!(
1267 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001268 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001269 inner,
1270 );
1271 writeln!(
1272 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001273 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001274 instance, inner,
1275 );
David Tolnay7e219b82020-03-01 13:14:51 -08001276 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001277 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001278 if can_construct_from_value {
1279 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001280 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001281 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001282 instance, inner, inner,
1283 );
David Tolnay63da4d32020-04-25 09:41:12 -07001284 writeln!(
1285 out,
1286 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1287 inner, inner,
1288 );
1289 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001290 }
David Tolnay7db73692019-10-20 14:51:12 -04001291 writeln!(
1292 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001293 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001294 instance, inner, inner,
1295 );
David Tolnay7e219b82020-03-01 13:14:51 -08001296 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001297 writeln!(out, "}}");
1298 writeln!(
1299 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001300 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001301 inner, instance, inner,
1302 );
1303 writeln!(out, " return ptr.get();");
1304 writeln!(out, "}}");
1305 writeln!(
1306 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001307 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001308 inner, instance, inner,
1309 );
1310 writeln!(out, " return ptr.release();");
1311 writeln!(out, "}}");
1312 writeln!(
1313 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001314 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001315 instance, inner,
1316 );
1317 writeln!(out, " ptr->~unique_ptr();");
1318 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001319}
Myron Ahneba35cf2020-02-05 19:41:51 +07001320
David Tolnay92105da2020-04-25 11:15:31 -07001321fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001322 let element = Type::Ident(element.clone());
1323 let inner = to_typename(&out.namespace, &element);
1324 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001325
David Tolnay8f16ae72020-10-08 18:21:13 -07001326 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1327 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001328 writeln!(
1329 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001330 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001331 instance, inner,
1332 );
1333 writeln!(out, " return s.size();");
1334 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001335 writeln!(
1336 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001337 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001338 inner, instance, inner,
1339 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001340 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001341 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001342
1343 write_unique_ptr_common(out, vector_ty, types);
1344
David Tolnay8f16ae72020-10-08 18:21:13 -07001345 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001346}