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