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