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