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