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