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