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