| David Tolnay | 754e21c | 2020-03-29 20:58:46 -0700 | [diff] [blame] | 1 | use crate::gen::namespace::Namespace; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 2 | use crate::gen::out::OutFile; |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 3 | use crate::gen::{include, Opt}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 4 | use crate::syntax::atom::Atom::{self, *}; |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 5 | use crate::syntax::{Api, ExternFn, ExternType, Receiver, Signature, Struct, Type, Types, Var}; |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 6 | use itertools::Itertools; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 7 | use proc_macro2::Ident; |
| 8 | |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 9 | pub(super) fn gen( |
| David Tolnay | 754e21c | 2020-03-29 20:58:46 -0700 | [diff] [blame] | 10 | namespace: Namespace, |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 11 | apis: &[Api], |
| 12 | types: &Types, |
| 13 | opt: Opt, |
| 14 | header: bool, |
| 15 | ) -> OutFile { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 16 | let mut out_file = OutFile::new(namespace.clone(), header); |
| 17 | let out = &mut out_file; |
| 18 | |
| 19 | if header { |
| 20 | writeln!(out, "#pragma once"); |
| 21 | } |
| 22 | |
| David Tolnay | 33d3029 | 2020-03-18 18:02:02 -0700 | [diff] [blame] | 23 | out.include.extend(opt.include); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 24 | for api in apis { |
| 25 | if let Api::Include(include) = api { |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 26 | out.include.insert(include.value()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
| 30 | write_includes(out, types); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 31 | write_include_cxxbridge(out, apis, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 32 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 33 | out.next_section(); |
| 34 | for name in &namespace { |
| 35 | writeln!(out, "namespace {} {{", name); |
| 36 | } |
| 37 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 38 | out.next_section(); |
| 39 | for api in apis { |
| 40 | match api { |
| 41 | Api::Struct(strct) => write_struct_decl(out, &strct.ident), |
| David Tolnay | 8861bee | 2020-01-20 18:39:24 -0800 | [diff] [blame] | 42 | Api::CxxType(ety) => write_struct_using(out, &ety.ident), |
| 43 | Api::RustType(ety) => write_struct_decl(out, &ety.ident), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 44 | _ => {} |
| 45 | } |
| 46 | } |
| 47 | |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 48 | let methods_for_type = apis.iter().filter_map(|api| match api { |
| 49 | Api::RustFunction(efn) => match &efn.sig.receiver { |
| 50 | Some(rcvr) => Some((&rcvr.ident, efn)), |
| 51 | _ => None, |
| 52 | }, |
| 53 | _ => None, |
| 54 | }).into_group_map(); |
| 55 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 56 | for api in apis { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 57 | match api { |
| 58 | Api::Struct(strct) => { |
| 59 | out.next_section(); |
| 60 | write_struct(out, strct); |
| 61 | } |
| 62 | Api::RustType(ety) => { |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 63 | match methods_for_type.get(&ety.ident) { |
| 64 | Some(methods) => { |
| 65 | out.next_section(); |
| 66 | write_struct_with_methods(out, ety, methods); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 67 | }, |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 68 | _ => {} |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | if !header { |
| 76 | out.begin_block("extern \"C\""); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 77 | write_exception_glue(out, apis); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 78 | for api in apis { |
| 79 | let (efn, write): (_, fn(_, _, _)) = match api { |
| 80 | Api::CxxFunction(efn) => (efn, write_cxx_function_shim), |
| 81 | Api::RustFunction(efn) => (efn, write_rust_function_decl), |
| 82 | _ => continue, |
| 83 | }; |
| 84 | out.next_section(); |
| 85 | write(out, efn, types); |
| 86 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 87 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | for api in apis { |
| 91 | if let Api::RustFunction(efn) = api { |
| 92 | out.next_section(); |
| 93 | write_rust_function_shim(out, efn, types); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | out.next_section(); |
| 98 | for name in namespace.iter().rev() { |
| 99 | writeln!(out, "}} // namespace {}", name); |
| 100 | } |
| 101 | |
| 102 | if !header { |
| 103 | out.next_section(); |
| 104 | write_generic_instantiations(out, types); |
| 105 | } |
| 106 | |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 107 | out.prepend(out.include.to_string()); |
| 108 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 109 | out_file |
| 110 | } |
| 111 | |
| 112 | fn write_includes(out: &mut OutFile, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 113 | for ty in types { |
| 114 | match ty { |
| 115 | Type::Ident(ident) => match Atom::from(ident) { |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 116 | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32) |
| 117 | | Some(I64) => out.include.cstdint = true, |
| 118 | Some(Usize) => out.include.cstddef = true, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 119 | Some(CxxString) => out.include.string = true, |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 120 | Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 121 | }, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 122 | Type::RustBox(_) => out.include.type_traits = true, |
| 123 | Type::UniquePtr(_) => out.include.memory = true, |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 124 | Type::SliceRefU8(_) => out.include.cstdint = true, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 125 | _ => {} |
| 126 | } |
| 127 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 130 | fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 131 | let mut needs_rust_string = false; |
| 132 | let mut needs_rust_str = false; |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 133 | let mut needs_rust_slice = false; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 134 | let mut needs_rust_box = false; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 135 | let mut needs_rust_fn = false; |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 136 | let mut needs_rust_isize = false; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 137 | for ty in types { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 138 | match ty { |
| 139 | Type::RustBox(_) => { |
| 140 | out.include.type_traits = true; |
| 141 | needs_rust_box = true; |
| 142 | } |
| 143 | Type::Str(_) => { |
| 144 | out.include.cstdint = true; |
| 145 | out.include.string = true; |
| 146 | needs_rust_str = true; |
| 147 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 148 | Type::Fn(_) => { |
| 149 | needs_rust_fn = true; |
| 150 | } |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 151 | Type::Slice(_) | Type::SliceRefU8(_) => { |
| 152 | needs_rust_slice = true; |
| 153 | } |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 154 | ty if ty == Isize => { |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 155 | out.include.base_tsd = true; |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 156 | needs_rust_isize = true; |
| 157 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 158 | ty if ty == RustString => { |
| 159 | out.include.array = true; |
| 160 | out.include.cstdint = true; |
| 161 | out.include.string = true; |
| 162 | needs_rust_string = true; |
| 163 | } |
| 164 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 168 | let mut needs_rust_error = false; |
| 169 | let mut needs_unsafe_bitcopy = false; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 170 | let mut needs_manually_drop = false; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 171 | let mut needs_maybe_uninit = false; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 172 | let mut needs_trycatch = false; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 173 | for api in apis { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 174 | match api { |
| 175 | Api::CxxFunction(efn) if !out.header => { |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 176 | if efn.throws { |
| 177 | needs_trycatch = true; |
| 178 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 179 | for arg in &efn.args { |
| 180 | if arg.ty == RustString { |
| 181 | needs_unsafe_bitcopy = true; |
| 182 | break; |
| 183 | } |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 186 | Api::RustFunction(efn) if !out.header => { |
| 187 | if efn.throws { |
| 188 | out.include.exception = true; |
| 189 | needs_rust_error = true; |
| 190 | } |
| 191 | for arg in &efn.args { |
| 192 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| 193 | needs_manually_drop = true; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | if let Some(ret) = &efn.ret { |
| 198 | if types.needs_indirect_abi(ret) { |
| 199 | needs_maybe_uninit = true; |
| 200 | } |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 203 | _ => {} |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 207 | out.begin_block("namespace rust"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 208 | out.begin_block("inline namespace cxxbridge02"); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 209 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 210 | if needs_rust_string |
| 211 | || needs_rust_str |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 212 | || needs_rust_slice |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 213 | || needs_rust_box |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 214 | || needs_rust_fn |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 215 | || needs_rust_error |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 216 | || needs_rust_isize |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 217 | || needs_unsafe_bitcopy |
| 218 | || needs_manually_drop |
| 219 | || needs_maybe_uninit |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 220 | || needs_trycatch |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 221 | { |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 222 | writeln!(out, "// #include \"rust/cxx.h\""); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| David Tolnay | d140274 | 2020-03-25 22:21:42 -0700 | [diff] [blame] | 225 | if needs_rust_string { |
| 226 | out.next_section(); |
| 227 | writeln!(out, "struct unsafe_bitcopy_t;"); |
| 228 | } |
| 229 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 230 | write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING"); |
| 231 | write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR"); |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 232 | write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE"); |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 233 | write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 234 | write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN"); |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 235 | write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR"); |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 236 | write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE"); |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 237 | write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY"); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 238 | |
| 239 | if needs_manually_drop { |
| 240 | out.next_section(); |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 241 | out.include.utility = true; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 242 | writeln!(out, "template <typename T>"); |
| 243 | writeln!(out, "union ManuallyDrop {{"); |
| 244 | writeln!(out, " T value;"); |
| 245 | writeln!( |
| 246 | out, |
| 247 | " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}", |
| 248 | ); |
| 249 | writeln!(out, " ~ManuallyDrop() {{}}"); |
| 250 | writeln!(out, "}};"); |
| 251 | } |
| 252 | |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 253 | if needs_maybe_uninit { |
| 254 | out.next_section(); |
| 255 | writeln!(out, "template <typename T>"); |
| 256 | writeln!(out, "union MaybeUninit {{"); |
| 257 | writeln!(out, " T value;"); |
| 258 | writeln!(out, " MaybeUninit() {{}}"); |
| 259 | writeln!(out, " ~MaybeUninit() {{}}"); |
| 260 | writeln!(out, "}};"); |
| 261 | } |
| 262 | |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 263 | out.end_block("namespace cxxbridge02"); |
| 264 | |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 265 | if needs_trycatch { |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 266 | out.begin_block("namespace behavior"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 267 | out.include.exception = true; |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 268 | out.include.type_traits = true; |
| 269 | out.include.utility = true; |
| 270 | writeln!(out, "class missing {{}};"); |
| 271 | writeln!(out, "missing trycatch(...);"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 272 | writeln!(out); |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 273 | writeln!(out, "template <typename Try, typename Fail>"); |
| 274 | writeln!(out, "static typename std::enable_if<"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 275 | writeln!( |
| 276 | out, |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 277 | " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),", |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 278 | ); |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 279 | writeln!(out, " missing>::value>::type"); |
| 280 | writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 281 | writeln!(out, " func();"); |
| 282 | writeln!(out, "}} catch (const ::std::exception &e) {{"); |
| 283 | writeln!(out, " fail(e.what());"); |
| 284 | writeln!(out, "}}"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 285 | out.end_block("namespace behavior"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 288 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 291 | fn write_header_section(out: &mut OutFile, needed: bool, section: &str) { |
| David Tolnay | 8e08661 | 2020-04-10 12:20:46 -0700 | [diff] [blame] | 292 | let section = include::get(section); |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 293 | if needed { |
| 294 | out.next_section(); |
| David Tolnay | 8e08661 | 2020-04-10 12:20:46 -0700 | [diff] [blame] | 295 | for line in section.lines() { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 296 | if !line.trim_start().starts_with("//") { |
| 297 | writeln!(out, "{}", line); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 303 | fn write_struct(out: &mut OutFile, strct: &Struct) { |
| 304 | for line in strct.doc.to_string().lines() { |
| 305 | writeln!(out, "//{}", line); |
| 306 | } |
| 307 | writeln!(out, "struct {} final {{", strct.ident); |
| 308 | for field in &strct.fields { |
| 309 | write!(out, " "); |
| 310 | write_type_space(out, &field.ty); |
| 311 | writeln!(out, "{};", field.ident); |
| 312 | } |
| 313 | writeln!(out, "}};"); |
| 314 | } |
| 315 | |
| 316 | fn write_struct_decl(out: &mut OutFile, ident: &Ident) { |
| 317 | writeln!(out, "struct {};", ident); |
| 318 | } |
| 319 | |
| David Tolnay | 8861bee | 2020-01-20 18:39:24 -0800 | [diff] [blame] | 320 | fn write_struct_using(out: &mut OutFile, ident: &Ident) { |
| 321 | writeln!(out, "using {} = {};", ident, ident); |
| 322 | } |
| 323 | |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 324 | fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &Vec<&ExternFn>) { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 325 | for line in ety.doc.to_string().lines() { |
| 326 | writeln!(out, "//{}", line); |
| 327 | } |
| 328 | writeln!(out, "struct {} final {{", ety.ident); |
| Joel Galenson | 187588e | 2020-04-17 16:19:54 -0700 | [diff] [blame^] | 329 | writeln!(out, " {}() = delete;", ety.ident); |
| 330 | writeln!(out, " {}(const {}&) = delete;", ety.ident, ety.ident); |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 331 | for method in methods { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 332 | write!(out, " "); |
| 333 | let sig = &method.sig; |
| 334 | let local_name = method.ident.to_string(); |
| 335 | write_rust_function_shim_decl(out, &local_name, sig, None, false); |
| 336 | writeln!(out, ";"); |
| 337 | } |
| 338 | writeln!(out, "}};"); |
| 339 | } |
| 340 | |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 341 | fn write_exception_glue(out: &mut OutFile, apis: &[Api]) { |
| 342 | let mut has_cxx_throws = false; |
| 343 | for api in apis { |
| 344 | if let Api::CxxFunction(efn) = api { |
| 345 | if efn.throws { |
| 346 | has_cxx_throws = true; |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if has_cxx_throws { |
| 353 | out.next_section(); |
| David Tolnay | e68634c | 2020-03-18 12:03:40 -0700 | [diff] [blame] | 354 | writeln!( |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 355 | out, |
| 356 | "const char *cxxbridge02$exception(const char *, size_t);", |
| 357 | ); |
| 358 | } |
| 359 | } |
| 360 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 361 | fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 362 | if efn.throws { |
| 363 | write!(out, "::rust::Str::Repr "); |
| 364 | } else { |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 365 | write_extern_return_type_space(out, &efn.ret, types); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 366 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 367 | let receiver_type = match &efn.receiver { |
| 368 | Some(base) => base.ident.to_string(), |
| 369 | None => "_".to_string(), |
| 370 | }; |
| 371 | write!(out, "{}cxxbridge02${}${}(", out.namespace, receiver_type, efn.ident); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 372 | if let Some(base) = &efn.receiver { |
| 373 | write!(out, "{} *__receiver$", base.ident); |
| 374 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 375 | for (i, arg) in efn.args.iter().enumerate() { |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 376 | if i > 0 || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 377 | write!(out, ", "); |
| 378 | } |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 379 | if arg.ty == RustString { |
| 380 | write!(out, "const "); |
| 381 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 382 | write_extern_arg(out, arg, types); |
| 383 | } |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 384 | let indirect_return = indirect_return(efn, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 385 | if indirect_return { |
| 386 | if !efn.args.is_empty() { |
| 387 | write!(out, ", "); |
| 388 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 389 | write_indirect_return_type_space(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 390 | write!(out, "*return$"); |
| 391 | } |
| 392 | writeln!(out, ") noexcept {{"); |
| 393 | write!(out, " "); |
| 394 | write_return_type(out, &efn.ret); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 395 | match &efn.receiver { |
| 396 | None => write!(out, "(*{}$)(", efn.ident), |
| 397 | Some(base) => write!(out, "({}::*{}$)(", base.ident, efn.ident), |
| 398 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 399 | for (i, arg) in efn.args.iter().enumerate() { |
| 400 | if i > 0 { |
| 401 | write!(out, ", "); |
| 402 | } |
| 403 | write_type(out, &arg.ty); |
| 404 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 405 | write!(out, ")"); |
| 406 | match &efn.receiver { |
| 407 | Some(Receiver { mutability: None, ident: _ }) => write!(out, " const"), |
| 408 | _ => {}, |
| 409 | } |
| 410 | write!(out, " = "); |
| 411 | match &efn.receiver { |
| 412 | None => write!(out, "{}", efn.ident), |
| 413 | Some(base) => write!(out, "&{}::{}", base.ident, efn.ident), |
| 414 | } |
| 415 | writeln!(out, ";"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 416 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 417 | if efn.throws { |
| 418 | writeln!(out, "::rust::Str::Repr throw$;"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 419 | writeln!(out, " ::rust::behavior::trycatch("); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 420 | writeln!(out, " [&] {{"); |
| 421 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 422 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 423 | if indirect_return { |
| 424 | write!(out, "new (return$) "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 425 | write_indirect_return_type(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 426 | write!(out, "("); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 427 | } else if efn.ret.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 428 | write!(out, "return "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 429 | } |
| 430 | match &efn.ret { |
| 431 | Some(Type::Ref(_)) => write!(out, "&"), |
| 432 | Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("), |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 433 | Some(Type::SliceRefU8(_)) if !indirect_return => { |
| 434 | write!(out, "::rust::Slice<uint8_t>::Repr(") |
| 435 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 436 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 437 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 438 | match &efn.receiver { |
| 439 | None => write!(out, "{}$(", efn.ident), |
| 440 | Some(_) => write!(out, "(__receiver$->*{}$)(", efn.ident), |
| 441 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 442 | for (i, arg) in efn.args.iter().enumerate() { |
| 443 | if i > 0 { |
| 444 | write!(out, ", "); |
| 445 | } |
| 446 | if let Type::RustBox(_) = &arg.ty { |
| 447 | write_type(out, &arg.ty); |
| 448 | write!(out, "::from_raw({})", arg.ident); |
| 449 | } else if let Type::UniquePtr(_) = &arg.ty { |
| 450 | write_type(out, &arg.ty); |
| 451 | write!(out, "({})", arg.ident); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 452 | } else if arg.ty == RustString { |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 453 | write!( |
| 454 | out, |
| 455 | "::rust::String(::rust::unsafe_bitcopy, *{})", |
| 456 | arg.ident, |
| 457 | ); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 458 | } else if types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 459 | out.include.utility = true; |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 460 | write!(out, "::std::move(*{})", arg.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 461 | } else { |
| 462 | write!(out, "{}", arg.ident); |
| 463 | } |
| 464 | } |
| 465 | write!(out, ")"); |
| 466 | match &efn.ret { |
| 467 | Some(Type::RustBox(_)) => write!(out, ".into_raw()"), |
| 468 | Some(Type::UniquePtr(_)) => write!(out, ".release()"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 469 | Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 470 | _ => {} |
| 471 | } |
| 472 | if indirect_return { |
| 473 | write!(out, ")"); |
| 474 | } |
| 475 | writeln!(out, ";"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 476 | if efn.throws { |
| 477 | out.include.cstring = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 478 | writeln!(out, " throw$.ptr = nullptr;"); |
| 479 | writeln!(out, " }},"); |
| David Tolnay | 82c1617 | 2020-03-17 22:54:12 -0700 | [diff] [blame] | 480 | writeln!(out, " [&](const char *catch$) noexcept {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 481 | writeln!(out, " throw$.len = ::std::strlen(catch$);"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 482 | writeln!( |
| 483 | out, |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 484 | " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 485 | ); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 486 | writeln!(out, " }});"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 487 | writeln!(out, " return throw$;"); |
| 488 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 489 | writeln!(out, "}}"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 490 | for arg in &efn.args { |
| 491 | if let Type::Fn(f) = &arg.ty { |
| 492 | let var = &arg.ident; |
| 493 | write_function_pointer_trampoline(out, efn, var, f, types); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | fn write_function_pointer_trampoline( |
| 499 | out: &mut OutFile, |
| 500 | efn: &ExternFn, |
| 501 | var: &Ident, |
| 502 | f: &Signature, |
| 503 | types: &Types, |
| 504 | ) { |
| 505 | out.next_section(); |
| 506 | let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var); |
| 507 | let indirect_call = true; |
| 508 | write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call); |
| 509 | |
| 510 | out.next_section(); |
| 511 | let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var); |
| 512 | write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 516 | let receiver_type = match &efn.receiver { |
| 517 | Some(base) => base.ident.to_string(), |
| 518 | None => "_".to_string(), |
| 519 | }; |
| 520 | let link_name = format!("{}cxxbridge02${}${}", out.namespace, receiver_type, efn.ident); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 521 | let indirect_call = false; |
| 522 | write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call); |
| 523 | } |
| 524 | |
| 525 | fn write_rust_function_decl_impl( |
| 526 | out: &mut OutFile, |
| 527 | link_name: &str, |
| 528 | sig: &Signature, |
| 529 | types: &Types, |
| 530 | indirect_call: bool, |
| 531 | ) { |
| 532 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 533 | write!(out, "::rust::Str::Repr "); |
| 534 | } else { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 535 | write_extern_return_type_space(out, &sig.ret, types); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 536 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 537 | write!(out, "{}(", link_name); |
| 538 | let mut needs_comma = false; |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 539 | if let Some(base) = &sig.receiver { |
| 540 | write!(out, "{} &__receiver$", base.ident); |
| 541 | needs_comma = true; |
| 542 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 543 | for arg in &sig.args { |
| 544 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 545 | write!(out, ", "); |
| 546 | } |
| 547 | write_extern_arg(out, arg, types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 548 | needs_comma = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 549 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 550 | if indirect_return(sig, types) { |
| 551 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 552 | write!(out, ", "); |
| 553 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 554 | write_return_type(out, &sig.ret); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 555 | write!(out, "*return$"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 556 | needs_comma = true; |
| 557 | } |
| 558 | if indirect_call { |
| 559 | if needs_comma { |
| 560 | write!(out, ", "); |
| 561 | } |
| 562 | write!(out, "void *"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 563 | } |
| 564 | writeln!(out, ") noexcept;"); |
| 565 | } |
| 566 | |
| 567 | fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 568 | for line in efn.doc.to_string().lines() { |
| 569 | writeln!(out, "//{}", line); |
| 570 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 571 | let local_name = efn.ident.to_string(); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 572 | let receiver_type = match &efn.receiver { |
| 573 | Some(base) => base.ident.to_string(), |
| 574 | None => "_".to_string(), |
| 575 | }; |
| 576 | let invoke = format!("{}cxxbridge02${}${}", out.namespace, receiver_type, efn.ident); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 577 | let indirect_call = false; |
| 578 | write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call); |
| 579 | } |
| 580 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 581 | fn write_rust_function_shim_decl( |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 582 | out: &mut OutFile, |
| 583 | local_name: &str, |
| 584 | sig: &Signature, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 585 | receiver: Option<&Receiver>, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 586 | indirect_call: bool, |
| 587 | ) { |
| 588 | write_return_type(out, &sig.ret); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 589 | if let Some(base) = receiver { |
| 590 | write!(out, "{}::", base.ident); |
| 591 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 592 | write!(out, "{}(", local_name); |
| 593 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 594 | if i > 0 { |
| 595 | write!(out, ", "); |
| 596 | } |
| 597 | write_type_space(out, &arg.ty); |
| 598 | write!(out, "{}", arg.ident); |
| 599 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 600 | if indirect_call { |
| 601 | if !sig.args.is_empty() { |
| 602 | write!(out, ", "); |
| 603 | } |
| 604 | write!(out, "void *extern$"); |
| 605 | } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 606 | write!(out, ")"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 607 | if !sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 608 | write!(out, " noexcept"); |
| 609 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | fn write_rust_function_shim_impl( |
| 613 | out: &mut OutFile, |
| 614 | local_name: &str, |
| 615 | sig: &Signature, |
| 616 | types: &Types, |
| 617 | invoke: &str, |
| 618 | indirect_call: bool, |
| 619 | ) { |
| 620 | if out.header && sig.receiver.is_some() { |
| 621 | // We've already defined this inside the struct. |
| 622 | return; |
| 623 | } |
| 624 | write_rust_function_shim_decl(out, local_name, sig, sig.receiver.as_ref(), indirect_call); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 625 | if out.header { |
| 626 | writeln!(out, ";"); |
| 627 | } else { |
| 628 | writeln!(out, " {{"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 629 | for arg in &sig.args { |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 630 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 631 | out.include.utility = true; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 632 | write!(out, " ::rust::ManuallyDrop<"); |
| 633 | write_type(out, &arg.ty); |
| 634 | writeln!(out, "> {}$(::std::move({0}));", arg.ident); |
| 635 | } |
| 636 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 637 | write!(out, " "); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 638 | let indirect_return = indirect_return(sig, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 639 | if indirect_return { |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 640 | write!(out, "::rust::MaybeUninit<"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 641 | write_type(out, sig.ret.as_ref().unwrap()); |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 642 | writeln!(out, "> return$;"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 643 | write!(out, " "); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 644 | } else if let Some(ret) = &sig.ret { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 645 | write!(out, "return "); |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 646 | match ret { |
| 647 | Type::RustBox(_) => { |
| 648 | write_type(out, ret); |
| 649 | write!(out, "::from_raw("); |
| 650 | } |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 651 | Type::UniquePtr(_) => { |
| 652 | write_type(out, ret); |
| 653 | write!(out, "("); |
| 654 | } |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 655 | Type::Ref(_) => write!(out, "*"), |
| 656 | _ => {} |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 657 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 658 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 659 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 660 | write!(out, "::rust::Str::Repr error$ = "); |
| 661 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 662 | write!(out, "{}(", invoke); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 663 | if let Some(_) = &sig.receiver { |
| 664 | write!(out, "*this"); |
| 665 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 666 | for (i, arg) in sig.args.iter().enumerate() { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 667 | if i > 0 || sig.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 668 | write!(out, ", "); |
| 669 | } |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 670 | match &arg.ty { |
| 671 | Type::Str(_) => write!(out, "::rust::Str::Repr("), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 672 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("), |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 673 | ty if types.needs_indirect_abi(ty) => write!(out, "&"), |
| 674 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 675 | } |
| 676 | write!(out, "{}", arg.ident); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 677 | match &arg.ty { |
| David Tolnay | 17955e2 | 2020-01-20 17:58:24 -0800 | [diff] [blame] | 678 | Type::RustBox(_) => write!(out, ".into_raw()"), |
| 679 | Type::UniquePtr(_) => write!(out, ".release()"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 680 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"), |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 681 | ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"), |
| David Tolnay | 17955e2 | 2020-01-20 17:58:24 -0800 | [diff] [blame] | 682 | _ => {} |
| 683 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 684 | } |
| 685 | if indirect_return { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 686 | if !sig.args.is_empty() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 687 | write!(out, ", "); |
| 688 | } |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 689 | write!(out, "&return$.value"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 690 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 691 | if indirect_call { |
| 692 | if !sig.args.is_empty() || indirect_return { |
| 693 | write!(out, ", "); |
| 694 | } |
| 695 | write!(out, "extern$"); |
| 696 | } |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 697 | write!(out, ")"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 698 | if let Some(ret) = &sig.ret { |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 699 | if let Type::RustBox(_) | Type::UniquePtr(_) = ret { |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 700 | write!(out, ")"); |
| 701 | } |
| 702 | } |
| 703 | writeln!(out, ";"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 704 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 705 | writeln!(out, " if (error$.ptr) {{"); |
| 706 | writeln!(out, " throw ::rust::Error(error$);"); |
| 707 | writeln!(out, " }}"); |
| 708 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 709 | if indirect_return { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 710 | out.include.utility = true; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 711 | writeln!(out, " return ::std::move(return$.value);"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 712 | } |
| 713 | writeln!(out, "}}"); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | fn write_return_type(out: &mut OutFile, ty: &Option<Type>) { |
| 718 | match ty { |
| 719 | None => write!(out, "void "), |
| 720 | Some(ty) => write_type_space(out, ty), |
| 721 | } |
| 722 | } |
| 723 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 724 | fn indirect_return(sig: &Signature, types: &Types) -> bool { |
| 725 | sig.ret |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 726 | .as_ref() |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 727 | .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret)) |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 730 | fn write_indirect_return_type(out: &mut OutFile, ty: &Type) { |
| 731 | match ty { |
| 732 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 733 | write_type_space(out, &ty.inner); |
| 734 | write!(out, "*"); |
| 735 | } |
| 736 | Type::Ref(ty) => { |
| 737 | if ty.mutability.is_none() { |
| 738 | write!(out, "const "); |
| 739 | } |
| 740 | write_type(out, &ty.inner); |
| 741 | write!(out, " *"); |
| 742 | } |
| 743 | Type::Str(_) => write!(out, "::rust::Str::Repr"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 744 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 745 | _ => write_type(out, ty), |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) { |
| 750 | write_indirect_return_type(out, ty); |
| 751 | match ty { |
| 752 | Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 753 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 754 | _ => write_space_after_type(out, ty), |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 759 | match ty { |
| 760 | Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => { |
| 761 | write_type_space(out, &ty.inner); |
| 762 | write!(out, "*"); |
| 763 | } |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 764 | Some(Type::Ref(ty)) => { |
| 765 | if ty.mutability.is_none() { |
| 766 | write!(out, "const "); |
| 767 | } |
| 768 | write_type(out, &ty.inner); |
| 769 | write!(out, " *"); |
| 770 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 771 | Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 772 | Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 773 | Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "), |
| 774 | _ => write_return_type(out, ty), |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) { |
| 779 | match &arg.ty { |
| 780 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 781 | write_type_space(out, &ty.inner); |
| 782 | write!(out, "*"); |
| 783 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 784 | Type::Str(_) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 785 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 786 | _ => write_type_space(out, &arg.ty), |
| 787 | } |
| 788 | if types.needs_indirect_abi(&arg.ty) { |
| 789 | write!(out, "*"); |
| 790 | } |
| 791 | write!(out, "{}", arg.ident); |
| 792 | } |
| 793 | |
| 794 | fn write_type(out: &mut OutFile, ty: &Type) { |
| 795 | match ty { |
| 796 | Type::Ident(ident) => match Atom::from(ident) { |
| 797 | Some(Bool) => write!(out, "bool"), |
| 798 | Some(U8) => write!(out, "uint8_t"), |
| 799 | Some(U16) => write!(out, "uint16_t"), |
| 800 | Some(U32) => write!(out, "uint32_t"), |
| 801 | Some(U64) => write!(out, "uint64_t"), |
| 802 | Some(Usize) => write!(out, "size_t"), |
| 803 | Some(I8) => write!(out, "int8_t"), |
| 804 | Some(I16) => write!(out, "int16_t"), |
| 805 | Some(I32) => write!(out, "int32_t"), |
| 806 | Some(I64) => write!(out, "int64_t"), |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 807 | Some(Isize) => write!(out, "::rust::isize"), |
| David Tolnay | 3383ae7 | 2020-03-13 01:12:26 -0700 | [diff] [blame] | 808 | Some(F32) => write!(out, "float"), |
| 809 | Some(F64) => write!(out, "double"), |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 810 | Some(CxxString) => write!(out, "::std::string"), |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 811 | Some(RustString) => write!(out, "::rust::String"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 812 | None => write!(out, "{}", ident), |
| 813 | }, |
| 814 | Type::RustBox(ty) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 815 | write!(out, "::rust::Box<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 816 | write_type(out, &ty.inner); |
| 817 | write!(out, ">"); |
| 818 | } |
| 819 | Type::UniquePtr(ptr) => { |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 820 | write!(out, "::std::unique_ptr<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 821 | write_type(out, &ptr.inner); |
| 822 | write!(out, ">"); |
| 823 | } |
| 824 | Type::Ref(r) => { |
| 825 | if r.mutability.is_none() { |
| 826 | write!(out, "const "); |
| 827 | } |
| 828 | write_type(out, &r.inner); |
| 829 | write!(out, " &"); |
| 830 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 831 | Type::Slice(_) => { |
| 832 | // For now, only U8 slices are supported, which are covered separately below |
| 833 | unreachable!() |
| 834 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 835 | Type::Str(_) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 836 | write!(out, "::rust::Str"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 837 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 838 | Type::SliceRefU8(_) => { |
| 839 | write!(out, "::rust::Slice<uint8_t>"); |
| 840 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 841 | Type::Fn(f) => { |
| 842 | write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" }); |
| 843 | match &f.ret { |
| 844 | Some(ret) => write_type(out, ret), |
| 845 | None => write!(out, "void"), |
| 846 | } |
| 847 | write!(out, "("); |
| 848 | for (i, arg) in f.args.iter().enumerate() { |
| 849 | if i > 0 { |
| 850 | write!(out, ", "); |
| 851 | } |
| 852 | write_type(out, &arg.ty); |
| 853 | } |
| 854 | write!(out, ")>"); |
| 855 | } |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 856 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | fn write_type_space(out: &mut OutFile, ty: &Type) { |
| 861 | write_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 862 | write_space_after_type(out, ty); |
| 863 | } |
| 864 | |
| 865 | fn write_space_after_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 866 | match ty { |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 867 | Type::Ident(_) |
| 868 | | Type::RustBox(_) |
| 869 | | Type::UniquePtr(_) |
| 870 | | Type::Str(_) |
| 871 | | Type::SliceRefU8(_) |
| 872 | | Type::Fn(_) => write!(out, " "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 873 | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 874 | Type::Void(_) | Type::Slice(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | fn write_generic_instantiations(out: &mut OutFile, types: &Types) { |
| 879 | fn allow_unique_ptr(ident: &Ident) -> bool { |
| 880 | Atom::from(ident).is_none() |
| 881 | } |
| 882 | |
| 883 | out.begin_block("extern \"C\""); |
| 884 | for ty in types { |
| 885 | if let Type::RustBox(ty) = ty { |
| 886 | if let Type::Ident(inner) = &ty.inner { |
| 887 | out.next_section(); |
| 888 | write_rust_box_extern(out, inner); |
| 889 | } |
| 890 | } else if let Type::UniquePtr(ptr) = ty { |
| 891 | if let Type::Ident(inner) = &ptr.inner { |
| 892 | if allow_unique_ptr(inner) { |
| 893 | out.next_section(); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 894 | write_unique_ptr(out, inner, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | } |
| 898 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 899 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 900 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 901 | out.begin_block("namespace rust"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 902 | out.begin_block("inline namespace cxxbridge02"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 903 | for ty in types { |
| 904 | if let Type::RustBox(ty) = ty { |
| 905 | if let Type::Ident(inner) = &ty.inner { |
| 906 | write_rust_box_impl(out, inner); |
| 907 | } |
| 908 | } |
| 909 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 910 | out.end_block("namespace cxxbridge02"); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 911 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) { |
| 915 | let mut inner = String::new(); |
| 916 | for name in &out.namespace { |
| 917 | inner += name; |
| 918 | inner += "::"; |
| 919 | } |
| 920 | inner += &ident.to_string(); |
| 921 | let instance = inner.replace("::", "$"); |
| 922 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 923 | writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance); |
| 924 | writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 925 | writeln!( |
| 926 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 927 | "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 928 | instance, inner, |
| 929 | ); |
| 930 | writeln!( |
| 931 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 932 | "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 933 | instance, inner, |
| 934 | ); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 935 | writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) { |
| 939 | let mut inner = String::new(); |
| 940 | for name in &out.namespace { |
| 941 | inner += name; |
| 942 | inner += "::"; |
| 943 | } |
| 944 | inner += &ident.to_string(); |
| 945 | let instance = inner.replace("::", "$"); |
| 946 | |
| 947 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 948 | writeln!(out, "void Box<{}>::uninit() noexcept {{", inner); |
| David Tolnay | 737e02e | 2020-04-04 21:52:46 -0700 | [diff] [blame] | 949 | writeln!(out, " cxxbridge02$box${}$uninit(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 950 | writeln!(out, "}}"); |
| 951 | |
| 952 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 953 | writeln!(out, "void Box<{}>::drop() noexcept {{", inner); |
| David Tolnay | 737e02e | 2020-04-04 21:52:46 -0700 | [diff] [blame] | 954 | writeln!(out, " cxxbridge02$box${}$drop(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 955 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 956 | } |
| 957 | |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 958 | fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 959 | out.include.utility = true; |
| 960 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 961 | let mut inner = String::new(); |
| 962 | for name in &out.namespace { |
| 963 | inner += name; |
| 964 | inner += "::"; |
| 965 | } |
| 966 | inner += &ident.to_string(); |
| 967 | let instance = inner.replace("::", "$"); |
| 968 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 969 | writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| 970 | writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 971 | writeln!( |
| 972 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 973 | "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 974 | inner, |
| 975 | ); |
| 976 | writeln!( |
| 977 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 978 | "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 979 | inner, |
| 980 | ); |
| 981 | writeln!( |
| 982 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 983 | "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 984 | instance, inner, |
| 985 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 986 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 987 | writeln!(out, "}}"); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 988 | if types.structs.contains_key(ident) { |
| 989 | writeln!( |
| 990 | out, |
| 991 | "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{", |
| 992 | instance, inner, inner, |
| 993 | ); |
| 994 | writeln!( |
| 995 | out, |
| 996 | " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));", |
| 997 | inner, inner, |
| 998 | ); |
| 999 | writeln!(out, "}}"); |
| 1000 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1001 | writeln!( |
| 1002 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1003 | "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1004 | instance, inner, inner, |
| 1005 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1006 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1007 | writeln!(out, "}}"); |
| 1008 | writeln!( |
| 1009 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1010 | "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1011 | inner, instance, inner, |
| 1012 | ); |
| 1013 | writeln!(out, " return ptr.get();"); |
| 1014 | writeln!(out, "}}"); |
| 1015 | writeln!( |
| 1016 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1017 | "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1018 | inner, instance, inner, |
| 1019 | ); |
| 1020 | writeln!(out, " return ptr.release();"); |
| 1021 | writeln!(out, "}}"); |
| 1022 | writeln!( |
| 1023 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1024 | "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1025 | instance, inner, |
| 1026 | ); |
| 1027 | writeln!(out, " ptr->~unique_ptr();"); |
| 1028 | writeln!(out, "}}"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1029 | writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1030 | } |