| David Tolnay | 754e21c | 2020-03-29 20:58:46 -0700 | [diff] [blame] | 1 | use crate::gen::namespace::Namespace; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 2 | use crate::gen::out::OutFile; |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 3 | use crate::gen::{include, Opt}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 4 | use crate::syntax::atom::Atom::{self, *}; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 5 | use crate::syntax::{Api, ExternFn, Signature, Struct, Type, Types, Var}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 6 | use proc_macro2::Ident; |
| 7 | |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 8 | pub(super) fn gen( |
| David Tolnay | 754e21c | 2020-03-29 20:58:46 -0700 | [diff] [blame] | 9 | namespace: Namespace, |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 10 | apis: &[Api], |
| 11 | types: &Types, |
| 12 | opt: Opt, |
| 13 | header: bool, |
| 14 | ) -> OutFile { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 15 | let mut out_file = OutFile::new(namespace.clone(), header); |
| 16 | let out = &mut out_file; |
| 17 | |
| 18 | if header { |
| 19 | writeln!(out, "#pragma once"); |
| 20 | } |
| 21 | |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 22 | out.include.extend(opt.include); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 23 | for api in apis { |
| 24 | if let Api::Include(include) = api { |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 25 | out.include.insert(include.value()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | write_includes(out, types); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 30 | write_include_cxxbridge(out, apis, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 31 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 32 | out.next_section(); |
| 33 | for name in &namespace { |
| 34 | writeln!(out, "namespace {} {{", name); |
| 35 | } |
| 36 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 37 | out.next_section(); |
| 38 | for api in apis { |
| 39 | match api { |
| 40 | Api::Struct(strct) => write_struct_decl(out, &strct.ident), |
| David Tolnay | 8861bee | 2020-01-20 18:39:24 -0800 | [diff] [blame] | 41 | Api::CxxType(ety) => write_struct_using(out, &ety.ident), |
| 42 | Api::RustType(ety) => write_struct_decl(out, &ety.ident), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 43 | _ => {} |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | for api in apis { |
| 48 | if let Api::Struct(strct) = api { |
| 49 | out.next_section(); |
| 50 | write_struct(out, strct); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if !header { |
| 55 | out.begin_block("extern \"C\""); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 56 | write_exception_glue(out, apis); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 57 | for api in apis { |
| 58 | let (efn, write): (_, fn(_, _, _)) = match api { |
| 59 | Api::CxxFunction(efn) => (efn, write_cxx_function_shim), |
| 60 | Api::RustFunction(efn) => (efn, write_rust_function_decl), |
| 61 | _ => continue, |
| 62 | }; |
| 63 | out.next_section(); |
| 64 | write(out, efn, types); |
| 65 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 66 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | for api in apis { |
| 70 | if let Api::RustFunction(efn) = api { |
| 71 | out.next_section(); |
| 72 | write_rust_function_shim(out, efn, types); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | out.next_section(); |
| 77 | for name in namespace.iter().rev() { |
| 78 | writeln!(out, "}} // namespace {}", name); |
| 79 | } |
| 80 | |
| 81 | if !header { |
| 82 | out.next_section(); |
| 83 | write_generic_instantiations(out, types); |
| 84 | } |
| 85 | |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 86 | out.prepend(out.include.to_string()); |
| 87 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 88 | out_file |
| 89 | } |
| 90 | |
| 91 | fn write_includes(out: &mut OutFile, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 92 | for ty in types { |
| 93 | match ty { |
| 94 | Type::Ident(ident) => match Atom::from(ident) { |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 95 | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32) |
| 96 | | Some(I64) => out.include.cstdint = true, |
| 97 | Some(Usize) => out.include.cstddef = true, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 98 | Some(CxxString) => out.include.string = true, |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 99 | Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 100 | }, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 101 | Type::RustBox(_) => out.include.type_traits = true, |
| 102 | Type::UniquePtr(_) => out.include.memory = true, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 103 | _ => {} |
| 104 | } |
| 105 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 108 | fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 109 | let mut needs_rust_string = false; |
| 110 | let mut needs_rust_str = false; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 111 | let mut needs_rust_box = false; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 112 | let mut needs_rust_fn = false; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 113 | for ty in types { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 114 | match ty { |
| 115 | Type::RustBox(_) => { |
| 116 | out.include.type_traits = true; |
| 117 | needs_rust_box = true; |
| 118 | } |
| 119 | Type::Str(_) => { |
| 120 | out.include.cstdint = true; |
| 121 | out.include.string = true; |
| 122 | needs_rust_str = true; |
| 123 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 124 | Type::Fn(_) => { |
| 125 | needs_rust_fn = true; |
| 126 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 127 | ty if ty == RustString => { |
| 128 | out.include.array = true; |
| 129 | out.include.cstdint = true; |
| 130 | out.include.string = true; |
| 131 | needs_rust_string = true; |
| 132 | } |
| 133 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 137 | let mut needs_rust_error = false; |
| 138 | let mut needs_unsafe_bitcopy = false; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 139 | let mut needs_manually_drop = false; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 140 | let mut needs_maybe_uninit = false; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 141 | let mut needs_trycatch = false; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 142 | for api in apis { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 143 | match api { |
| 144 | Api::CxxFunction(efn) if !out.header => { |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 145 | if efn.throws { |
| 146 | needs_trycatch = true; |
| 147 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 148 | for arg in &efn.args { |
| 149 | if arg.ty == RustString { |
| 150 | needs_unsafe_bitcopy = true; |
| 151 | break; |
| 152 | } |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 155 | Api::RustFunction(efn) if !out.header => { |
| 156 | if efn.throws { |
| 157 | out.include.exception = true; |
| 158 | needs_rust_error = true; |
| 159 | } |
| 160 | for arg in &efn.args { |
| 161 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| 162 | needs_manually_drop = true; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | if let Some(ret) = &efn.ret { |
| 167 | if types.needs_indirect_abi(ret) { |
| 168 | needs_maybe_uninit = true; |
| 169 | } |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 172 | _ => {} |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 176 | out.begin_block("namespace rust"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 177 | out.begin_block("inline namespace cxxbridge02"); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 178 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 179 | if needs_rust_string |
| 180 | || needs_rust_str |
| 181 | || needs_rust_box |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 182 | || needs_rust_fn |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 183 | || needs_rust_error |
| 184 | || needs_unsafe_bitcopy |
| 185 | || needs_manually_drop |
| 186 | || needs_maybe_uninit |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 187 | || needs_trycatch |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 188 | { |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 189 | writeln!(out, "// #include \"rust/cxx.h\""); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| David Tolnay | d140274 | 2020-03-25 22:21:42 -0700 | [diff] [blame] | 192 | if needs_rust_string { |
| 193 | out.next_section(); |
| 194 | writeln!(out, "struct unsafe_bitcopy_t;"); |
| 195 | } |
| 196 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 197 | write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING"); |
| 198 | write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR"); |
| 199 | write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 200 | write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN"); |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 201 | write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR"); |
| 202 | write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY"); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 203 | |
| 204 | if needs_manually_drop { |
| 205 | out.next_section(); |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 206 | out.include.utility = true; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 207 | writeln!(out, "template <typename T>"); |
| 208 | writeln!(out, "union ManuallyDrop {{"); |
| 209 | writeln!(out, " T value;"); |
| 210 | writeln!( |
| 211 | out, |
| 212 | " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}", |
| 213 | ); |
| 214 | writeln!(out, " ~ManuallyDrop() {{}}"); |
| 215 | writeln!(out, "}};"); |
| 216 | } |
| 217 | |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 218 | if needs_maybe_uninit { |
| 219 | out.next_section(); |
| 220 | writeln!(out, "template <typename T>"); |
| 221 | writeln!(out, "union MaybeUninit {{"); |
| 222 | writeln!(out, " T value;"); |
| 223 | writeln!(out, " MaybeUninit() {{}}"); |
| 224 | writeln!(out, " ~MaybeUninit() {{}}"); |
| 225 | writeln!(out, "}};"); |
| 226 | } |
| 227 | |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 228 | out.end_block("namespace cxxbridge02"); |
| 229 | |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 230 | if needs_trycatch { |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 231 | out.begin_block("namespace behavior"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 232 | out.include.exception = true; |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 233 | out.include.type_traits = true; |
| 234 | out.include.utility = true; |
| 235 | writeln!(out, "class missing {{}};"); |
| 236 | writeln!(out, "missing trycatch(...);"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 237 | writeln!(out); |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 238 | writeln!(out, "template <typename Try, typename Fail>"); |
| 239 | writeln!(out, "static typename std::enable_if<"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 240 | writeln!( |
| 241 | out, |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 242 | " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),", |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 243 | ); |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 244 | writeln!(out, " missing>::value>::type"); |
| 245 | writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 246 | writeln!(out, " func();"); |
| 247 | writeln!(out, "}} catch (const ::std::exception &e) {{"); |
| 248 | writeln!(out, " fail(e.what());"); |
| 249 | writeln!(out, "}}"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 250 | out.end_block("namespace behavior"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 253 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 256 | fn write_header_section(out: &mut OutFile, needed: bool, section: &str) { |
| 257 | if needed { |
| 258 | out.next_section(); |
| 259 | for line in include::get(section).lines() { |
| 260 | if !line.trim_start().starts_with("//") { |
| 261 | writeln!(out, "{}", line); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 267 | fn write_struct(out: &mut OutFile, strct: &Struct) { |
| 268 | for line in strct.doc.to_string().lines() { |
| 269 | writeln!(out, "//{}", line); |
| 270 | } |
| 271 | writeln!(out, "struct {} final {{", strct.ident); |
| 272 | for field in &strct.fields { |
| 273 | write!(out, " "); |
| 274 | write_type_space(out, &field.ty); |
| 275 | writeln!(out, "{};", field.ident); |
| 276 | } |
| 277 | writeln!(out, "}};"); |
| 278 | } |
| 279 | |
| 280 | fn write_struct_decl(out: &mut OutFile, ident: &Ident) { |
| 281 | writeln!(out, "struct {};", ident); |
| 282 | } |
| 283 | |
| David Tolnay | 8861bee | 2020-01-20 18:39:24 -0800 | [diff] [blame] | 284 | fn write_struct_using(out: &mut OutFile, ident: &Ident) { |
| 285 | writeln!(out, "using {} = {};", ident, ident); |
| 286 | } |
| 287 | |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 288 | fn write_exception_glue(out: &mut OutFile, apis: &[Api]) { |
| 289 | let mut has_cxx_throws = false; |
| 290 | for api in apis { |
| 291 | if let Api::CxxFunction(efn) = api { |
| 292 | if efn.throws { |
| 293 | has_cxx_throws = true; |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if has_cxx_throws { |
| 300 | out.next_section(); |
| David Tolnay | e68634c | 2020-03-18 12:03:40 -0700 | [diff] [blame] | 301 | writeln!( |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 302 | out, |
| 303 | "const char *cxxbridge02$exception(const char *, size_t);", |
| 304 | ); |
| 305 | } |
| 306 | } |
| 307 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 308 | fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 309 | if efn.throws { |
| 310 | write!(out, "::rust::Str::Repr "); |
| 311 | } else { |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 312 | write_extern_return_type_space(out, &efn.ret, types); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 313 | } |
| David Tolnay | d815de0 | 2020-03-29 21:29:17 -0700 | [diff] [blame] | 314 | write!(out, "{}cxxbridge02${}(", out.namespace, efn.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 315 | for (i, arg) in efn.args.iter().enumerate() { |
| 316 | if i > 0 { |
| 317 | write!(out, ", "); |
| 318 | } |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 319 | if arg.ty == RustString { |
| 320 | write!(out, "const "); |
| 321 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 322 | write_extern_arg(out, arg, types); |
| 323 | } |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 324 | let indirect_return = indirect_return(efn, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 325 | if indirect_return { |
| 326 | if !efn.args.is_empty() { |
| 327 | write!(out, ", "); |
| 328 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 329 | write_indirect_return_type_space(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 330 | write!(out, "*return$"); |
| 331 | } |
| 332 | writeln!(out, ") noexcept {{"); |
| 333 | write!(out, " "); |
| 334 | write_return_type(out, &efn.ret); |
| 335 | write!(out, "(*{}$)(", efn.ident); |
| 336 | for (i, arg) in efn.args.iter().enumerate() { |
| 337 | if i > 0 { |
| 338 | write!(out, ", "); |
| 339 | } |
| 340 | write_type(out, &arg.ty); |
| 341 | } |
| 342 | writeln!(out, ") = {};", efn.ident); |
| 343 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 344 | if efn.throws { |
| 345 | writeln!(out, "::rust::Str::Repr throw$;"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 346 | writeln!(out, " ::rust::behavior::trycatch("); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 347 | writeln!(out, " [&] {{"); |
| 348 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 349 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 350 | if indirect_return { |
| 351 | write!(out, "new (return$) "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 352 | write_indirect_return_type(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 353 | write!(out, "("); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 354 | } else if efn.ret.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 355 | write!(out, "return "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 356 | } |
| 357 | match &efn.ret { |
| 358 | Some(Type::Ref(_)) => write!(out, "&"), |
| 359 | Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("), |
| 360 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 361 | } |
| 362 | write!(out, "{}$(", efn.ident); |
| 363 | for (i, arg) in efn.args.iter().enumerate() { |
| 364 | if i > 0 { |
| 365 | write!(out, ", "); |
| 366 | } |
| 367 | if let Type::RustBox(_) = &arg.ty { |
| 368 | write_type(out, &arg.ty); |
| 369 | write!(out, "::from_raw({})", arg.ident); |
| 370 | } else if let Type::UniquePtr(_) = &arg.ty { |
| 371 | write_type(out, &arg.ty); |
| 372 | write!(out, "({})", arg.ident); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 373 | } else if arg.ty == RustString { |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 374 | write!( |
| 375 | out, |
| 376 | "::rust::String(::rust::unsafe_bitcopy, *{})", |
| 377 | arg.ident, |
| 378 | ); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 379 | } else if types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 380 | out.include.utility = true; |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 381 | write!(out, "::std::move(*{})", arg.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 382 | } else { |
| 383 | write!(out, "{}", arg.ident); |
| 384 | } |
| 385 | } |
| 386 | write!(out, ")"); |
| 387 | match &efn.ret { |
| 388 | Some(Type::RustBox(_)) => write!(out, ".into_raw()"), |
| 389 | Some(Type::UniquePtr(_)) => write!(out, ".release()"), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 390 | Some(Type::Str(_)) if !indirect_return => write!(out, ")"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 391 | _ => {} |
| 392 | } |
| 393 | if indirect_return { |
| 394 | write!(out, ")"); |
| 395 | } |
| 396 | writeln!(out, ";"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 397 | if efn.throws { |
| 398 | out.include.cstring = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 399 | writeln!(out, " throw$.ptr = nullptr;"); |
| 400 | writeln!(out, " }},"); |
| David Tolnay | 82c1617 | 2020-03-17 22:54:12 -0700 | [diff] [blame] | 401 | writeln!(out, " [&](const char *catch$) noexcept {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 402 | writeln!(out, " throw$.len = ::std::strlen(catch$);"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 403 | writeln!( |
| 404 | out, |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 405 | " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 406 | ); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 407 | writeln!(out, " }});"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 408 | writeln!(out, " return throw$;"); |
| 409 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 410 | writeln!(out, "}}"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 411 | for arg in &efn.args { |
| 412 | if let Type::Fn(f) = &arg.ty { |
| 413 | let var = &arg.ident; |
| 414 | write_function_pointer_trampoline(out, efn, var, f, types); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | fn write_function_pointer_trampoline( |
| 420 | out: &mut OutFile, |
| 421 | efn: &ExternFn, |
| 422 | var: &Ident, |
| 423 | f: &Signature, |
| 424 | types: &Types, |
| 425 | ) { |
| 426 | out.next_section(); |
| 427 | let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var); |
| 428 | let indirect_call = true; |
| 429 | write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call); |
| 430 | |
| 431 | out.next_section(); |
| 432 | let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var); |
| 433 | write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 437 | let link_name = format!("{}cxxbridge02${}", out.namespace, efn.ident); |
| 438 | let indirect_call = false; |
| 439 | write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call); |
| 440 | } |
| 441 | |
| 442 | fn write_rust_function_decl_impl( |
| 443 | out: &mut OutFile, |
| 444 | link_name: &str, |
| 445 | sig: &Signature, |
| 446 | types: &Types, |
| 447 | indirect_call: bool, |
| 448 | ) { |
| 449 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 450 | write!(out, "::rust::Str::Repr "); |
| 451 | } else { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 452 | write_extern_return_type_space(out, &sig.ret, types); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 453 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 454 | write!(out, "{}(", link_name); |
| 455 | let mut needs_comma = false; |
| 456 | for arg in &sig.args { |
| 457 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 458 | write!(out, ", "); |
| 459 | } |
| 460 | write_extern_arg(out, arg, types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 461 | needs_comma = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 462 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 463 | if indirect_return(sig, types) { |
| 464 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 465 | write!(out, ", "); |
| 466 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 467 | write_return_type(out, &sig.ret); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 468 | write!(out, "*return$"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 469 | needs_comma = true; |
| 470 | } |
| 471 | if indirect_call { |
| 472 | if needs_comma { |
| 473 | write!(out, ", "); |
| 474 | } |
| 475 | write!(out, "void *"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 476 | } |
| 477 | writeln!(out, ") noexcept;"); |
| 478 | } |
| 479 | |
| 480 | fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 481 | for line in efn.doc.to_string().lines() { |
| 482 | writeln!(out, "//{}", line); |
| 483 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 484 | let local_name = efn.ident.to_string(); |
| 485 | let invoke = format!("{}cxxbridge02${}", out.namespace, efn.ident); |
| 486 | let indirect_call = false; |
| 487 | write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call); |
| 488 | } |
| 489 | |
| 490 | fn write_rust_function_shim_impl( |
| 491 | out: &mut OutFile, |
| 492 | local_name: &str, |
| 493 | sig: &Signature, |
| 494 | types: &Types, |
| 495 | invoke: &str, |
| 496 | indirect_call: bool, |
| 497 | ) { |
| 498 | write_return_type(out, &sig.ret); |
| 499 | write!(out, "{}(", local_name); |
| 500 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 501 | if i > 0 { |
| 502 | write!(out, ", "); |
| 503 | } |
| 504 | write_type_space(out, &arg.ty); |
| 505 | write!(out, "{}", arg.ident); |
| 506 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 507 | if indirect_call { |
| 508 | if !sig.args.is_empty() { |
| 509 | write!(out, ", "); |
| 510 | } |
| 511 | write!(out, "void *extern$"); |
| 512 | } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 513 | write!(out, ")"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 514 | if !sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 515 | write!(out, " noexcept"); |
| 516 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 517 | if out.header { |
| 518 | writeln!(out, ";"); |
| 519 | } else { |
| 520 | writeln!(out, " {{"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 521 | for arg in &sig.args { |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 522 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 523 | out.include.utility = true; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 524 | write!(out, " ::rust::ManuallyDrop<"); |
| 525 | write_type(out, &arg.ty); |
| 526 | writeln!(out, "> {}$(::std::move({0}));", arg.ident); |
| 527 | } |
| 528 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 529 | write!(out, " "); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 530 | let indirect_return = indirect_return(sig, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 531 | if indirect_return { |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 532 | write!(out, "::rust::MaybeUninit<"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 533 | write_type(out, sig.ret.as_ref().unwrap()); |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 534 | writeln!(out, "> return$;"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 535 | write!(out, " "); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 536 | } else if let Some(ret) = &sig.ret { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 537 | write!(out, "return "); |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 538 | match ret { |
| 539 | Type::RustBox(_) => { |
| 540 | write_type(out, ret); |
| 541 | write!(out, "::from_raw("); |
| 542 | } |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 543 | Type::UniquePtr(_) => { |
| 544 | write_type(out, ret); |
| 545 | write!(out, "("); |
| 546 | } |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 547 | Type::Ref(_) => write!(out, "*"), |
| 548 | _ => {} |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 549 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 550 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 551 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 552 | write!(out, "::rust::Str::Repr error$ = "); |
| 553 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 554 | write!(out, "{}(", invoke); |
| 555 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 556 | if i > 0 { |
| 557 | write!(out, ", "); |
| 558 | } |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 559 | match &arg.ty { |
| 560 | Type::Str(_) => write!(out, "::rust::Str::Repr("), |
| 561 | ty if types.needs_indirect_abi(ty) => write!(out, "&"), |
| 562 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 563 | } |
| 564 | write!(out, "{}", arg.ident); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 565 | match &arg.ty { |
| David Tolnay | 17955e2 | 2020-01-20 17:58:24 -0800 | [diff] [blame] | 566 | Type::RustBox(_) => write!(out, ".into_raw()"), |
| 567 | Type::UniquePtr(_) => write!(out, ".release()"), |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 568 | Type::Str(_) => write!(out, ")"), |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 569 | ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"), |
| David Tolnay | 17955e2 | 2020-01-20 17:58:24 -0800 | [diff] [blame] | 570 | _ => {} |
| 571 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 572 | } |
| 573 | if indirect_return { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 574 | if !sig.args.is_empty() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 575 | write!(out, ", "); |
| 576 | } |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 577 | write!(out, "&return$.value"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 578 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 579 | if indirect_call { |
| 580 | if !sig.args.is_empty() || indirect_return { |
| 581 | write!(out, ", "); |
| 582 | } |
| 583 | write!(out, "extern$"); |
| 584 | } |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 585 | write!(out, ")"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 586 | if let Some(ret) = &sig.ret { |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 587 | if let Type::RustBox(_) | Type::UniquePtr(_) = ret { |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 588 | write!(out, ")"); |
| 589 | } |
| 590 | } |
| 591 | writeln!(out, ";"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 592 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 593 | writeln!(out, " if (error$.ptr) {{"); |
| 594 | writeln!(out, " throw ::rust::Error(error$);"); |
| 595 | writeln!(out, " }}"); |
| 596 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 597 | if indirect_return { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 598 | out.include.utility = true; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 599 | writeln!(out, " return ::std::move(return$.value);"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 600 | } |
| 601 | writeln!(out, "}}"); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | fn write_return_type(out: &mut OutFile, ty: &Option<Type>) { |
| 606 | match ty { |
| 607 | None => write!(out, "void "), |
| 608 | Some(ty) => write_type_space(out, ty), |
| 609 | } |
| 610 | } |
| 611 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 612 | fn indirect_return(sig: &Signature, types: &Types) -> bool { |
| 613 | sig.ret |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 614 | .as_ref() |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 615 | .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret)) |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 618 | fn write_indirect_return_type(out: &mut OutFile, ty: &Type) { |
| 619 | match ty { |
| 620 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 621 | write_type_space(out, &ty.inner); |
| 622 | write!(out, "*"); |
| 623 | } |
| 624 | Type::Ref(ty) => { |
| 625 | if ty.mutability.is_none() { |
| 626 | write!(out, "const "); |
| 627 | } |
| 628 | write_type(out, &ty.inner); |
| 629 | write!(out, " *"); |
| 630 | } |
| 631 | Type::Str(_) => write!(out, "::rust::Str::Repr"), |
| 632 | _ => write_type(out, ty), |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) { |
| 637 | write_indirect_return_type(out, ty); |
| 638 | match ty { |
| 639 | Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {} |
| 640 | Type::Str(_) => write!(out, " "), |
| 641 | _ => write_space_after_type(out, ty), |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 646 | match ty { |
| 647 | Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => { |
| 648 | write_type_space(out, &ty.inner); |
| 649 | write!(out, "*"); |
| 650 | } |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 651 | Some(Type::Ref(ty)) => { |
| 652 | if ty.mutability.is_none() { |
| 653 | write!(out, "const "); |
| 654 | } |
| 655 | write_type(out, &ty.inner); |
| 656 | write!(out, " *"); |
| 657 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 658 | Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 659 | Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "), |
| 660 | _ => write_return_type(out, ty), |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) { |
| 665 | match &arg.ty { |
| 666 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 667 | write_type_space(out, &ty.inner); |
| 668 | write!(out, "*"); |
| 669 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 670 | Type::Str(_) => write!(out, "::rust::Str::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 671 | _ => write_type_space(out, &arg.ty), |
| 672 | } |
| 673 | if types.needs_indirect_abi(&arg.ty) { |
| 674 | write!(out, "*"); |
| 675 | } |
| 676 | write!(out, "{}", arg.ident); |
| 677 | } |
| 678 | |
| 679 | fn write_type(out: &mut OutFile, ty: &Type) { |
| 680 | match ty { |
| 681 | Type::Ident(ident) => match Atom::from(ident) { |
| 682 | Some(Bool) => write!(out, "bool"), |
| 683 | Some(U8) => write!(out, "uint8_t"), |
| 684 | Some(U16) => write!(out, "uint16_t"), |
| 685 | Some(U32) => write!(out, "uint32_t"), |
| 686 | Some(U64) => write!(out, "uint64_t"), |
| 687 | Some(Usize) => write!(out, "size_t"), |
| 688 | Some(I8) => write!(out, "int8_t"), |
| 689 | Some(I16) => write!(out, "int16_t"), |
| 690 | Some(I32) => write!(out, "int32_t"), |
| 691 | Some(I64) => write!(out, "int64_t"), |
| 692 | Some(Isize) => write!(out, "ssize_t"), |
| David Tolnay | 3383ae7 | 2020-03-13 01:12:26 -0700 | [diff] [blame] | 693 | Some(F32) => write!(out, "float"), |
| 694 | Some(F64) => write!(out, "double"), |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 695 | Some(CxxString) => write!(out, "::std::string"), |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 696 | Some(RustString) => write!(out, "::rust::String"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 697 | None => write!(out, "{}", ident), |
| 698 | }, |
| 699 | Type::RustBox(ty) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 700 | write!(out, "::rust::Box<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 701 | write_type(out, &ty.inner); |
| 702 | write!(out, ">"); |
| 703 | } |
| 704 | Type::UniquePtr(ptr) => { |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 705 | write!(out, "::std::unique_ptr<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 706 | write_type(out, &ptr.inner); |
| 707 | write!(out, ">"); |
| 708 | } |
| 709 | Type::Ref(r) => { |
| 710 | if r.mutability.is_none() { |
| 711 | write!(out, "const "); |
| 712 | } |
| 713 | write_type(out, &r.inner); |
| 714 | write!(out, " &"); |
| 715 | } |
| 716 | Type::Str(_) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 717 | write!(out, "::rust::Str"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 718 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 719 | Type::Fn(f) => { |
| 720 | write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" }); |
| 721 | match &f.ret { |
| 722 | Some(ret) => write_type(out, ret), |
| 723 | None => write!(out, "void"), |
| 724 | } |
| 725 | write!(out, "("); |
| 726 | for (i, arg) in f.args.iter().enumerate() { |
| 727 | if i > 0 { |
| 728 | write!(out, ", "); |
| 729 | } |
| 730 | write_type(out, &arg.ty); |
| 731 | } |
| 732 | write!(out, ")>"); |
| 733 | } |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 734 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
| 738 | fn write_type_space(out: &mut OutFile, ty: &Type) { |
| 739 | write_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 740 | write_space_after_type(out, ty); |
| 741 | } |
| 742 | |
| 743 | fn write_space_after_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 744 | match ty { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame^] | 745 | Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::Fn(_) => { |
| 746 | write!(out, " ") |
| 747 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 748 | Type::Ref(_) => {} |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 749 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | fn write_generic_instantiations(out: &mut OutFile, types: &Types) { |
| 754 | fn allow_unique_ptr(ident: &Ident) -> bool { |
| 755 | Atom::from(ident).is_none() |
| 756 | } |
| 757 | |
| 758 | out.begin_block("extern \"C\""); |
| 759 | for ty in types { |
| 760 | if let Type::RustBox(ty) = ty { |
| 761 | if let Type::Ident(inner) = &ty.inner { |
| 762 | out.next_section(); |
| 763 | write_rust_box_extern(out, inner); |
| 764 | } |
| 765 | } else if let Type::UniquePtr(ptr) = ty { |
| 766 | if let Type::Ident(inner) = &ptr.inner { |
| 767 | if allow_unique_ptr(inner) { |
| 768 | out.next_section(); |
| 769 | write_unique_ptr(out, inner); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 774 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 775 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 776 | out.begin_block("namespace rust"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 777 | out.begin_block("inline namespace cxxbridge02"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 778 | for ty in types { |
| 779 | if let Type::RustBox(ty) = ty { |
| 780 | if let Type::Ident(inner) = &ty.inner { |
| 781 | write_rust_box_impl(out, inner); |
| 782 | } |
| 783 | } |
| 784 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 785 | out.end_block("namespace cxxbridge02"); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 786 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) { |
| 790 | let mut inner = String::new(); |
| 791 | for name in &out.namespace { |
| 792 | inner += name; |
| 793 | inner += "::"; |
| 794 | } |
| 795 | inner += &ident.to_string(); |
| 796 | let instance = inner.replace("::", "$"); |
| 797 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 798 | writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance); |
| 799 | writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 800 | writeln!( |
| 801 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 802 | "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 803 | instance, inner, |
| 804 | ); |
| 805 | writeln!( |
| 806 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 807 | "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 808 | instance, inner, |
| 809 | ); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 810 | writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) { |
| 814 | let mut inner = String::new(); |
| 815 | for name in &out.namespace { |
| 816 | inner += name; |
| 817 | inner += "::"; |
| 818 | } |
| 819 | inner += &ident.to_string(); |
| 820 | let instance = inner.replace("::", "$"); |
| 821 | |
| 822 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 823 | writeln!(out, "void Box<{}>::uninit() noexcept {{", inner); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 824 | writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 825 | writeln!(out, "}}"); |
| 826 | |
| 827 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 828 | writeln!(out, "void Box<{}>::drop() noexcept {{", inner); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 829 | writeln!(out, " return cxxbridge02$box${}$drop(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 830 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | fn write_unique_ptr(out: &mut OutFile, ident: &Ident) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 834 | out.include.utility = true; |
| 835 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 836 | let mut inner = String::new(); |
| 837 | for name in &out.namespace { |
| 838 | inner += name; |
| 839 | inner += "::"; |
| 840 | } |
| 841 | inner += &ident.to_string(); |
| 842 | let instance = inner.replace("::", "$"); |
| 843 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 844 | writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| 845 | writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 846 | writeln!( |
| 847 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 848 | "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 849 | inner, |
| 850 | ); |
| 851 | writeln!( |
| 852 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 853 | "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 854 | inner, |
| 855 | ); |
| 856 | writeln!( |
| 857 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 858 | "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 859 | instance, inner, |
| 860 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 861 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 862 | writeln!(out, "}}"); |
| 863 | writeln!( |
| 864 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 865 | "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 866 | instance, inner, inner, |
| 867 | ); |
| 868 | writeln!( |
| 869 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 870 | " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 871 | inner, inner, |
| 872 | ); |
| 873 | writeln!(out, "}}"); |
| 874 | writeln!( |
| 875 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 876 | "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 877 | instance, inner, inner, |
| 878 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 879 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 880 | writeln!(out, "}}"); |
| 881 | writeln!( |
| 882 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 883 | "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 884 | inner, instance, inner, |
| 885 | ); |
| 886 | writeln!(out, " return ptr.get();"); |
| 887 | writeln!(out, "}}"); |
| 888 | writeln!( |
| 889 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 890 | "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 891 | inner, instance, inner, |
| 892 | ); |
| 893 | writeln!(out, " return ptr.release();"); |
| 894 | writeln!(out, "}}"); |
| 895 | writeln!( |
| 896 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 897 | "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 898 | instance, inner, |
| 899 | ); |
| 900 | writeln!(out, " ptr->~unique_ptr();"); |
| 901 | writeln!(out, "}}"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 902 | writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 903 | } |