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