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