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