| 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); |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 373 | if let Some(receiver) = &efn.receiver { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 374 | if receiver.mutability.is_none() { |
| 375 | write!(out, "const "); |
| 376 | } |
| David Tolnay | 41909e6 | 2020-04-20 00:55:15 -0700 | [diff] [blame^] | 377 | write!(out, "{} &self", receiver.ident); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 378 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 379 | for (i, arg) in efn.args.iter().enumerate() { |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 380 | if i > 0 || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 381 | write!(out, ", "); |
| 382 | } |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 383 | if arg.ty == RustString { |
| 384 | write!(out, "const "); |
| 385 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 386 | write_extern_arg(out, arg, types); |
| 387 | } |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 388 | let indirect_return = indirect_return(efn, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 389 | if indirect_return { |
| 390 | if !efn.args.is_empty() { |
| 391 | write!(out, ", "); |
| 392 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 393 | write_indirect_return_type_space(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 394 | write!(out, "*return$"); |
| 395 | } |
| 396 | writeln!(out, ") noexcept {{"); |
| 397 | write!(out, " "); |
| 398 | write_return_type(out, &efn.ret); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 399 | match &efn.receiver { |
| 400 | None => write!(out, "(*{}$)(", efn.ident), |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 401 | Some(receiver) => write!(out, "({}::*{}$)(", receiver.ident, efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 402 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 403 | for (i, arg) in efn.args.iter().enumerate() { |
| 404 | if i > 0 { |
| 405 | write!(out, ", "); |
| 406 | } |
| 407 | write_type(out, &arg.ty); |
| 408 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 409 | write!(out, ")"); |
| David Tolnay | 4e7123f | 2020-04-19 21:11:37 -0700 | [diff] [blame] | 410 | if let Some(receiver) = &efn.receiver { |
| 411 | if receiver.mutability.is_none() { |
| 412 | write!(out, " const"); |
| 413 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 414 | } |
| 415 | write!(out, " = "); |
| 416 | match &efn.receiver { |
| 417 | None => write!(out, "{}", efn.ident), |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 418 | Some(receiver) => write!(out, "&{}::{}", receiver.ident, efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 419 | } |
| 420 | writeln!(out, ";"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 421 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 422 | if efn.throws { |
| 423 | writeln!(out, "::rust::Str::Repr throw$;"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 424 | writeln!(out, " ::rust::behavior::trycatch("); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 425 | writeln!(out, " [&] {{"); |
| 426 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 427 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 428 | if indirect_return { |
| 429 | write!(out, "new (return$) "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 430 | write_indirect_return_type(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 431 | write!(out, "("); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 432 | } else if efn.ret.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 433 | write!(out, "return "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 434 | } |
| 435 | match &efn.ret { |
| 436 | Some(Type::Ref(_)) => write!(out, "&"), |
| 437 | Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("), |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 438 | Some(Type::SliceRefU8(_)) if !indirect_return => { |
| 439 | write!(out, "::rust::Slice<uint8_t>::Repr(") |
| 440 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 441 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 442 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 443 | match &efn.receiver { |
| 444 | None => write!(out, "{}$(", efn.ident), |
| David Tolnay | 41909e6 | 2020-04-20 00:55:15 -0700 | [diff] [blame^] | 445 | Some(_) => write!(out, "(self.*{}$)(", efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 446 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 447 | for (i, arg) in efn.args.iter().enumerate() { |
| 448 | if i > 0 { |
| 449 | write!(out, ", "); |
| 450 | } |
| 451 | if let Type::RustBox(_) = &arg.ty { |
| 452 | write_type(out, &arg.ty); |
| 453 | write!(out, "::from_raw({})", arg.ident); |
| 454 | } else if let Type::UniquePtr(_) = &arg.ty { |
| 455 | write_type(out, &arg.ty); |
| 456 | write!(out, "({})", arg.ident); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 457 | } else if arg.ty == RustString { |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 458 | write!( |
| 459 | out, |
| 460 | "::rust::String(::rust::unsafe_bitcopy, *{})", |
| 461 | arg.ident, |
| 462 | ); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 463 | } else if types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 464 | out.include.utility = true; |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 465 | write!(out, "::std::move(*{})", arg.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 466 | } else { |
| 467 | write!(out, "{}", arg.ident); |
| 468 | } |
| 469 | } |
| 470 | write!(out, ")"); |
| 471 | match &efn.ret { |
| 472 | Some(Type::RustBox(_)) => write!(out, ".into_raw()"), |
| 473 | Some(Type::UniquePtr(_)) => write!(out, ".release()"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 474 | Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 475 | _ => {} |
| 476 | } |
| 477 | if indirect_return { |
| 478 | write!(out, ")"); |
| 479 | } |
| 480 | writeln!(out, ";"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 481 | if efn.throws { |
| 482 | out.include.cstring = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 483 | writeln!(out, " throw$.ptr = nullptr;"); |
| 484 | writeln!(out, " }},"); |
| David Tolnay | 82c1617 | 2020-03-17 22:54:12 -0700 | [diff] [blame] | 485 | writeln!(out, " [&](const char *catch$) noexcept {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 486 | writeln!(out, " throw$.len = ::std::strlen(catch$);"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 487 | writeln!( |
| 488 | out, |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 489 | " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 490 | ); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 491 | writeln!(out, " }});"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 492 | writeln!(out, " return throw$;"); |
| 493 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 494 | writeln!(out, "}}"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 495 | for arg in &efn.args { |
| 496 | if let Type::Fn(f) = &arg.ty { |
| 497 | let var = &arg.ident; |
| 498 | write_function_pointer_trampoline(out, efn, var, f, types); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | fn write_function_pointer_trampoline( |
| 504 | out: &mut OutFile, |
| 505 | efn: &ExternFn, |
| 506 | var: &Ident, |
| 507 | f: &Signature, |
| 508 | types: &Types, |
| 509 | ) { |
| 510 | out.next_section(); |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 511 | let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 512 | let indirect_call = true; |
| 513 | write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call); |
| 514 | |
| 515 | out.next_section(); |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 516 | let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 517 | 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] | 518 | } |
| 519 | |
| 520 | fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 521 | let link_name = mangle::extern_fn(&out.namespace, efn); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 522 | let indirect_call = false; |
| 523 | write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call); |
| 524 | } |
| 525 | |
| 526 | fn write_rust_function_decl_impl( |
| 527 | out: &mut OutFile, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 528 | link_name: &Symbol, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 529 | sig: &Signature, |
| 530 | types: &Types, |
| 531 | indirect_call: bool, |
| 532 | ) { |
| 533 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 534 | write!(out, "::rust::Str::Repr "); |
| 535 | } else { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 536 | write_extern_return_type_space(out, &sig.ret, types); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 537 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 538 | write!(out, "{}(", link_name); |
| 539 | let mut needs_comma = false; |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 540 | if let Some(receiver) = &sig.receiver { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 541 | if receiver.mutability.is_none() { |
| 542 | write!(out, "const "); |
| 543 | } |
| David Tolnay | c3f485c | 2020-04-20 00:52:01 -0700 | [diff] [blame] | 544 | write!(out, "{} &self", receiver.ident); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 545 | needs_comma = true; |
| 546 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 547 | for arg in &sig.args { |
| 548 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 549 | write!(out, ", "); |
| 550 | } |
| 551 | write_extern_arg(out, arg, types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 552 | needs_comma = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 553 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 554 | if indirect_return(sig, types) { |
| 555 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 556 | write!(out, ", "); |
| 557 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 558 | write_return_type(out, &sig.ret); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 559 | write!(out, "*return$"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 560 | needs_comma = true; |
| 561 | } |
| 562 | if indirect_call { |
| 563 | if needs_comma { |
| 564 | write!(out, ", "); |
| 565 | } |
| 566 | write!(out, "void *"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 567 | } |
| 568 | writeln!(out, ") noexcept;"); |
| 569 | } |
| 570 | |
| 571 | fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 572 | for line in efn.doc.to_string().lines() { |
| 573 | writeln!(out, "//{}", line); |
| 574 | } |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 575 | let local_name = Symbol::from(&efn.ident); |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 576 | let invoke = mangle::extern_fn(&out.namespace, efn); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 577 | let indirect_call = false; |
| 578 | write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call); |
| 579 | } |
| 580 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 581 | fn write_rust_function_shim_decl( |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 582 | out: &mut OutFile, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 583 | local_name: &Symbol, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 584 | sig: &Signature, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 585 | receiver: Option<&Receiver>, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 586 | indirect_call: bool, |
| 587 | ) { |
| 588 | write_return_type(out, &sig.ret); |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 589 | if let Some(receiver) = receiver { |
| 590 | write!(out, "{}::", receiver.ident); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 591 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 592 | write!(out, "{}(", local_name); |
| 593 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 594 | if i > 0 { |
| 595 | write!(out, ", "); |
| 596 | } |
| 597 | write_type_space(out, &arg.ty); |
| 598 | write!(out, "{}", arg.ident); |
| 599 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 600 | if indirect_call { |
| 601 | if !sig.args.is_empty() { |
| 602 | write!(out, ", "); |
| 603 | } |
| 604 | write!(out, "void *extern$"); |
| 605 | } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 606 | write!(out, ")"); |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 607 | if let Some(receiver) = &sig.receiver { |
| 608 | if receiver.mutability.is_none() { |
| 609 | write!(out, " const"); |
| 610 | } |
| 611 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 612 | if !sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 613 | write!(out, " noexcept"); |
| 614 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | fn write_rust_function_shim_impl( |
| 618 | out: &mut OutFile, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 619 | local_name: &Symbol, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 620 | sig: &Signature, |
| 621 | types: &Types, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 622 | invoke: &Symbol, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 623 | indirect_call: bool, |
| 624 | ) { |
| 625 | if out.header && sig.receiver.is_some() { |
| 626 | // We've already defined this inside the struct. |
| 627 | return; |
| 628 | } |
| 629 | 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] | 630 | if out.header { |
| 631 | writeln!(out, ";"); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 632 | return; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 633 | } |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 634 | writeln!(out, " {{"); |
| 635 | for arg in &sig.args { |
| 636 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| 637 | out.include.utility = true; |
| 638 | write!(out, " ::rust::ManuallyDrop<"); |
| 639 | write_type(out, &arg.ty); |
| 640 | writeln!(out, "> {}$(::std::move({0}));", arg.ident); |
| 641 | } |
| 642 | } |
| 643 | write!(out, " "); |
| 644 | let indirect_return = indirect_return(sig, types); |
| 645 | if indirect_return { |
| 646 | write!(out, "::rust::MaybeUninit<"); |
| 647 | write_type(out, sig.ret.as_ref().unwrap()); |
| 648 | writeln!(out, "> return$;"); |
| 649 | write!(out, " "); |
| 650 | } else if let Some(ret) = &sig.ret { |
| 651 | write!(out, "return "); |
| 652 | match ret { |
| 653 | Type::RustBox(_) => { |
| 654 | write_type(out, ret); |
| 655 | write!(out, "::from_raw("); |
| 656 | } |
| 657 | Type::UniquePtr(_) => { |
| 658 | write_type(out, ret); |
| 659 | write!(out, "("); |
| 660 | } |
| 661 | Type::Ref(_) => write!(out, "*"), |
| 662 | _ => {} |
| 663 | } |
| 664 | } |
| 665 | if sig.throws { |
| 666 | write!(out, "::rust::Str::Repr error$ = "); |
| 667 | } |
| 668 | write!(out, "{}(", invoke); |
| 669 | if sig.receiver.is_some() { |
| 670 | write!(out, "*this"); |
| 671 | } |
| 672 | for (i, arg) in sig.args.iter().enumerate() { |
| 673 | if i > 0 || sig.receiver.is_some() { |
| 674 | write!(out, ", "); |
| 675 | } |
| 676 | match &arg.ty { |
| 677 | Type::Str(_) => write!(out, "::rust::Str::Repr("), |
| 678 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("), |
| 679 | ty if types.needs_indirect_abi(ty) => write!(out, "&"), |
| 680 | _ => {} |
| 681 | } |
| 682 | write!(out, "{}", arg.ident); |
| 683 | match &arg.ty { |
| 684 | Type::RustBox(_) => write!(out, ".into_raw()"), |
| 685 | Type::UniquePtr(_) => write!(out, ".release()"), |
| 686 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"), |
| 687 | ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"), |
| 688 | _ => {} |
| 689 | } |
| 690 | } |
| 691 | if indirect_return { |
| 692 | if !sig.args.is_empty() { |
| 693 | write!(out, ", "); |
| 694 | } |
| 695 | write!(out, "&return$.value"); |
| 696 | } |
| 697 | if indirect_call { |
| 698 | if !sig.args.is_empty() || indirect_return { |
| 699 | write!(out, ", "); |
| 700 | } |
| 701 | write!(out, "extern$"); |
| 702 | } |
| 703 | write!(out, ")"); |
| 704 | if let Some(ret) = &sig.ret { |
| 705 | if let Type::RustBox(_) | Type::UniquePtr(_) = ret { |
| 706 | write!(out, ")"); |
| 707 | } |
| 708 | } |
| 709 | writeln!(out, ";"); |
| 710 | if sig.throws { |
| 711 | writeln!(out, " if (error$.ptr) {{"); |
| 712 | writeln!(out, " throw ::rust::Error(error$);"); |
| 713 | writeln!(out, " }}"); |
| 714 | } |
| 715 | if indirect_return { |
| 716 | out.include.utility = true; |
| 717 | writeln!(out, " return ::std::move(return$.value);"); |
| 718 | } |
| 719 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | fn write_return_type(out: &mut OutFile, ty: &Option<Type>) { |
| 723 | match ty { |
| 724 | None => write!(out, "void "), |
| 725 | Some(ty) => write_type_space(out, ty), |
| 726 | } |
| 727 | } |
| 728 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 729 | fn indirect_return(sig: &Signature, types: &Types) -> bool { |
| 730 | sig.ret |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 731 | .as_ref() |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 732 | .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret)) |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 735 | fn write_indirect_return_type(out: &mut OutFile, ty: &Type) { |
| 736 | match ty { |
| 737 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 738 | write_type_space(out, &ty.inner); |
| 739 | write!(out, "*"); |
| 740 | } |
| 741 | Type::Ref(ty) => { |
| 742 | if ty.mutability.is_none() { |
| 743 | write!(out, "const "); |
| 744 | } |
| 745 | write_type(out, &ty.inner); |
| 746 | write!(out, " *"); |
| 747 | } |
| 748 | Type::Str(_) => write!(out, "::rust::Str::Repr"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 749 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 750 | _ => write_type(out, ty), |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) { |
| 755 | write_indirect_return_type(out, ty); |
| 756 | match ty { |
| 757 | Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 758 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 759 | _ => write_space_after_type(out, ty), |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | 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] | 764 | match ty { |
| 765 | Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => { |
| 766 | write_type_space(out, &ty.inner); |
| 767 | write!(out, "*"); |
| 768 | } |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 769 | Some(Type::Ref(ty)) => { |
| 770 | if ty.mutability.is_none() { |
| 771 | write!(out, "const "); |
| 772 | } |
| 773 | write_type(out, &ty.inner); |
| 774 | write!(out, " *"); |
| 775 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 776 | Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 777 | Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 778 | Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "), |
| 779 | _ => write_return_type(out, ty), |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) { |
| 784 | match &arg.ty { |
| 785 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 786 | write_type_space(out, &ty.inner); |
| 787 | write!(out, "*"); |
| 788 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 789 | Type::Str(_) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 790 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 791 | _ => write_type_space(out, &arg.ty), |
| 792 | } |
| 793 | if types.needs_indirect_abi(&arg.ty) { |
| 794 | write!(out, "*"); |
| 795 | } |
| 796 | write!(out, "{}", arg.ident); |
| 797 | } |
| 798 | |
| 799 | fn write_type(out: &mut OutFile, ty: &Type) { |
| 800 | match ty { |
| 801 | Type::Ident(ident) => match Atom::from(ident) { |
| 802 | Some(Bool) => write!(out, "bool"), |
| 803 | Some(U8) => write!(out, "uint8_t"), |
| 804 | Some(U16) => write!(out, "uint16_t"), |
| 805 | Some(U32) => write!(out, "uint32_t"), |
| 806 | Some(U64) => write!(out, "uint64_t"), |
| 807 | Some(Usize) => write!(out, "size_t"), |
| 808 | Some(I8) => write!(out, "int8_t"), |
| 809 | Some(I16) => write!(out, "int16_t"), |
| 810 | Some(I32) => write!(out, "int32_t"), |
| 811 | Some(I64) => write!(out, "int64_t"), |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 812 | Some(Isize) => write!(out, "::rust::isize"), |
| David Tolnay | 3383ae7 | 2020-03-13 01:12:26 -0700 | [diff] [blame] | 813 | Some(F32) => write!(out, "float"), |
| 814 | Some(F64) => write!(out, "double"), |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 815 | Some(CxxString) => write!(out, "::std::string"), |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 816 | Some(RustString) => write!(out, "::rust::String"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 817 | None => write!(out, "{}", ident), |
| 818 | }, |
| 819 | Type::RustBox(ty) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 820 | write!(out, "::rust::Box<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 821 | write_type(out, &ty.inner); |
| 822 | write!(out, ">"); |
| 823 | } |
| 824 | Type::UniquePtr(ptr) => { |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 825 | write!(out, "::std::unique_ptr<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 826 | write_type(out, &ptr.inner); |
| 827 | write!(out, ">"); |
| 828 | } |
| 829 | Type::Ref(r) => { |
| 830 | if r.mutability.is_none() { |
| 831 | write!(out, "const "); |
| 832 | } |
| 833 | write_type(out, &r.inner); |
| 834 | write!(out, " &"); |
| 835 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 836 | Type::Slice(_) => { |
| 837 | // For now, only U8 slices are supported, which are covered separately below |
| 838 | unreachable!() |
| 839 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 840 | Type::Str(_) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 841 | write!(out, "::rust::Str"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 842 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 843 | Type::SliceRefU8(_) => { |
| 844 | write!(out, "::rust::Slice<uint8_t>"); |
| 845 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 846 | Type::Fn(f) => { |
| 847 | write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" }); |
| 848 | match &f.ret { |
| 849 | Some(ret) => write_type(out, ret), |
| 850 | None => write!(out, "void"), |
| 851 | } |
| 852 | write!(out, "("); |
| 853 | for (i, arg) in f.args.iter().enumerate() { |
| 854 | if i > 0 { |
| 855 | write!(out, ", "); |
| 856 | } |
| 857 | write_type(out, &arg.ty); |
| 858 | } |
| 859 | write!(out, ")>"); |
| 860 | } |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 861 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
| 865 | fn write_type_space(out: &mut OutFile, ty: &Type) { |
| 866 | write_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 867 | write_space_after_type(out, ty); |
| 868 | } |
| 869 | |
| 870 | fn write_space_after_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 871 | match ty { |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 872 | Type::Ident(_) |
| 873 | | Type::RustBox(_) |
| 874 | | Type::UniquePtr(_) |
| 875 | | Type::Str(_) |
| 876 | | Type::SliceRefU8(_) |
| 877 | | Type::Fn(_) => write!(out, " "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 878 | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 879 | Type::Void(_) | Type::Slice(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
| 883 | fn write_generic_instantiations(out: &mut OutFile, types: &Types) { |
| 884 | fn allow_unique_ptr(ident: &Ident) -> bool { |
| 885 | Atom::from(ident).is_none() |
| 886 | } |
| 887 | |
| 888 | out.begin_block("extern \"C\""); |
| 889 | for ty in types { |
| 890 | if let Type::RustBox(ty) = ty { |
| 891 | if let Type::Ident(inner) = &ty.inner { |
| 892 | out.next_section(); |
| 893 | write_rust_box_extern(out, inner); |
| 894 | } |
| 895 | } else if let Type::UniquePtr(ptr) = ty { |
| 896 | if let Type::Ident(inner) = &ptr.inner { |
| 897 | if allow_unique_ptr(inner) { |
| 898 | out.next_section(); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 899 | write_unique_ptr(out, inner, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | } |
| 903 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 904 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 905 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 906 | out.begin_block("namespace rust"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 907 | out.begin_block("inline namespace cxxbridge02"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 908 | for ty in types { |
| 909 | if let Type::RustBox(ty) = ty { |
| 910 | if let Type::Ident(inner) = &ty.inner { |
| 911 | write_rust_box_impl(out, inner); |
| 912 | } |
| 913 | } |
| 914 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 915 | out.end_block("namespace cxxbridge02"); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 916 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) { |
| 920 | let mut inner = String::new(); |
| 921 | for name in &out.namespace { |
| 922 | inner += name; |
| 923 | inner += "::"; |
| 924 | } |
| 925 | inner += &ident.to_string(); |
| 926 | let instance = inner.replace("::", "$"); |
| 927 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 928 | writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance); |
| 929 | writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 930 | writeln!( |
| 931 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 932 | "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 933 | instance, inner, |
| 934 | ); |
| 935 | writeln!( |
| 936 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 937 | "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 938 | instance, inner, |
| 939 | ); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 940 | writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) { |
| 944 | let mut inner = String::new(); |
| 945 | for name in &out.namespace { |
| 946 | inner += name; |
| 947 | inner += "::"; |
| 948 | } |
| 949 | inner += &ident.to_string(); |
| 950 | let instance = inner.replace("::", "$"); |
| 951 | |
| 952 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 953 | writeln!(out, "void Box<{}>::uninit() noexcept {{", inner); |
| David Tolnay | 737e02e | 2020-04-04 21:52:46 -0700 | [diff] [blame] | 954 | writeln!(out, " cxxbridge02$box${}$uninit(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 955 | writeln!(out, "}}"); |
| 956 | |
| 957 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 958 | writeln!(out, "void Box<{}>::drop() noexcept {{", inner); |
| David Tolnay | 737e02e | 2020-04-04 21:52:46 -0700 | [diff] [blame] | 959 | writeln!(out, " cxxbridge02$box${}$drop(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 960 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 961 | } |
| 962 | |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 963 | fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 964 | out.include.utility = true; |
| 965 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 966 | let mut inner = String::new(); |
| 967 | for name in &out.namespace { |
| 968 | inner += name; |
| 969 | inner += "::"; |
| 970 | } |
| 971 | inner += &ident.to_string(); |
| 972 | let instance = inner.replace("::", "$"); |
| 973 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 974 | writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| 975 | writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 976 | writeln!( |
| 977 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 978 | "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 979 | inner, |
| 980 | ); |
| 981 | writeln!( |
| 982 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 983 | "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 984 | inner, |
| 985 | ); |
| 986 | writeln!( |
| 987 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 988 | "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 989 | instance, inner, |
| 990 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 991 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 992 | writeln!(out, "}}"); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 993 | if types.structs.contains_key(ident) { |
| 994 | writeln!( |
| 995 | out, |
| 996 | "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{", |
| 997 | instance, inner, inner, |
| 998 | ); |
| 999 | writeln!( |
| 1000 | out, |
| 1001 | " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));", |
| 1002 | inner, inner, |
| 1003 | ); |
| 1004 | writeln!(out, "}}"); |
| 1005 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1006 | writeln!( |
| 1007 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1008 | "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1009 | instance, inner, inner, |
| 1010 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1011 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1012 | writeln!(out, "}}"); |
| 1013 | writeln!( |
| 1014 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1015 | "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1016 | inner, instance, inner, |
| 1017 | ); |
| 1018 | writeln!(out, " return ptr.get();"); |
| 1019 | writeln!(out, "}}"); |
| 1020 | writeln!( |
| 1021 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1022 | "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1023 | inner, instance, inner, |
| 1024 | ); |
| 1025 | writeln!(out, " return ptr.release();"); |
| 1026 | writeln!(out, "}}"); |
| 1027 | writeln!( |
| 1028 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1029 | "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1030 | instance, inner, |
| 1031 | ); |
| 1032 | writeln!(out, " ptr->~unique_ptr();"); |
| 1033 | writeln!(out, "}}"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1034 | writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1035 | } |