| 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 | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 329 | for method in methods { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 330 | write!(out, " "); |
| 331 | let sig = &method.sig; |
| 332 | let local_name = method.ident.to_string(); |
| 333 | write_rust_function_shim_decl(out, &local_name, sig, None, false); |
| 334 | writeln!(out, ";"); |
| 335 | } |
| 336 | writeln!(out, "}};"); |
| 337 | } |
| 338 | |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 339 | fn write_exception_glue(out: &mut OutFile, apis: &[Api]) { |
| 340 | let mut has_cxx_throws = false; |
| 341 | for api in apis { |
| 342 | if let Api::CxxFunction(efn) = api { |
| 343 | if efn.throws { |
| 344 | has_cxx_throws = true; |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if has_cxx_throws { |
| 351 | out.next_section(); |
| David Tolnay | e68634c | 2020-03-18 12:03:40 -0700 | [diff] [blame] | 352 | writeln!( |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 353 | out, |
| 354 | "const char *cxxbridge02$exception(const char *, size_t);", |
| 355 | ); |
| 356 | } |
| 357 | } |
| 358 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 359 | fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 360 | if efn.throws { |
| 361 | write!(out, "::rust::Str::Repr "); |
| 362 | } else { |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 363 | write_extern_return_type_space(out, &efn.ret, types); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 364 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 365 | let receiver_type = match &efn.receiver { |
| 366 | Some(base) => base.ident.to_string(), |
| 367 | None => "_".to_string(), |
| 368 | }; |
| 369 | write!(out, "{}cxxbridge02${}${}(", out.namespace, receiver_type, efn.ident); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 370 | if let Some(base) = &efn.receiver { |
| 371 | write!(out, "{} *__receiver$", base.ident); |
| 372 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 373 | for (i, arg) in efn.args.iter().enumerate() { |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 374 | if i > 0 || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 375 | write!(out, ", "); |
| 376 | } |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 377 | if arg.ty == RustString { |
| 378 | write!(out, "const "); |
| 379 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 380 | write_extern_arg(out, arg, types); |
| 381 | } |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 382 | let indirect_return = indirect_return(efn, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 383 | if indirect_return { |
| 384 | if !efn.args.is_empty() { |
| 385 | write!(out, ", "); |
| 386 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 387 | write_indirect_return_type_space(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 388 | write!(out, "*return$"); |
| 389 | } |
| 390 | writeln!(out, ") noexcept {{"); |
| 391 | write!(out, " "); |
| 392 | write_return_type(out, &efn.ret); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 393 | match &efn.receiver { |
| 394 | None => write!(out, "(*{}$)(", efn.ident), |
| 395 | Some(base) => write!(out, "({}::*{}$)(", base.ident, efn.ident), |
| 396 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 397 | for (i, arg) in efn.args.iter().enumerate() { |
| 398 | if i > 0 { |
| 399 | write!(out, ", "); |
| 400 | } |
| 401 | write_type(out, &arg.ty); |
| 402 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 403 | write!(out, ")"); |
| 404 | match &efn.receiver { |
| 405 | Some(Receiver { mutability: None, ident: _ }) => write!(out, " const"), |
| 406 | _ => {}, |
| 407 | } |
| 408 | write!(out, " = "); |
| 409 | match &efn.receiver { |
| 410 | None => write!(out, "{}", efn.ident), |
| 411 | Some(base) => write!(out, "&{}::{}", base.ident, efn.ident), |
| 412 | } |
| 413 | writeln!(out, ";"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 414 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 415 | if efn.throws { |
| 416 | writeln!(out, "::rust::Str::Repr throw$;"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 417 | writeln!(out, " ::rust::behavior::trycatch("); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 418 | writeln!(out, " [&] {{"); |
| 419 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 420 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 421 | if indirect_return { |
| 422 | write!(out, "new (return$) "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 423 | write_indirect_return_type(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 424 | write!(out, "("); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 425 | } else if efn.ret.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 426 | write!(out, "return "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 427 | } |
| 428 | match &efn.ret { |
| 429 | Some(Type::Ref(_)) => write!(out, "&"), |
| 430 | Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("), |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 431 | Some(Type::SliceRefU8(_)) if !indirect_return => { |
| 432 | write!(out, "::rust::Slice<uint8_t>::Repr(") |
| 433 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 434 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 435 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 436 | match &efn.receiver { |
| 437 | None => write!(out, "{}$(", efn.ident), |
| 438 | Some(_) => write!(out, "(__receiver$->*{}$)(", efn.ident), |
| 439 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 440 | for (i, arg) in efn.args.iter().enumerate() { |
| 441 | if i > 0 { |
| 442 | write!(out, ", "); |
| 443 | } |
| 444 | if let Type::RustBox(_) = &arg.ty { |
| 445 | write_type(out, &arg.ty); |
| 446 | write!(out, "::from_raw({})", arg.ident); |
| 447 | } else if let Type::UniquePtr(_) = &arg.ty { |
| 448 | write_type(out, &arg.ty); |
| 449 | write!(out, "({})", arg.ident); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 450 | } else if arg.ty == RustString { |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 451 | write!( |
| 452 | out, |
| 453 | "::rust::String(::rust::unsafe_bitcopy, *{})", |
| 454 | arg.ident, |
| 455 | ); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 456 | } else if types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 457 | out.include.utility = true; |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 458 | write!(out, "::std::move(*{})", arg.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 459 | } else { |
| 460 | write!(out, "{}", arg.ident); |
| 461 | } |
| 462 | } |
| 463 | write!(out, ")"); |
| 464 | match &efn.ret { |
| 465 | Some(Type::RustBox(_)) => write!(out, ".into_raw()"), |
| 466 | Some(Type::UniquePtr(_)) => write!(out, ".release()"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 467 | Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 468 | _ => {} |
| 469 | } |
| 470 | if indirect_return { |
| 471 | write!(out, ")"); |
| 472 | } |
| 473 | writeln!(out, ";"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 474 | if efn.throws { |
| 475 | out.include.cstring = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 476 | writeln!(out, " throw$.ptr = nullptr;"); |
| 477 | writeln!(out, " }},"); |
| David Tolnay | 82c1617 | 2020-03-17 22:54:12 -0700 | [diff] [blame] | 478 | writeln!(out, " [&](const char *catch$) noexcept {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 479 | writeln!(out, " throw$.len = ::std::strlen(catch$);"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 480 | writeln!( |
| 481 | out, |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 482 | " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 483 | ); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 484 | writeln!(out, " }});"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 485 | writeln!(out, " return throw$;"); |
| 486 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 487 | writeln!(out, "}}"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 488 | for arg in &efn.args { |
| 489 | if let Type::Fn(f) = &arg.ty { |
| 490 | let var = &arg.ident; |
| 491 | write_function_pointer_trampoline(out, efn, var, f, types); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | fn write_function_pointer_trampoline( |
| 497 | out: &mut OutFile, |
| 498 | efn: &ExternFn, |
| 499 | var: &Ident, |
| 500 | f: &Signature, |
| 501 | types: &Types, |
| 502 | ) { |
| 503 | out.next_section(); |
| 504 | let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var); |
| 505 | let indirect_call = true; |
| 506 | write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call); |
| 507 | |
| 508 | out.next_section(); |
| 509 | let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var); |
| 510 | 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] | 511 | } |
| 512 | |
| 513 | fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 514 | let receiver_type = match &efn.receiver { |
| 515 | Some(base) => base.ident.to_string(), |
| 516 | None => "_".to_string(), |
| 517 | }; |
| 518 | let link_name = format!("{}cxxbridge02${}${}", out.namespace, receiver_type, efn.ident); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 519 | let indirect_call = false; |
| 520 | write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call); |
| 521 | } |
| 522 | |
| 523 | fn write_rust_function_decl_impl( |
| 524 | out: &mut OutFile, |
| 525 | link_name: &str, |
| 526 | sig: &Signature, |
| 527 | types: &Types, |
| 528 | indirect_call: bool, |
| 529 | ) { |
| 530 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 531 | write!(out, "::rust::Str::Repr "); |
| 532 | } else { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 533 | write_extern_return_type_space(out, &sig.ret, types); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 534 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 535 | write!(out, "{}(", link_name); |
| 536 | let mut needs_comma = false; |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 537 | if let Some(base) = &sig.receiver { |
| 538 | write!(out, "{} &__receiver$", base.ident); |
| 539 | needs_comma = true; |
| 540 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 541 | for arg in &sig.args { |
| 542 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 543 | write!(out, ", "); |
| 544 | } |
| 545 | write_extern_arg(out, arg, types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 546 | needs_comma = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 547 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 548 | if indirect_return(sig, types) { |
| 549 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 550 | write!(out, ", "); |
| 551 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 552 | write_return_type(out, &sig.ret); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 553 | write!(out, "*return$"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 554 | needs_comma = true; |
| 555 | } |
| 556 | if indirect_call { |
| 557 | if needs_comma { |
| 558 | write!(out, ", "); |
| 559 | } |
| 560 | write!(out, "void *"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 561 | } |
| 562 | writeln!(out, ") noexcept;"); |
| 563 | } |
| 564 | |
| 565 | fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 566 | for line in efn.doc.to_string().lines() { |
| 567 | writeln!(out, "//{}", line); |
| 568 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 569 | let local_name = efn.ident.to_string(); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 570 | let receiver_type = match &efn.receiver { |
| 571 | Some(base) => base.ident.to_string(), |
| 572 | None => "_".to_string(), |
| 573 | }; |
| 574 | let invoke = format!("{}cxxbridge02${}${}", out.namespace, receiver_type, efn.ident); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 575 | let indirect_call = false; |
| 576 | write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call); |
| 577 | } |
| 578 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 579 | fn write_rust_function_shim_decl( |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 580 | out: &mut OutFile, |
| 581 | local_name: &str, |
| 582 | sig: &Signature, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 583 | receiver: Option<&Receiver>, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 584 | indirect_call: bool, |
| 585 | ) { |
| 586 | write_return_type(out, &sig.ret); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 587 | if let Some(base) = receiver { |
| 588 | write!(out, "{}::", base.ident); |
| 589 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 590 | write!(out, "{}(", local_name); |
| 591 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 592 | if i > 0 { |
| 593 | write!(out, ", "); |
| 594 | } |
| 595 | write_type_space(out, &arg.ty); |
| 596 | write!(out, "{}", arg.ident); |
| 597 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 598 | if indirect_call { |
| 599 | if !sig.args.is_empty() { |
| 600 | write!(out, ", "); |
| 601 | } |
| 602 | write!(out, "void *extern$"); |
| 603 | } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 604 | write!(out, ")"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 605 | if !sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 606 | write!(out, " noexcept"); |
| 607 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | fn write_rust_function_shim_impl( |
| 611 | out: &mut OutFile, |
| 612 | local_name: &str, |
| 613 | sig: &Signature, |
| 614 | types: &Types, |
| 615 | invoke: &str, |
| 616 | indirect_call: bool, |
| 617 | ) { |
| 618 | if out.header && sig.receiver.is_some() { |
| 619 | // We've already defined this inside the struct. |
| 620 | return; |
| 621 | } |
| 622 | 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] | 623 | if out.header { |
| 624 | writeln!(out, ";"); |
| 625 | } else { |
| 626 | writeln!(out, " {{"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 627 | for arg in &sig.args { |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 628 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 629 | out.include.utility = true; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 630 | write!(out, " ::rust::ManuallyDrop<"); |
| 631 | write_type(out, &arg.ty); |
| 632 | writeln!(out, "> {}$(::std::move({0}));", arg.ident); |
| 633 | } |
| 634 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 635 | write!(out, " "); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 636 | let indirect_return = indirect_return(sig, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 637 | if indirect_return { |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 638 | write!(out, "::rust::MaybeUninit<"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 639 | write_type(out, sig.ret.as_ref().unwrap()); |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 640 | writeln!(out, "> return$;"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 641 | write!(out, " "); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 642 | } else if let Some(ret) = &sig.ret { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 643 | write!(out, "return "); |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 644 | match ret { |
| 645 | Type::RustBox(_) => { |
| 646 | write_type(out, ret); |
| 647 | write!(out, "::from_raw("); |
| 648 | } |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 649 | Type::UniquePtr(_) => { |
| 650 | write_type(out, ret); |
| 651 | write!(out, "("); |
| 652 | } |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 653 | Type::Ref(_) => write!(out, "*"), |
| 654 | _ => {} |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 655 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 656 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 657 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 658 | write!(out, "::rust::Str::Repr error$ = "); |
| 659 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 660 | write!(out, "{}(", invoke); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 661 | if let Some(_) = &sig.receiver { |
| 662 | write!(out, "*this"); |
| 663 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 664 | for (i, arg) in sig.args.iter().enumerate() { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 665 | if i > 0 || sig.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 666 | write!(out, ", "); |
| 667 | } |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 668 | match &arg.ty { |
| 669 | Type::Str(_) => write!(out, "::rust::Str::Repr("), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 670 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("), |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 671 | ty if types.needs_indirect_abi(ty) => write!(out, "&"), |
| 672 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 673 | } |
| 674 | write!(out, "{}", arg.ident); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 675 | match &arg.ty { |
| David Tolnay | 17955e2 | 2020-01-20 17:58:24 -0800 | [diff] [blame] | 676 | Type::RustBox(_) => write!(out, ".into_raw()"), |
| 677 | Type::UniquePtr(_) => write!(out, ".release()"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 678 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"), |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 679 | ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"), |
| David Tolnay | 17955e2 | 2020-01-20 17:58:24 -0800 | [diff] [blame] | 680 | _ => {} |
| 681 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 682 | } |
| 683 | if indirect_return { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 684 | if !sig.args.is_empty() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 685 | write!(out, ", "); |
| 686 | } |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 687 | write!(out, "&return$.value"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 688 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 689 | if indirect_call { |
| 690 | if !sig.args.is_empty() || indirect_return { |
| 691 | write!(out, ", "); |
| 692 | } |
| 693 | write!(out, "extern$"); |
| 694 | } |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 695 | write!(out, ")"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 696 | if let Some(ret) = &sig.ret { |
| David Tolnay | 4b3a66e | 2020-03-06 16:14:00 -0800 | [diff] [blame] | 697 | if let Type::RustBox(_) | Type::UniquePtr(_) = ret { |
| David Tolnay | 5cd8d61 | 2020-03-06 15:56:30 -0800 | [diff] [blame] | 698 | write!(out, ")"); |
| 699 | } |
| 700 | } |
| 701 | writeln!(out, ";"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 702 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 703 | writeln!(out, " if (error$.ptr) {{"); |
| 704 | writeln!(out, " throw ::rust::Error(error$);"); |
| 705 | writeln!(out, " }}"); |
| 706 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 707 | if indirect_return { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 708 | out.include.utility = true; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 709 | writeln!(out, " return ::std::move(return$.value);"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 710 | } |
| 711 | writeln!(out, "}}"); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | fn write_return_type(out: &mut OutFile, ty: &Option<Type>) { |
| 716 | match ty { |
| 717 | None => write!(out, "void "), |
| 718 | Some(ty) => write_type_space(out, ty), |
| 719 | } |
| 720 | } |
| 721 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 722 | fn indirect_return(sig: &Signature, types: &Types) -> bool { |
| 723 | sig.ret |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 724 | .as_ref() |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 725 | .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret)) |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 728 | fn write_indirect_return_type(out: &mut OutFile, ty: &Type) { |
| 729 | match ty { |
| 730 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 731 | write_type_space(out, &ty.inner); |
| 732 | write!(out, "*"); |
| 733 | } |
| 734 | Type::Ref(ty) => { |
| 735 | if ty.mutability.is_none() { |
| 736 | write!(out, "const "); |
| 737 | } |
| 738 | write_type(out, &ty.inner); |
| 739 | write!(out, " *"); |
| 740 | } |
| 741 | Type::Str(_) => write!(out, "::rust::Str::Repr"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 742 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 743 | _ => write_type(out, ty), |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) { |
| 748 | write_indirect_return_type(out, ty); |
| 749 | match ty { |
| 750 | Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 751 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 752 | _ => write_space_after_type(out, ty), |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | 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] | 757 | match ty { |
| 758 | Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => { |
| 759 | write_type_space(out, &ty.inner); |
| 760 | write!(out, "*"); |
| 761 | } |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 762 | Some(Type::Ref(ty)) => { |
| 763 | if ty.mutability.is_none() { |
| 764 | write!(out, "const "); |
| 765 | } |
| 766 | write_type(out, &ty.inner); |
| 767 | write!(out, " *"); |
| 768 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 769 | Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 770 | Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 771 | Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "), |
| 772 | _ => write_return_type(out, ty), |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) { |
| 777 | match &arg.ty { |
| 778 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 779 | write_type_space(out, &ty.inner); |
| 780 | write!(out, "*"); |
| 781 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 782 | Type::Str(_) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 783 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 784 | _ => write_type_space(out, &arg.ty), |
| 785 | } |
| 786 | if types.needs_indirect_abi(&arg.ty) { |
| 787 | write!(out, "*"); |
| 788 | } |
| 789 | write!(out, "{}", arg.ident); |
| 790 | } |
| 791 | |
| 792 | fn write_type(out: &mut OutFile, ty: &Type) { |
| 793 | match ty { |
| 794 | Type::Ident(ident) => match Atom::from(ident) { |
| 795 | Some(Bool) => write!(out, "bool"), |
| 796 | Some(U8) => write!(out, "uint8_t"), |
| 797 | Some(U16) => write!(out, "uint16_t"), |
| 798 | Some(U32) => write!(out, "uint32_t"), |
| 799 | Some(U64) => write!(out, "uint64_t"), |
| 800 | Some(Usize) => write!(out, "size_t"), |
| 801 | Some(I8) => write!(out, "int8_t"), |
| 802 | Some(I16) => write!(out, "int16_t"), |
| 803 | Some(I32) => write!(out, "int32_t"), |
| 804 | Some(I64) => write!(out, "int64_t"), |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 805 | Some(Isize) => write!(out, "::rust::isize"), |
| David Tolnay | 3383ae7 | 2020-03-13 01:12:26 -0700 | [diff] [blame] | 806 | Some(F32) => write!(out, "float"), |
| 807 | Some(F64) => write!(out, "double"), |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 808 | Some(CxxString) => write!(out, "::std::string"), |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 809 | Some(RustString) => write!(out, "::rust::String"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 810 | None => write!(out, "{}", ident), |
| 811 | }, |
| 812 | Type::RustBox(ty) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 813 | write!(out, "::rust::Box<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 814 | write_type(out, &ty.inner); |
| 815 | write!(out, ">"); |
| 816 | } |
| 817 | Type::UniquePtr(ptr) => { |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 818 | write!(out, "::std::unique_ptr<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 819 | write_type(out, &ptr.inner); |
| 820 | write!(out, ">"); |
| 821 | } |
| 822 | Type::Ref(r) => { |
| 823 | if r.mutability.is_none() { |
| 824 | write!(out, "const "); |
| 825 | } |
| 826 | write_type(out, &r.inner); |
| 827 | write!(out, " &"); |
| 828 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 829 | Type::Slice(_) => { |
| 830 | // For now, only U8 slices are supported, which are covered separately below |
| 831 | unreachable!() |
| 832 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 833 | Type::Str(_) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 834 | write!(out, "::rust::Str"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 835 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 836 | Type::SliceRefU8(_) => { |
| 837 | write!(out, "::rust::Slice<uint8_t>"); |
| 838 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 839 | Type::Fn(f) => { |
| 840 | write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" }); |
| 841 | match &f.ret { |
| 842 | Some(ret) => write_type(out, ret), |
| 843 | None => write!(out, "void"), |
| 844 | } |
| 845 | write!(out, "("); |
| 846 | for (i, arg) in f.args.iter().enumerate() { |
| 847 | if i > 0 { |
| 848 | write!(out, ", "); |
| 849 | } |
| 850 | write_type(out, &arg.ty); |
| 851 | } |
| 852 | write!(out, ")>"); |
| 853 | } |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 854 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
| 858 | fn write_type_space(out: &mut OutFile, ty: &Type) { |
| 859 | write_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 860 | write_space_after_type(out, ty); |
| 861 | } |
| 862 | |
| 863 | fn write_space_after_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 864 | match ty { |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 865 | Type::Ident(_) |
| 866 | | Type::RustBox(_) |
| 867 | | Type::UniquePtr(_) |
| 868 | | Type::Str(_) |
| 869 | | Type::SliceRefU8(_) |
| 870 | | Type::Fn(_) => write!(out, " "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 871 | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 872 | Type::Void(_) | Type::Slice(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | fn write_generic_instantiations(out: &mut OutFile, types: &Types) { |
| 877 | fn allow_unique_ptr(ident: &Ident) -> bool { |
| 878 | Atom::from(ident).is_none() |
| 879 | } |
| 880 | |
| 881 | out.begin_block("extern \"C\""); |
| 882 | for ty in types { |
| 883 | if let Type::RustBox(ty) = ty { |
| 884 | if let Type::Ident(inner) = &ty.inner { |
| 885 | out.next_section(); |
| 886 | write_rust_box_extern(out, inner); |
| 887 | } |
| 888 | } else if let Type::UniquePtr(ptr) = ty { |
| 889 | if let Type::Ident(inner) = &ptr.inner { |
| 890 | if allow_unique_ptr(inner) { |
| 891 | out.next_section(); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 892 | write_unique_ptr(out, inner, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | } |
| 896 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 897 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 898 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 899 | out.begin_block("namespace rust"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 900 | out.begin_block("inline namespace cxxbridge02"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 901 | for ty in types { |
| 902 | if let Type::RustBox(ty) = ty { |
| 903 | if let Type::Ident(inner) = &ty.inner { |
| 904 | write_rust_box_impl(out, inner); |
| 905 | } |
| 906 | } |
| 907 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 908 | out.end_block("namespace cxxbridge02"); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 909 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) { |
| 913 | let mut inner = String::new(); |
| 914 | for name in &out.namespace { |
| 915 | inner += name; |
| 916 | inner += "::"; |
| 917 | } |
| 918 | inner += &ident.to_string(); |
| 919 | let instance = inner.replace("::", "$"); |
| 920 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 921 | writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance); |
| 922 | writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 923 | writeln!( |
| 924 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 925 | "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 926 | instance, inner, |
| 927 | ); |
| 928 | writeln!( |
| 929 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 930 | "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 931 | instance, inner, |
| 932 | ); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 933 | writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) { |
| 937 | let mut inner = String::new(); |
| 938 | for name in &out.namespace { |
| 939 | inner += name; |
| 940 | inner += "::"; |
| 941 | } |
| 942 | inner += &ident.to_string(); |
| 943 | let instance = inner.replace("::", "$"); |
| 944 | |
| 945 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 946 | writeln!(out, "void Box<{}>::uninit() noexcept {{", inner); |
| David Tolnay | 737e02e | 2020-04-04 21:52:46 -0700 | [diff] [blame] | 947 | writeln!(out, " cxxbridge02$box${}$uninit(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 948 | writeln!(out, "}}"); |
| 949 | |
| 950 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 951 | writeln!(out, "void Box<{}>::drop() noexcept {{", inner); |
| David Tolnay | 737e02e | 2020-04-04 21:52:46 -0700 | [diff] [blame] | 952 | writeln!(out, " cxxbridge02$box${}$drop(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 953 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 954 | } |
| 955 | |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 956 | fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 957 | out.include.utility = true; |
| 958 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 959 | let mut inner = String::new(); |
| 960 | for name in &out.namespace { |
| 961 | inner += name; |
| 962 | inner += "::"; |
| 963 | } |
| 964 | inner += &ident.to_string(); |
| 965 | let instance = inner.replace("::", "$"); |
| 966 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 967 | writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| 968 | writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 969 | writeln!( |
| 970 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 971 | "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 972 | inner, |
| 973 | ); |
| 974 | writeln!( |
| 975 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 976 | "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 977 | inner, |
| 978 | ); |
| 979 | writeln!( |
| 980 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 981 | "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 982 | instance, inner, |
| 983 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 984 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 985 | writeln!(out, "}}"); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 986 | if types.structs.contains_key(ident) { |
| 987 | writeln!( |
| 988 | out, |
| 989 | "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{", |
| 990 | instance, inner, inner, |
| 991 | ); |
| 992 | writeln!( |
| 993 | out, |
| 994 | " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));", |
| 995 | inner, inner, |
| 996 | ); |
| 997 | writeln!(out, "}}"); |
| 998 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 999 | writeln!( |
| 1000 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1001 | "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1002 | instance, inner, inner, |
| 1003 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1004 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1005 | writeln!(out, "}}"); |
| 1006 | writeln!( |
| 1007 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1008 | "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1009 | inner, instance, inner, |
| 1010 | ); |
| 1011 | writeln!(out, " return ptr.get();"); |
| 1012 | writeln!(out, "}}"); |
| 1013 | writeln!( |
| 1014 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1015 | "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1016 | inner, instance, inner, |
| 1017 | ); |
| 1018 | writeln!(out, " return ptr.release();"); |
| 1019 | writeln!(out, "}}"); |
| 1020 | writeln!( |
| 1021 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1022 | "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1023 | instance, inner, |
| 1024 | ); |
| 1025 | writeln!(out, " ptr->~unique_ptr();"); |
| 1026 | writeln!(out, "}}"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1027 | writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1028 | } |