| 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, *}; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 4 | use crate::syntax::mangled::ToMangled; |
| David Tolnay | 0841930 | 2020-04-19 20:38:20 -0700 | [diff] [blame] | 5 | use crate::syntax::namespace::Namespace; |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 6 | use crate::syntax::symbol::Symbol; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 7 | use crate::syntax::typename::ToTypename; |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 8 | use crate::syntax::{mangle, Api, ExternFn, ExternType, Signature, Struct, Type, Types, Var}; |
| 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), |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 47 | _ => (), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 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 |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 56 | .entry(&receiver.ty) |
| David Tolnay | f94bef1 | 2020-04-17 14:46:42 -0700 | [diff] [blame] | 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, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 128 | Type::Vector(_) => out.include.vector = true, |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 129 | Type::SliceRefU8(_) => out.include.cstdint = true, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 130 | _ => (), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 131 | } |
| 132 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 135 | fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 136 | let mut needs_rust_string = false; |
| 137 | let mut needs_rust_str = false; |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 138 | let mut needs_rust_slice = false; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 139 | let mut needs_rust_box = false; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 140 | let mut needs_rust_vec = false; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 141 | let mut needs_rust_fn = false; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 142 | for ty in types { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 143 | match ty { |
| 144 | Type::RustBox(_) => { |
| 145 | out.include.type_traits = true; |
| 146 | needs_rust_box = true; |
| 147 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 148 | Type::RustVec(_) => { |
| 149 | out.include.type_traits = true; |
| 150 | needs_rust_vec = true; |
| 151 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 152 | Type::Str(_) => { |
| 153 | out.include.cstdint = true; |
| 154 | out.include.string = true; |
| 155 | needs_rust_str = true; |
| 156 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 157 | Type::Fn(_) => { |
| 158 | needs_rust_fn = true; |
| 159 | } |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 160 | Type::Slice(_) | Type::SliceRefU8(_) => { |
| 161 | needs_rust_slice = true; |
| 162 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 163 | ty if ty == RustString => { |
| 164 | out.include.array = true; |
| 165 | out.include.cstdint = true; |
| 166 | out.include.string = true; |
| 167 | needs_rust_string = true; |
| 168 | } |
| 169 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 173 | let mut needs_rust_error = false; |
| 174 | let mut needs_unsafe_bitcopy = false; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 175 | let mut needs_manually_drop = false; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 176 | let mut needs_maybe_uninit = false; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 177 | let mut needs_trycatch = false; |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 178 | for api in apis { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 179 | match api { |
| 180 | Api::CxxFunction(efn) if !out.header => { |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 181 | if efn.throws { |
| 182 | needs_trycatch = true; |
| 183 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 184 | for arg in &efn.args { |
| 185 | if arg.ty == RustString { |
| 186 | needs_unsafe_bitcopy = true; |
| 187 | break; |
| 188 | } |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 191 | Api::RustFunction(efn) if !out.header => { |
| 192 | if efn.throws { |
| 193 | out.include.exception = true; |
| 194 | needs_rust_error = true; |
| 195 | } |
| 196 | for arg in &efn.args { |
| 197 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| 198 | needs_manually_drop = true; |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | if let Some(ret) = &efn.ret { |
| 203 | if types.needs_indirect_abi(ret) { |
| 204 | needs_maybe_uninit = true; |
| 205 | } |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 208 | _ => {} |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 212 | out.begin_block("namespace rust"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 213 | out.begin_block("inline namespace cxxbridge02"); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 214 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 215 | if needs_rust_string |
| 216 | || needs_rust_str |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 217 | || needs_rust_slice |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 218 | || needs_rust_box |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 219 | || needs_rust_vec |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 220 | || needs_rust_fn |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 221 | || needs_rust_error |
| 222 | || needs_unsafe_bitcopy |
| 223 | || needs_manually_drop |
| 224 | || needs_maybe_uninit |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 225 | || needs_trycatch |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 226 | { |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 227 | writeln!(out, "// #include \"rust/cxx.h\""); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| David Tolnay | d140274 | 2020-03-25 22:21:42 -0700 | [diff] [blame] | 230 | if needs_rust_string { |
| 231 | out.next_section(); |
| 232 | writeln!(out, "struct unsafe_bitcopy_t;"); |
| 233 | } |
| 234 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 235 | write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING"); |
| 236 | write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR"); |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 237 | write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE"); |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 238 | write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 239 | write_header_section(out, needs_rust_vec, "CXXBRIDGE02_RUST_VEC"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 240 | write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN"); |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 241 | write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR"); |
| 242 | write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY"); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 243 | |
| 244 | if needs_manually_drop { |
| 245 | out.next_section(); |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 246 | out.include.utility = true; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 247 | writeln!(out, "template <typename T>"); |
| 248 | writeln!(out, "union ManuallyDrop {{"); |
| 249 | writeln!(out, " T value;"); |
| 250 | writeln!( |
| 251 | out, |
| 252 | " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}", |
| 253 | ); |
| 254 | writeln!(out, " ~ManuallyDrop() {{}}"); |
| 255 | writeln!(out, "}};"); |
| 256 | } |
| 257 | |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 258 | if needs_maybe_uninit { |
| 259 | out.next_section(); |
| 260 | writeln!(out, "template <typename T>"); |
| 261 | writeln!(out, "union MaybeUninit {{"); |
| 262 | writeln!(out, " T value;"); |
| 263 | writeln!(out, " MaybeUninit() {{}}"); |
| 264 | writeln!(out, " ~MaybeUninit() {{}}"); |
| 265 | writeln!(out, "}};"); |
| 266 | } |
| 267 | |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 268 | out.end_block("namespace cxxbridge02"); |
| 269 | |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 270 | if needs_trycatch { |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 271 | out.begin_block("namespace behavior"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 272 | out.include.exception = true; |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 273 | out.include.type_traits = true; |
| 274 | out.include.utility = true; |
| 275 | writeln!(out, "class missing {{}};"); |
| 276 | writeln!(out, "missing trycatch(...);"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 277 | writeln!(out); |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 278 | writeln!(out, "template <typename Try, typename Fail>"); |
| 279 | writeln!(out, "static typename std::enable_if<"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 280 | writeln!( |
| 281 | out, |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 282 | " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),", |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 283 | ); |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 284 | writeln!(out, " missing>::value>::type"); |
| 285 | writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 286 | writeln!(out, " func();"); |
| 287 | writeln!(out, "}} catch (const ::std::exception &e) {{"); |
| 288 | writeln!(out, " fail(e.what());"); |
| 289 | writeln!(out, "}}"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 290 | out.end_block("namespace behavior"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 293 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 296 | fn write_header_section(out: &mut OutFile, needed: bool, section: &str) { |
| David Tolnay | 8e08661 | 2020-04-10 12:20:46 -0700 | [diff] [blame] | 297 | let section = include::get(section); |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 298 | if needed { |
| 299 | out.next_section(); |
| David Tolnay | 8e08661 | 2020-04-10 12:20:46 -0700 | [diff] [blame] | 300 | for line in section.lines() { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 301 | if !line.trim_start().starts_with("//") { |
| 302 | writeln!(out, "{}", line); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 308 | fn write_struct(out: &mut OutFile, strct: &Struct) { |
| 309 | for line in strct.doc.to_string().lines() { |
| 310 | writeln!(out, "//{}", line); |
| 311 | } |
| 312 | writeln!(out, "struct {} final {{", strct.ident); |
| 313 | for field in &strct.fields { |
| 314 | write!(out, " "); |
| 315 | write_type_space(out, &field.ty); |
| 316 | writeln!(out, "{};", field.ident); |
| 317 | } |
| 318 | writeln!(out, "}};"); |
| 319 | } |
| 320 | |
| 321 | fn write_struct_decl(out: &mut OutFile, ident: &Ident) { |
| 322 | writeln!(out, "struct {};", ident); |
| 323 | } |
| 324 | |
| David Tolnay | 8861bee | 2020-01-20 18:39:24 -0800 | [diff] [blame] | 325 | fn write_struct_using(out: &mut OutFile, ident: &Ident) { |
| 326 | writeln!(out, "using {} = {};", ident, ident); |
| 327 | } |
| 328 | |
| David Tolnay | c1fe005 | 2020-04-17 15:15:06 -0700 | [diff] [blame] | 329 | fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 330 | for line in ety.doc.to_string().lines() { |
| 331 | writeln!(out, "//{}", line); |
| 332 | } |
| 333 | writeln!(out, "struct {} final {{", ety.ident); |
| Joel Galenson | 187588e | 2020-04-17 16:19:54 -0700 | [diff] [blame] | 334 | writeln!(out, " {}() = delete;", ety.ident); |
| David Tolnay | 44395e3 | 2020-04-19 14:52:49 -0700 | [diff] [blame] | 335 | writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident); |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 336 | for method in methods { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 337 | write!(out, " "); |
| 338 | let sig = &method.sig; |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 339 | let local_name = method.ident.to_string(); |
| 340 | write_rust_function_shim_decl(out, &local_name, sig, false); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 341 | writeln!(out, ";"); |
| 342 | } |
| 343 | writeln!(out, "}};"); |
| 344 | } |
| 345 | |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 346 | fn write_exception_glue(out: &mut OutFile, apis: &[Api]) { |
| 347 | let mut has_cxx_throws = false; |
| 348 | for api in apis { |
| 349 | if let Api::CxxFunction(efn) = api { |
| 350 | if efn.throws { |
| 351 | has_cxx_throws = true; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if has_cxx_throws { |
| 358 | out.next_section(); |
| David Tolnay | e68634c | 2020-03-18 12:03:40 -0700 | [diff] [blame] | 359 | writeln!( |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 360 | out, |
| 361 | "const char *cxxbridge02$exception(const char *, size_t);", |
| 362 | ); |
| 363 | } |
| 364 | } |
| 365 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 366 | fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 367 | if efn.throws { |
| 368 | write!(out, "::rust::Str::Repr "); |
| 369 | } else { |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 370 | write_extern_return_type_space(out, &efn.ret, types); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 371 | } |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 372 | let mangled = mangle::extern_fn(&out.namespace, efn); |
| 373 | write!(out, "{}(", mangled); |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 374 | if let Some(receiver) = &efn.receiver { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 375 | if receiver.mutability.is_none() { |
| 376 | write!(out, "const "); |
| 377 | } |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 378 | write!(out, "{} &self", receiver.ty); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 379 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 380 | for (i, arg) in efn.args.iter().enumerate() { |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 381 | if i > 0 || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 382 | write!(out, ", "); |
| 383 | } |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 384 | if arg.ty == RustString { |
| 385 | write!(out, "const "); |
| 386 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 387 | write_extern_arg(out, arg, types); |
| 388 | } |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 389 | let indirect_return = indirect_return(efn, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 390 | if indirect_return { |
| 391 | if !efn.args.is_empty() { |
| 392 | write!(out, ", "); |
| 393 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 394 | write_indirect_return_type_space(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 395 | write!(out, "*return$"); |
| 396 | } |
| 397 | writeln!(out, ") noexcept {{"); |
| 398 | write!(out, " "); |
| 399 | write_return_type(out, &efn.ret); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 400 | match &efn.receiver { |
| 401 | None => write!(out, "(*{}$)(", efn.ident), |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 402 | Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 403 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 404 | for (i, arg) in efn.args.iter().enumerate() { |
| 405 | if i > 0 { |
| 406 | write!(out, ", "); |
| 407 | } |
| 408 | write_type(out, &arg.ty); |
| 409 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 410 | write!(out, ")"); |
| David Tolnay | 4e7123f | 2020-04-19 21:11:37 -0700 | [diff] [blame] | 411 | if let Some(receiver) = &efn.receiver { |
| 412 | if receiver.mutability.is_none() { |
| 413 | write!(out, " const"); |
| 414 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 415 | } |
| 416 | write!(out, " = "); |
| 417 | match &efn.receiver { |
| 418 | None => write!(out, "{}", efn.ident), |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 419 | Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 420 | } |
| 421 | writeln!(out, ";"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 422 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 423 | if efn.throws { |
| 424 | writeln!(out, "::rust::Str::Repr throw$;"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 425 | writeln!(out, " ::rust::behavior::trycatch("); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 426 | writeln!(out, " [&] {{"); |
| 427 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 428 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 429 | if indirect_return { |
| 430 | write!(out, "new (return$) "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 431 | write_indirect_return_type(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 432 | write!(out, "("); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 433 | } else if efn.ret.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 434 | write!(out, "return "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 435 | } |
| 436 | match &efn.ret { |
| 437 | Some(Type::Ref(_)) => write!(out, "&"), |
| 438 | Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("), |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 439 | Some(Type::SliceRefU8(_)) if !indirect_return => { |
| 440 | write!(out, "::rust::Slice<uint8_t>::Repr(") |
| 441 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 442 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 443 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 444 | match &efn.receiver { |
| 445 | None => write!(out, "{}$(", efn.ident), |
| David Tolnay | 41909e6 | 2020-04-20 00:55:15 -0700 | [diff] [blame] | 446 | Some(_) => write!(out, "(self.*{}$)(", efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 447 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 448 | for (i, arg) in efn.args.iter().enumerate() { |
| 449 | if i > 0 { |
| 450 | write!(out, ", "); |
| 451 | } |
| 452 | if let Type::RustBox(_) = &arg.ty { |
| 453 | write_type(out, &arg.ty); |
| 454 | write!(out, "::from_raw({})", arg.ident); |
| 455 | } else if let Type::UniquePtr(_) = &arg.ty { |
| 456 | write_type(out, &arg.ty); |
| 457 | write!(out, "({})", arg.ident); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 458 | } else if arg.ty == RustString { |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 459 | write!( |
| 460 | out, |
| 461 | "::rust::String(::rust::unsafe_bitcopy, *{})", |
| 462 | arg.ident, |
| 463 | ); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 464 | } else if types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 465 | out.include.utility = true; |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 466 | write!(out, "::std::move(*{})", arg.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 467 | } else { |
| 468 | write!(out, "{}", arg.ident); |
| 469 | } |
| 470 | } |
| 471 | write!(out, ")"); |
| 472 | match &efn.ret { |
| 473 | Some(Type::RustBox(_)) => write!(out, ".into_raw()"), |
| 474 | Some(Type::UniquePtr(_)) => write!(out, ".release()"), |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 475 | Some(Type::Vector(_)) => write!( |
| 476 | out, |
| 477 | " /* Use RVO to convert to r-value and move construct */" |
| 478 | ), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 479 | Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 480 | _ => {} |
| 481 | } |
| 482 | if indirect_return { |
| 483 | write!(out, ")"); |
| 484 | } |
| 485 | writeln!(out, ";"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 486 | if efn.throws { |
| 487 | out.include.cstring = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 488 | writeln!(out, " throw$.ptr = nullptr;"); |
| 489 | writeln!(out, " }},"); |
| David Tolnay | 82c1617 | 2020-03-17 22:54:12 -0700 | [diff] [blame] | 490 | writeln!(out, " [&](const char *catch$) noexcept {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 491 | writeln!(out, " throw$.len = ::std::strlen(catch$);"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 492 | writeln!( |
| 493 | out, |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 494 | " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 495 | ); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 496 | writeln!(out, " }});"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 497 | writeln!(out, " return throw$;"); |
| 498 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 499 | writeln!(out, "}}"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 500 | for arg in &efn.args { |
| 501 | if let Type::Fn(f) = &arg.ty { |
| 502 | let var = &arg.ident; |
| 503 | write_function_pointer_trampoline(out, efn, var, f, types); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | fn write_function_pointer_trampoline( |
| 509 | out: &mut OutFile, |
| 510 | efn: &ExternFn, |
| 511 | var: &Ident, |
| 512 | f: &Signature, |
| 513 | types: &Types, |
| 514 | ) { |
| 515 | out.next_section(); |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 516 | let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 517 | let indirect_call = true; |
| 518 | write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call); |
| 519 | |
| 520 | out.next_section(); |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 521 | let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string(); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 522 | 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] | 523 | } |
| 524 | |
| 525 | fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 526 | let link_name = mangle::extern_fn(&out.namespace, efn); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 527 | let indirect_call = false; |
| 528 | write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call); |
| 529 | } |
| 530 | |
| 531 | fn write_rust_function_decl_impl( |
| 532 | out: &mut OutFile, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 533 | link_name: &Symbol, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 534 | sig: &Signature, |
| 535 | types: &Types, |
| 536 | indirect_call: bool, |
| 537 | ) { |
| 538 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 539 | write!(out, "::rust::Str::Repr "); |
| 540 | } else { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 541 | write_extern_return_type_space(out, &sig.ret, types); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 542 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 543 | write!(out, "{}(", link_name); |
| 544 | let mut needs_comma = false; |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 545 | if let Some(receiver) = &sig.receiver { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 546 | if receiver.mutability.is_none() { |
| 547 | write!(out, "const "); |
| 548 | } |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 549 | write!(out, "{} &self", receiver.ty); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 550 | needs_comma = true; |
| 551 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 552 | for arg in &sig.args { |
| 553 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 554 | write!(out, ", "); |
| 555 | } |
| 556 | write_extern_arg(out, arg, types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 557 | needs_comma = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 558 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 559 | if indirect_return(sig, types) { |
| 560 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 561 | write!(out, ", "); |
| 562 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 563 | write_return_type(out, &sig.ret); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 564 | write!(out, "*return$"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 565 | needs_comma = true; |
| 566 | } |
| 567 | if indirect_call { |
| 568 | if needs_comma { |
| 569 | write!(out, ", "); |
| 570 | } |
| 571 | write!(out, "void *"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 572 | } |
| 573 | writeln!(out, ") noexcept;"); |
| 574 | } |
| 575 | |
| 576 | fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 577 | for line in efn.doc.to_string().lines() { |
| 578 | writeln!(out, "//{}", line); |
| 579 | } |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 580 | let local_name = match &efn.sig.receiver { |
| 581 | None => efn.ident.to_string(), |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 582 | Some(receiver) => format!("{}::{}", receiver.ty, efn.ident), |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 583 | }; |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 584 | let invoke = mangle::extern_fn(&out.namespace, efn); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 585 | let indirect_call = false; |
| 586 | write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call); |
| 587 | } |
| 588 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 589 | fn write_rust_function_shim_decl( |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 590 | out: &mut OutFile, |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 591 | local_name: &str, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 592 | sig: &Signature, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 593 | indirect_call: bool, |
| 594 | ) { |
| 595 | write_return_type(out, &sig.ret); |
| 596 | write!(out, "{}(", local_name); |
| 597 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 598 | if i > 0 { |
| 599 | write!(out, ", "); |
| 600 | } |
| 601 | write_type_space(out, &arg.ty); |
| 602 | write!(out, "{}", arg.ident); |
| 603 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 604 | if indirect_call { |
| 605 | if !sig.args.is_empty() { |
| 606 | write!(out, ", "); |
| 607 | } |
| 608 | write!(out, "void *extern$"); |
| 609 | } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 610 | write!(out, ")"); |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 611 | if let Some(receiver) = &sig.receiver { |
| 612 | if receiver.mutability.is_none() { |
| 613 | write!(out, " const"); |
| 614 | } |
| 615 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 616 | if !sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 617 | write!(out, " noexcept"); |
| 618 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | fn write_rust_function_shim_impl( |
| 622 | out: &mut OutFile, |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 623 | local_name: &str, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 624 | sig: &Signature, |
| 625 | types: &Types, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 626 | invoke: &Symbol, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 627 | indirect_call: bool, |
| 628 | ) { |
| 629 | if out.header && sig.receiver.is_some() { |
| 630 | // We've already defined this inside the struct. |
| 631 | return; |
| 632 | } |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 633 | write_rust_function_shim_decl(out, local_name, sig, indirect_call); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 634 | if out.header { |
| 635 | writeln!(out, ";"); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 636 | return; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 637 | } |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 638 | writeln!(out, " {{"); |
| 639 | for arg in &sig.args { |
| 640 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| 641 | out.include.utility = true; |
| 642 | write!(out, " ::rust::ManuallyDrop<"); |
| 643 | write_type(out, &arg.ty); |
| 644 | writeln!(out, "> {}$(::std::move({0}));", arg.ident); |
| 645 | } |
| 646 | } |
| 647 | write!(out, " "); |
| 648 | let indirect_return = indirect_return(sig, types); |
| 649 | if indirect_return { |
| 650 | write!(out, "::rust::MaybeUninit<"); |
| 651 | write_type(out, sig.ret.as_ref().unwrap()); |
| 652 | writeln!(out, "> return$;"); |
| 653 | write!(out, " "); |
| 654 | } else if let Some(ret) = &sig.ret { |
| 655 | write!(out, "return "); |
| 656 | match ret { |
| 657 | Type::RustBox(_) => { |
| 658 | write_type(out, ret); |
| 659 | write!(out, "::from_raw("); |
| 660 | } |
| 661 | Type::UniquePtr(_) => { |
| 662 | write_type(out, ret); |
| 663 | write!(out, "("); |
| 664 | } |
| 665 | Type::Ref(_) => write!(out, "*"), |
| 666 | _ => {} |
| 667 | } |
| 668 | } |
| 669 | if sig.throws { |
| 670 | write!(out, "::rust::Str::Repr error$ = "); |
| 671 | } |
| 672 | write!(out, "{}(", invoke); |
| 673 | if sig.receiver.is_some() { |
| 674 | write!(out, "*this"); |
| 675 | } |
| 676 | for (i, arg) in sig.args.iter().enumerate() { |
| 677 | if i > 0 || sig.receiver.is_some() { |
| 678 | write!(out, ", "); |
| 679 | } |
| 680 | match &arg.ty { |
| 681 | Type::Str(_) => write!(out, "::rust::Str::Repr("), |
| 682 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("), |
| 683 | ty if types.needs_indirect_abi(ty) => write!(out, "&"), |
| 684 | _ => {} |
| 685 | } |
| 686 | write!(out, "{}", arg.ident); |
| 687 | match &arg.ty { |
| 688 | Type::RustBox(_) => write!(out, ".into_raw()"), |
| 689 | Type::UniquePtr(_) => write!(out, ".release()"), |
| 690 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"), |
| 691 | ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"), |
| 692 | _ => {} |
| 693 | } |
| 694 | } |
| 695 | if indirect_return { |
| 696 | if !sig.args.is_empty() { |
| 697 | write!(out, ", "); |
| 698 | } |
| 699 | write!(out, "&return$.value"); |
| 700 | } |
| 701 | if indirect_call { |
| 702 | if !sig.args.is_empty() || indirect_return { |
| 703 | write!(out, ", "); |
| 704 | } |
| 705 | write!(out, "extern$"); |
| 706 | } |
| 707 | write!(out, ")"); |
| 708 | if let Some(ret) = &sig.ret { |
| 709 | if let Type::RustBox(_) | Type::UniquePtr(_) = ret { |
| 710 | write!(out, ")"); |
| 711 | } |
| 712 | } |
| 713 | writeln!(out, ";"); |
| 714 | if sig.throws { |
| 715 | writeln!(out, " if (error$.ptr) {{"); |
| 716 | writeln!(out, " throw ::rust::Error(error$);"); |
| 717 | writeln!(out, " }}"); |
| 718 | } |
| 719 | if indirect_return { |
| 720 | out.include.utility = true; |
| 721 | writeln!(out, " return ::std::move(return$.value);"); |
| 722 | } |
| 723 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | fn write_return_type(out: &mut OutFile, ty: &Option<Type>) { |
| 727 | match ty { |
| 728 | None => write!(out, "void "), |
| 729 | Some(ty) => write_type_space(out, ty), |
| 730 | } |
| 731 | } |
| 732 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 733 | fn indirect_return(sig: &Signature, types: &Types) -> bool { |
| 734 | sig.ret |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 735 | .as_ref() |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 736 | .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret)) |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 739 | fn write_indirect_return_type(out: &mut OutFile, ty: &Type) { |
| 740 | match ty { |
| 741 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 742 | write_type_space(out, &ty.inner); |
| 743 | write!(out, "*"); |
| 744 | } |
| 745 | Type::Ref(ty) => { |
| 746 | if ty.mutability.is_none() { |
| 747 | write!(out, "const "); |
| 748 | } |
| 749 | write_type(out, &ty.inner); |
| 750 | write!(out, " *"); |
| 751 | } |
| 752 | Type::Str(_) => write!(out, "::rust::Str::Repr"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 753 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 754 | _ => write_type(out, ty), |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) { |
| 759 | write_indirect_return_type(out, ty); |
| 760 | match ty { |
| 761 | Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 762 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 763 | _ => write_space_after_type(out, ty), |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | 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] | 768 | match ty { |
| 769 | Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => { |
| 770 | write_type_space(out, &ty.inner); |
| 771 | write!(out, "*"); |
| 772 | } |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 773 | Some(Type::Ref(ty)) => { |
| 774 | if ty.mutability.is_none() { |
| 775 | write!(out, "const "); |
| 776 | } |
| 777 | write_type(out, &ty.inner); |
| 778 | write!(out, " *"); |
| 779 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 780 | Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 781 | Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 782 | Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "), |
| 783 | _ => write_return_type(out, ty), |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) { |
| 788 | match &arg.ty { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 789 | Type::RustBox(ty) | Type::UniquePtr(ty) | Type::Vector(ty) => { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 790 | write_type_space(out, &ty.inner); |
| 791 | write!(out, "*"); |
| 792 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 793 | Type::Str(_) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 794 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 795 | _ => write_type_space(out, &arg.ty), |
| 796 | } |
| 797 | if types.needs_indirect_abi(&arg.ty) { |
| 798 | write!(out, "*"); |
| 799 | } |
| 800 | write!(out, "{}", arg.ident); |
| 801 | } |
| 802 | |
| 803 | fn write_type(out: &mut OutFile, ty: &Type) { |
| 804 | match ty { |
| 805 | Type::Ident(ident) => match Atom::from(ident) { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 806 | Some(a) => write!(out, "{}", a.to_cxx()), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 807 | None => write!(out, "{}", ident), |
| 808 | }, |
| 809 | Type::RustBox(ty) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 810 | write!(out, "::rust::Box<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 811 | write_type(out, &ty.inner); |
| 812 | write!(out, ">"); |
| 813 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 814 | Type::RustVec(ty) => { |
| 815 | write!(out, "::rust::Vec<"); |
| 816 | write_type(out, &ty.inner); |
| 817 | write!(out, ">"); |
| 818 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 819 | Type::UniquePtr(ptr) => { |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 820 | write!(out, "::std::unique_ptr<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 821 | write_type(out, &ptr.inner); |
| 822 | write!(out, ">"); |
| 823 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 824 | Type::Vector(ty) => { |
| 825 | write!(out, "::std::vector<"); |
| 826 | write_type(out, &ty.inner); |
| 827 | write!(out, ">"); |
| 828 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 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(_) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 876 | | Type::Vector(_) |
| 877 | | Type::RustVec(_) |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 878 | | Type::SliceRefU8(_) |
| 879 | | Type::Fn(_) => write!(out, " "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 880 | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 881 | Type::Void(_) | Type::Slice(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
| 885 | fn write_generic_instantiations(out: &mut OutFile, types: &Types) { |
| 886 | fn allow_unique_ptr(ident: &Ident) -> bool { |
| 887 | Atom::from(ident).is_none() |
| 888 | } |
| 889 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 890 | fn allow_vector(ident: &Ident) -> bool { |
| 891 | // Note: built-in types such as u8 are already defined in cxx.cc |
| 892 | Atom::from(ident).is_none() |
| 893 | } |
| 894 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 895 | out.begin_block("extern \"C\""); |
| 896 | for ty in types { |
| 897 | if let Type::RustBox(ty) = ty { |
| 898 | if let Type::Ident(inner) = &ty.inner { |
| 899 | out.next_section(); |
| 900 | write_rust_box_extern(out, inner); |
| 901 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 902 | } else if let Type::RustVec(ty) = ty { |
| 903 | if let Type::Ident(_) = &ty.inner { |
| 904 | out.next_section(); |
| 905 | write_rust_vec_extern(out, &ty.inner); |
| 906 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 907 | } else if let Type::UniquePtr(ptr) = ty { |
| 908 | if let Type::Ident(inner) = &ptr.inner { |
| 909 | if allow_unique_ptr(inner) { |
| 910 | out.next_section(); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 911 | write_unique_ptr(out, &ptr.inner, types); |
| 912 | } |
| 913 | } else if let Type::Vector(ptr1) = &ptr.inner { |
| 914 | if let Type::Ident(inner) = &ptr1.inner { |
| 915 | if allow_vector(inner) { |
| 916 | out.next_section(); |
| 917 | write_unique_ptr(out, &ptr.inner, types); |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } else if let Type::Vector(ptr) = ty { |
| 922 | if let Type::Ident(inner) = &ptr.inner { |
| 923 | if allow_vector(inner) { |
| 924 | out.next_section(); |
| 925 | write_vector(out, inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 930 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 931 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 932 | out.begin_block("namespace rust"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 933 | out.begin_block("inline namespace cxxbridge02"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 934 | for ty in types { |
| 935 | if let Type::RustBox(ty) = ty { |
| 936 | if let Type::Ident(inner) = &ty.inner { |
| 937 | write_rust_box_impl(out, inner); |
| 938 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 939 | } else if let Type::RustVec(ty) = ty { |
| 940 | if let Type::Ident(_) = &ty.inner { |
| 941 | write_rust_vec_impl(out, &ty.inner); |
| 942 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 943 | } |
| 944 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 945 | out.end_block("namespace cxxbridge02"); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 946 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) { |
| 950 | let mut inner = String::new(); |
| 951 | for name in &out.namespace { |
| 952 | inner += name; |
| 953 | inner += "::"; |
| 954 | } |
| 955 | inner += &ident.to_string(); |
| 956 | let instance = inner.replace("::", "$"); |
| 957 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 958 | writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance); |
| 959 | writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 960 | writeln!( |
| 961 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 962 | "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 963 | instance, inner, |
| 964 | ); |
| 965 | writeln!( |
| 966 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 967 | "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 968 | instance, inner, |
| 969 | ); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 970 | writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 971 | } |
| 972 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 973 | fn write_rust_vec_extern(out: &mut OutFile, ty: &Type) { |
| 974 | let namespace = out.namespace.iter().cloned().collect::<Vec<String>>(); |
| 975 | let inner = ty.to_typename(&namespace); |
| 976 | let instance = ty.to_mangled(&namespace); |
| 977 | |
| 978 | writeln!(out, "#ifndef CXXBRIDGE02_RUST_VEC_{}", instance); |
| 979 | writeln!(out, "#define CXXBRIDGE02_RUST_VEC_{}", instance); |
| 980 | writeln!( |
| 981 | out, |
| 982 | "void cxxbridge02$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;", |
| 983 | instance, inner, |
| 984 | ); |
| 985 | writeln!( |
| 986 | out, |
| 987 | "void cxxbridge02$rust_vec${}$vector_from(const ::rust::Vec<{}> *ptr, const std::vector<{}> &vector) noexcept;", |
| 988 | instance, inner, inner |
| 989 | ); |
| 990 | writeln!( |
| 991 | out, |
| 992 | "size_t cxxbridge02$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;", |
| 993 | instance, inner, |
| 994 | ); |
| 995 | writeln!(out, "#endif // CXXBRIDGE02_RUST_VEC_{}", instance); |
| 996 | } |
| 997 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 998 | fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) { |
| 999 | let mut inner = String::new(); |
| 1000 | for name in &out.namespace { |
| 1001 | inner += name; |
| 1002 | inner += "::"; |
| 1003 | } |
| 1004 | inner += &ident.to_string(); |
| 1005 | let instance = inner.replace("::", "$"); |
| 1006 | |
| 1007 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 1008 | writeln!(out, "void Box<{}>::uninit() noexcept {{", inner); |
| David Tolnay | 737e02e | 2020-04-04 21:52:46 -0700 | [diff] [blame] | 1009 | writeln!(out, " cxxbridge02$box${}$uninit(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1010 | writeln!(out, "}}"); |
| 1011 | |
| 1012 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 1013 | writeln!(out, "void Box<{}>::drop() noexcept {{", inner); |
| David Tolnay | 737e02e | 2020-04-04 21:52:46 -0700 | [diff] [blame] | 1014 | writeln!(out, " cxxbridge02$box${}$drop(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1015 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 1018 | fn write_rust_vec_impl(out: &mut OutFile, ty: &Type) { |
| 1019 | let namespace = out.namespace.iter().cloned().collect::<Vec<String>>(); |
| 1020 | let inner = ty.to_typename(&namespace); |
| 1021 | let instance = ty.to_mangled(&namespace); |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 1022 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 1023 | writeln!(out, "template <>"); |
| 1024 | writeln!(out, "void Vec<{}>::drop() noexcept {{", inner); |
| 1025 | writeln!( |
| 1026 | out, |
| 1027 | " return cxxbridge02$rust_vec${}$drop(this);", |
| 1028 | instance |
| 1029 | ); |
| 1030 | writeln!(out, "}}"); |
| 1031 | |
| 1032 | writeln!(out, "template <>"); |
| 1033 | writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner); |
| 1034 | writeln!(out, " return cxxbridge02$rust_vec${}$len(this);", instance); |
| 1035 | writeln!(out, "}}"); |
| 1036 | |
| 1037 | writeln!(out, "template <>"); |
| 1038 | writeln!( |
| 1039 | out, |
| 1040 | "Vec<{}>::operator std::vector<{}>() const noexcept {{", |
| 1041 | inner, inner |
| 1042 | ); |
| 1043 | writeln!( |
| 1044 | out, |
| 1045 | " std::vector<{}> v; v.reserve(this->size()); cxxbridge02$rust_vec${}$vector_from(this, v); return v;", |
| 1046 | inner, instance, |
| 1047 | ); |
| 1048 | writeln!(out, "}}"); |
| 1049 | } |
| 1050 | |
| 1051 | fn write_unique_ptr(out: &mut OutFile, ty: &Type, types: &Types) { |
| 1052 | out.include.utility = true; |
| 1053 | let namespace = out.namespace.iter().cloned().collect::<Vec<String>>(); |
| 1054 | let inner = ty.to_typename(&namespace); |
| 1055 | let instance = ty.to_mangled(&namespace); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1056 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1057 | writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| 1058 | writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1059 | writeln!( |
| 1060 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1061 | "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1062 | inner, |
| 1063 | ); |
| 1064 | writeln!( |
| 1065 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1066 | "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1067 | inner, |
| 1068 | ); |
| 1069 | writeln!( |
| 1070 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1071 | "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1072 | instance, inner, |
| 1073 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1074 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1075 | writeln!(out, "}}"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 1076 | match ty { |
| 1077 | Type::Ident(ident) if types.structs.contains_key(ident) => { |
| 1078 | writeln!( |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1079 | out, |
| 1080 | "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{", |
| 1081 | instance, inner, inner, |
| 1082 | ); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 1083 | writeln!( |
| 1084 | out, |
| 1085 | " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));", |
| 1086 | inner, inner, |
| 1087 | ); |
| 1088 | writeln!(out, "}}"); |
| 1089 | } |
| 1090 | _ => (), |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1091 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1092 | writeln!( |
| 1093 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1094 | "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1095 | instance, inner, inner, |
| 1096 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1097 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1098 | writeln!(out, "}}"); |
| 1099 | writeln!( |
| 1100 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1101 | "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1102 | inner, instance, inner, |
| 1103 | ); |
| 1104 | writeln!(out, " return ptr.get();"); |
| 1105 | writeln!(out, "}}"); |
| 1106 | writeln!( |
| 1107 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1108 | "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1109 | inner, instance, inner, |
| 1110 | ); |
| 1111 | writeln!(out, " return ptr.release();"); |
| 1112 | writeln!(out, "}}"); |
| 1113 | writeln!( |
| 1114 | out, |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1115 | "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1116 | instance, inner, |
| 1117 | ); |
| 1118 | writeln!(out, " ptr->~unique_ptr();"); |
| 1119 | writeln!(out, "}}"); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 1120 | writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1121 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 1122 | |
| 1123 | fn write_vector(out: &mut OutFile, ident: &Ident) { |
| 1124 | let mut inner = String::new(); |
| 1125 | // Do not apply namespace to built-in type |
| 1126 | let is_user_type = Atom::from(ident).is_none(); |
| 1127 | if is_user_type { |
| 1128 | for name in &out.namespace { |
| 1129 | inner += name; |
| 1130 | inner += "::"; |
| 1131 | } |
| 1132 | } |
| 1133 | let mut instance = inner.clone(); |
| 1134 | if let Some(ti) = Atom::from(ident) { |
| 1135 | inner += ti.to_cxx(); |
| 1136 | } else { |
| 1137 | inner += &ident.to_string(); |
| 1138 | }; |
| 1139 | instance += &ident.to_string(); |
| 1140 | let instance = instance.replace("::", "$"); |
| 1141 | |
| 1142 | writeln!(out, "#ifndef CXXBRIDGE02_vector_{}", instance); |
| 1143 | writeln!(out, "#define CXXBRIDGE02_vector_{}", instance); |
| 1144 | writeln!( |
| 1145 | out, |
| 1146 | "size_t cxxbridge02$std$vector${}$length(const std::vector<{}> &s) noexcept {{", |
| 1147 | instance, inner, |
| 1148 | ); |
| 1149 | writeln!(out, " return s.size();"); |
| 1150 | writeln!(out, "}}"); |
| 1151 | |
| 1152 | writeln!( |
| 1153 | out, |
| 1154 | "void cxxbridge02$std$vector${}$push_back(std::vector<{}> &s, const {} &item) noexcept {{", |
| 1155 | instance, inner, inner |
| 1156 | ); |
| 1157 | writeln!(out, " s.push_back(item);"); |
| 1158 | writeln!(out, "}}"); |
| 1159 | |
| 1160 | writeln!( |
| 1161 | out, |
| 1162 | "const {} *cxxbridge02$std$vector${}$get_unchecked(const std::vector<{}> &s, size_t pos) noexcept {{", |
| 1163 | inner, instance, inner, |
| 1164 | ); |
| 1165 | writeln!(out, " return &s[pos];"); |
| 1166 | writeln!(out, "}}"); |
| 1167 | writeln!(out, "#endif // CXXBRIDGE02_vector_{}", instance); |
| 1168 | } |