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