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