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