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