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