| 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 | 91e87fa | 2020-05-11 19:10:23 -0700 | [diff] [blame] | 27 | out.include.insert(include); |
| 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 | 32ee9d3 | 2020-05-12 20:01:09 -0700 | [diff] [blame] | 247 | if needs_rust_string || needs_rust_vec { |
| David Tolnay | d140274 | 2020-03-25 22:21:42 -0700 | [diff] [blame] | 248 | out.next_section(); |
| 249 | writeln!(out, "struct unsafe_bitcopy_t;"); |
| 250 | } |
| 251 | |
| David Tolnay | e54f338 | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 252 | include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING"); |
| 253 | include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR"); |
| 254 | include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE"); |
| 255 | include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX"); |
| 256 | include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC"); |
| 257 | include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN"); |
| 258 | include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR"); |
| 259 | include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE"); |
| 260 | include::write(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 | |
| 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 | |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 352 | fn write_enum(out: &mut OutFile, enm: &Enum) { |
| 353 | for line in enm.doc.to_string().lines() { |
| 354 | writeln!(out, "//{}", line); |
| 355 | } |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 356 | write!(out, "enum class {} : ", enm.ident); |
| 357 | write_atom(out, enm.repr); |
| 358 | writeln!(out, " {{"); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 359 | for variant in &enm.variants { |
| Joel Galenson | 8854773 | 2020-05-05 08:23:42 -0700 | [diff] [blame] | 360 | writeln!(out, " {} = {},", variant.ident, variant.discriminant); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 361 | } |
| 362 | writeln!(out, "}};"); |
| 363 | } |
| 364 | |
| Joel Galenson | 905eb2e | 2020-05-04 14:58:14 -0700 | [diff] [blame] | 365 | fn check_enum(out: &mut OutFile, enm: &Enum) { |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 366 | write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident); |
| 367 | write_atom(out, enm.repr); |
| 368 | writeln!(out, "), \"incorrect size\");"); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 369 | for variant in &enm.variants { |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 370 | write!(out, "static_assert(static_cast<"); |
| 371 | write_atom(out, enm.repr); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 372 | writeln!( |
| 373 | out, |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 374 | ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");", |
| Joel Galenson | 8854773 | 2020-05-05 08:23:42 -0700 | [diff] [blame] | 375 | enm.ident, variant.ident, variant.discriminant, |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 376 | ); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 377 | } |
| Joel Galenson | 905eb2e | 2020-05-04 14:58:14 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 380 | fn write_exception_glue(out: &mut OutFile, apis: &[Api]) { |
| 381 | let mut has_cxx_throws = false; |
| 382 | for api in apis { |
| 383 | if let Api::CxxFunction(efn) = api { |
| 384 | if efn.throws { |
| 385 | has_cxx_throws = true; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if has_cxx_throws { |
| 392 | out.next_section(); |
| David Tolnay | e68634c | 2020-03-18 12:03:40 -0700 | [diff] [blame] | 393 | writeln!( |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 394 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 395 | "const char *cxxbridge03$exception(const char *, size_t);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 396 | ); |
| 397 | } |
| 398 | } |
| 399 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 400 | fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 401 | if efn.throws { |
| 402 | write!(out, "::rust::Str::Repr "); |
| 403 | } else { |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 404 | write_extern_return_type_space(out, &efn.ret, types); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 405 | } |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 406 | let mangled = mangle::extern_fn(&out.namespace, efn); |
| 407 | write!(out, "{}(", mangled); |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 408 | if let Some(receiver) = &efn.receiver { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 409 | if receiver.mutability.is_none() { |
| 410 | write!(out, "const "); |
| 411 | } |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 412 | write!(out, "{} &self", receiver.ty); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 413 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 414 | for (i, arg) in efn.args.iter().enumerate() { |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 415 | if i > 0 || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 416 | write!(out, ", "); |
| 417 | } |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 418 | if arg.ty == RustString { |
| 419 | write!(out, "const "); |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 420 | } else if let Type::RustVec(_) = arg.ty { |
| 421 | write!(out, "const "); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 422 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 423 | write_extern_arg(out, arg, types); |
| 424 | } |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 425 | let indirect_return = indirect_return(efn, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 426 | if indirect_return { |
| 427 | if !efn.args.is_empty() { |
| 428 | write!(out, ", "); |
| 429 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 430 | write_indirect_return_type_space(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 431 | write!(out, "*return$"); |
| 432 | } |
| 433 | writeln!(out, ") noexcept {{"); |
| 434 | write!(out, " "); |
| 435 | write_return_type(out, &efn.ret); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 436 | match &efn.receiver { |
| 437 | None => write!(out, "(*{}$)(", efn.ident), |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 438 | Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 439 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 440 | for (i, arg) in efn.args.iter().enumerate() { |
| 441 | if i > 0 { |
| 442 | write!(out, ", "); |
| 443 | } |
| 444 | write_type(out, &arg.ty); |
| 445 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 446 | write!(out, ")"); |
| David Tolnay | 4e7123f | 2020-04-19 21:11:37 -0700 | [diff] [blame] | 447 | if let Some(receiver) = &efn.receiver { |
| 448 | if receiver.mutability.is_none() { |
| 449 | write!(out, " const"); |
| 450 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 451 | } |
| 452 | write!(out, " = "); |
| 453 | match &efn.receiver { |
| 454 | None => write!(out, "{}", efn.ident), |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 455 | Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 456 | } |
| 457 | writeln!(out, ";"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 458 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 459 | if efn.throws { |
| 460 | writeln!(out, "::rust::Str::Repr throw$;"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 461 | writeln!(out, " ::rust::behavior::trycatch("); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 462 | writeln!(out, " [&] {{"); |
| 463 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 464 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 465 | if indirect_return { |
| 466 | write!(out, "new (return$) "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 467 | write_indirect_return_type(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 468 | write!(out, "("); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 469 | } else if efn.ret.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 470 | write!(out, "return "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 471 | } |
| 472 | match &efn.ret { |
| 473 | Some(Type::Ref(_)) => write!(out, "&"), |
| 474 | Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("), |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 475 | Some(Type::SliceRefU8(_)) if !indirect_return => { |
| 476 | write!(out, "::rust::Slice<uint8_t>::Repr(") |
| 477 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 478 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 479 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 480 | match &efn.receiver { |
| 481 | None => write!(out, "{}$(", efn.ident), |
| David Tolnay | 41909e6 | 2020-04-20 00:55:15 -0700 | [diff] [blame] | 482 | Some(_) => write!(out, "(self.*{}$)(", efn.ident), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 483 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 484 | for (i, arg) in efn.args.iter().enumerate() { |
| 485 | if i > 0 { |
| 486 | write!(out, ", "); |
| 487 | } |
| 488 | if let Type::RustBox(_) = &arg.ty { |
| 489 | write_type(out, &arg.ty); |
| 490 | write!(out, "::from_raw({})", arg.ident); |
| 491 | } else if let Type::UniquePtr(_) = &arg.ty { |
| 492 | write_type(out, &arg.ty); |
| 493 | write!(out, "({})", arg.ident); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 494 | } else if arg.ty == RustString { |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 495 | write!( |
| 496 | out, |
| 497 | "::rust::String(::rust::unsafe_bitcopy, *{})", |
| 498 | arg.ident, |
| 499 | ); |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 500 | } else if let Type::RustVec(_) = arg.ty { |
| 501 | write_type(out, &arg.ty); |
| 502 | write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 503 | } else if types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 504 | out.include.utility = true; |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 505 | write!(out, "::std::move(*{})", arg.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 506 | } else { |
| 507 | write!(out, "{}", arg.ident); |
| 508 | } |
| 509 | } |
| 510 | write!(out, ")"); |
| 511 | match &efn.ret { |
| 512 | Some(Type::RustBox(_)) => write!(out, ".into_raw()"), |
| 513 | Some(Type::UniquePtr(_)) => write!(out, ".release()"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 514 | Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 515 | _ => {} |
| 516 | } |
| 517 | if indirect_return { |
| 518 | write!(out, ")"); |
| 519 | } |
| 520 | writeln!(out, ";"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 521 | if efn.throws { |
| 522 | out.include.cstring = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 523 | writeln!(out, " throw$.ptr = nullptr;"); |
| 524 | writeln!(out, " }},"); |
| David Tolnay | 82c1617 | 2020-03-17 22:54:12 -0700 | [diff] [blame] | 525 | writeln!(out, " [&](const char *catch$) noexcept {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 526 | writeln!(out, " throw$.len = ::std::strlen(catch$);"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 527 | writeln!( |
| 528 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 529 | " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 530 | ); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 531 | writeln!(out, " }});"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 532 | writeln!(out, " return throw$;"); |
| 533 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 534 | writeln!(out, "}}"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 535 | for arg in &efn.args { |
| 536 | if let Type::Fn(f) = &arg.ty { |
| 537 | let var = &arg.ident; |
| 538 | write_function_pointer_trampoline(out, efn, var, f, types); |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | fn write_function_pointer_trampoline( |
| 544 | out: &mut OutFile, |
| 545 | efn: &ExternFn, |
| 546 | var: &Ident, |
| 547 | f: &Signature, |
| 548 | types: &Types, |
| 549 | ) { |
| 550 | out.next_section(); |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 551 | let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 552 | let indirect_call = true; |
| 553 | write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call); |
| 554 | |
| 555 | out.next_section(); |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 556 | let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string(); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 557 | 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] | 558 | } |
| 559 | |
| 560 | fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 561 | let link_name = mangle::extern_fn(&out.namespace, efn); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 562 | let indirect_call = false; |
| 563 | write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call); |
| 564 | } |
| 565 | |
| 566 | fn write_rust_function_decl_impl( |
| 567 | out: &mut OutFile, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 568 | link_name: &Symbol, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 569 | sig: &Signature, |
| 570 | types: &Types, |
| 571 | indirect_call: bool, |
| 572 | ) { |
| 573 | if sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 574 | write!(out, "::rust::Str::Repr "); |
| 575 | } else { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 576 | write_extern_return_type_space(out, &sig.ret, types); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 577 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 578 | write!(out, "{}(", link_name); |
| 579 | let mut needs_comma = false; |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 580 | if let Some(receiver) = &sig.receiver { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 581 | if receiver.mutability.is_none() { |
| 582 | write!(out, "const "); |
| 583 | } |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 584 | write!(out, "{} &self", receiver.ty); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 585 | needs_comma = true; |
| 586 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 587 | for arg in &sig.args { |
| 588 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 589 | write!(out, ", "); |
| 590 | } |
| 591 | write_extern_arg(out, arg, types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 592 | needs_comma = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 593 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 594 | if indirect_return(sig, types) { |
| 595 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 596 | write!(out, ", "); |
| 597 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 598 | write_return_type(out, &sig.ret); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 599 | write!(out, "*return$"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 600 | needs_comma = true; |
| 601 | } |
| 602 | if indirect_call { |
| 603 | if needs_comma { |
| 604 | write!(out, ", "); |
| 605 | } |
| 606 | write!(out, "void *"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 607 | } |
| 608 | writeln!(out, ") noexcept;"); |
| 609 | } |
| 610 | |
| 611 | fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 612 | for line in efn.doc.to_string().lines() { |
| 613 | writeln!(out, "//{}", line); |
| 614 | } |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 615 | let local_name = match &efn.sig.receiver { |
| 616 | None => efn.ident.to_string(), |
| David Tolnay | 05e11cc | 2020-04-20 02:13:56 -0700 | [diff] [blame] | 617 | Some(receiver) => format!("{}::{}", receiver.ty, efn.ident), |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 618 | }; |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 619 | let invoke = mangle::extern_fn(&out.namespace, efn); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 620 | let indirect_call = false; |
| 621 | write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call); |
| 622 | } |
| 623 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 624 | fn write_rust_function_shim_decl( |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 625 | out: &mut OutFile, |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 626 | local_name: &str, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 627 | sig: &Signature, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 628 | indirect_call: bool, |
| 629 | ) { |
| 630 | write_return_type(out, &sig.ret); |
| 631 | write!(out, "{}(", local_name); |
| 632 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 633 | if i > 0 { |
| 634 | write!(out, ", "); |
| 635 | } |
| 636 | write_type_space(out, &arg.ty); |
| 637 | write!(out, "{}", arg.ident); |
| 638 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 639 | if indirect_call { |
| 640 | if !sig.args.is_empty() { |
| 641 | write!(out, ", "); |
| 642 | } |
| 643 | write!(out, "void *extern$"); |
| 644 | } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 645 | write!(out, ")"); |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 646 | if let Some(receiver) = &sig.receiver { |
| 647 | if receiver.mutability.is_none() { |
| 648 | write!(out, " const"); |
| 649 | } |
| 650 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 651 | if !sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 652 | write!(out, " noexcept"); |
| 653 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | fn write_rust_function_shim_impl( |
| 657 | out: &mut OutFile, |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 658 | local_name: &str, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 659 | sig: &Signature, |
| 660 | types: &Types, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 661 | invoke: &Symbol, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 662 | indirect_call: bool, |
| 663 | ) { |
| 664 | if out.header && sig.receiver.is_some() { |
| 665 | // We've already defined this inside the struct. |
| 666 | return; |
| 667 | } |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 668 | write_rust_function_shim_decl(out, local_name, sig, indirect_call); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 669 | if out.header { |
| 670 | writeln!(out, ";"); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 671 | return; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 672 | } |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 673 | writeln!(out, " {{"); |
| 674 | for arg in &sig.args { |
| 675 | if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) { |
| 676 | out.include.utility = true; |
| 677 | write!(out, " ::rust::ManuallyDrop<"); |
| 678 | write_type(out, &arg.ty); |
| 679 | writeln!(out, "> {}$(::std::move({0}));", arg.ident); |
| 680 | } |
| 681 | } |
| 682 | write!(out, " "); |
| 683 | let indirect_return = indirect_return(sig, types); |
| 684 | if indirect_return { |
| 685 | write!(out, "::rust::MaybeUninit<"); |
| 686 | write_type(out, sig.ret.as_ref().unwrap()); |
| 687 | writeln!(out, "> return$;"); |
| 688 | write!(out, " "); |
| 689 | } else if let Some(ret) = &sig.ret { |
| 690 | write!(out, "return "); |
| 691 | match ret { |
| 692 | Type::RustBox(_) => { |
| 693 | write_type(out, ret); |
| 694 | write!(out, "::from_raw("); |
| 695 | } |
| 696 | Type::UniquePtr(_) => { |
| 697 | write_type(out, ret); |
| 698 | write!(out, "("); |
| 699 | } |
| 700 | Type::Ref(_) => write!(out, "*"), |
| 701 | _ => {} |
| 702 | } |
| 703 | } |
| 704 | if sig.throws { |
| 705 | write!(out, "::rust::Str::Repr error$ = "); |
| 706 | } |
| 707 | write!(out, "{}(", invoke); |
| 708 | if sig.receiver.is_some() { |
| 709 | write!(out, "*this"); |
| 710 | } |
| 711 | for (i, arg) in sig.args.iter().enumerate() { |
| 712 | if i > 0 || sig.receiver.is_some() { |
| 713 | write!(out, ", "); |
| 714 | } |
| 715 | match &arg.ty { |
| 716 | Type::Str(_) => write!(out, "::rust::Str::Repr("), |
| 717 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("), |
| 718 | ty if types.needs_indirect_abi(ty) => write!(out, "&"), |
| 719 | _ => {} |
| 720 | } |
| 721 | write!(out, "{}", arg.ident); |
| 722 | match &arg.ty { |
| 723 | Type::RustBox(_) => write!(out, ".into_raw()"), |
| 724 | Type::UniquePtr(_) => write!(out, ".release()"), |
| 725 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"), |
| 726 | ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"), |
| 727 | _ => {} |
| 728 | } |
| 729 | } |
| 730 | if indirect_return { |
| 731 | if !sig.args.is_empty() { |
| 732 | write!(out, ", "); |
| 733 | } |
| 734 | write!(out, "&return$.value"); |
| 735 | } |
| 736 | if indirect_call { |
| 737 | if !sig.args.is_empty() || indirect_return { |
| 738 | write!(out, ", "); |
| 739 | } |
| 740 | write!(out, "extern$"); |
| 741 | } |
| 742 | write!(out, ")"); |
| 743 | if let Some(ret) = &sig.ret { |
| 744 | if let Type::RustBox(_) | Type::UniquePtr(_) = ret { |
| 745 | write!(out, ")"); |
| 746 | } |
| 747 | } |
| 748 | writeln!(out, ";"); |
| 749 | if sig.throws { |
| 750 | writeln!(out, " if (error$.ptr) {{"); |
| 751 | writeln!(out, " throw ::rust::Error(error$);"); |
| 752 | writeln!(out, " }}"); |
| 753 | } |
| 754 | if indirect_return { |
| 755 | out.include.utility = true; |
| 756 | writeln!(out, " return ::std::move(return$.value);"); |
| 757 | } |
| 758 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | fn write_return_type(out: &mut OutFile, ty: &Option<Type>) { |
| 762 | match ty { |
| 763 | None => write!(out, "void "), |
| 764 | Some(ty) => write_type_space(out, ty), |
| 765 | } |
| 766 | } |
| 767 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 768 | fn indirect_return(sig: &Signature, types: &Types) -> bool { |
| 769 | sig.ret |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 770 | .as_ref() |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 771 | .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret)) |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 774 | fn write_indirect_return_type(out: &mut OutFile, ty: &Type) { |
| 775 | match ty { |
| 776 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| 777 | write_type_space(out, &ty.inner); |
| 778 | write!(out, "*"); |
| 779 | } |
| 780 | Type::Ref(ty) => { |
| 781 | if ty.mutability.is_none() { |
| 782 | write!(out, "const "); |
| 783 | } |
| 784 | write_type(out, &ty.inner); |
| 785 | write!(out, " *"); |
| 786 | } |
| 787 | Type::Str(_) => write!(out, "::rust::Str::Repr"), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 788 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 789 | _ => write_type(out, ty), |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) { |
| 794 | write_indirect_return_type(out, ty); |
| 795 | match ty { |
| 796 | Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 797 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 798 | _ => write_space_after_type(out, ty), |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | 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] | 803 | match ty { |
| 804 | Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => { |
| 805 | write_type_space(out, &ty.inner); |
| 806 | write!(out, "*"); |
| 807 | } |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 808 | Some(Type::Ref(ty)) => { |
| 809 | if ty.mutability.is_none() { |
| 810 | write!(out, "const "); |
| 811 | } |
| 812 | write_type(out, &ty.inner); |
| 813 | write!(out, " *"); |
| 814 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 815 | Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 816 | Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 817 | Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "), |
| 818 | _ => write_return_type(out, ty), |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) { |
| 823 | match &arg.ty { |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 824 | Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 825 | write_type_space(out, &ty.inner); |
| 826 | write!(out, "*"); |
| 827 | } |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 828 | Type::Str(_) => write!(out, "::rust::Str::Repr "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 829 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 830 | _ => write_type_space(out, &arg.ty), |
| 831 | } |
| 832 | if types.needs_indirect_abi(&arg.ty) { |
| 833 | write!(out, "*"); |
| 834 | } |
| 835 | write!(out, "{}", arg.ident); |
| 836 | } |
| 837 | |
| 838 | fn write_type(out: &mut OutFile, ty: &Type) { |
| 839 | match ty { |
| 840 | Type::Ident(ident) => match Atom::from(ident) { |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 841 | Some(atom) => write_atom(out, atom), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 842 | None => write!(out, "{}", ident), |
| 843 | }, |
| 844 | Type::RustBox(ty) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 845 | write!(out, "::rust::Box<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 846 | write_type(out, &ty.inner); |
| 847 | write!(out, ">"); |
| 848 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 849 | Type::RustVec(ty) => { |
| 850 | write!(out, "::rust::Vec<"); |
| 851 | write_type(out, &ty.inner); |
| 852 | write!(out, ">"); |
| 853 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 854 | Type::UniquePtr(ptr) => { |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 855 | write!(out, "::std::unique_ptr<"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 856 | write_type(out, &ptr.inner); |
| 857 | write!(out, ">"); |
| 858 | } |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 859 | Type::CxxVector(ty) => { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 860 | write!(out, "::std::vector<"); |
| 861 | write_type(out, &ty.inner); |
| 862 | write!(out, ">"); |
| 863 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 864 | Type::Ref(r) => { |
| 865 | if r.mutability.is_none() { |
| 866 | write!(out, "const "); |
| 867 | } |
| 868 | write_type(out, &r.inner); |
| 869 | write!(out, " &"); |
| 870 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 871 | Type::Slice(_) => { |
| 872 | // For now, only U8 slices are supported, which are covered separately below |
| 873 | unreachable!() |
| 874 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 875 | Type::Str(_) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 876 | write!(out, "::rust::Str"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 877 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 878 | Type::SliceRefU8(_) => { |
| 879 | write!(out, "::rust::Slice<uint8_t>"); |
| 880 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 881 | Type::Fn(f) => { |
| 882 | write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" }); |
| 883 | match &f.ret { |
| 884 | Some(ret) => write_type(out, ret), |
| 885 | None => write!(out, "void"), |
| 886 | } |
| 887 | write!(out, "("); |
| 888 | for (i, arg) in f.args.iter().enumerate() { |
| 889 | if i > 0 { |
| 890 | write!(out, ", "); |
| 891 | } |
| 892 | write_type(out, &arg.ty); |
| 893 | } |
| 894 | write!(out, ")>"); |
| 895 | } |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 896 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 900 | fn write_atom(out: &mut OutFile, atom: Atom) { |
| 901 | match atom { |
| 902 | Bool => write!(out, "bool"), |
| 903 | U8 => write!(out, "uint8_t"), |
| 904 | U16 => write!(out, "uint16_t"), |
| 905 | U32 => write!(out, "uint32_t"), |
| 906 | U64 => write!(out, "uint64_t"), |
| 907 | Usize => write!(out, "size_t"), |
| 908 | I8 => write!(out, "int8_t"), |
| 909 | I16 => write!(out, "int16_t"), |
| 910 | I32 => write!(out, "int32_t"), |
| 911 | I64 => write!(out, "int64_t"), |
| 912 | Isize => write!(out, "::rust::isize"), |
| 913 | F32 => write!(out, "float"), |
| 914 | F64 => write!(out, "double"), |
| 915 | CxxString => write!(out, "::std::string"), |
| 916 | RustString => write!(out, "::rust::String"), |
| 917 | } |
| 918 | } |
| 919 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 920 | fn write_type_space(out: &mut OutFile, ty: &Type) { |
| 921 | write_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 922 | write_space_after_type(out, ty); |
| 923 | } |
| 924 | |
| 925 | fn write_space_after_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 926 | match ty { |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 927 | Type::Ident(_) |
| 928 | | Type::RustBox(_) |
| 929 | | Type::UniquePtr(_) |
| 930 | | Type::Str(_) |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 931 | | Type::CxxVector(_) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 932 | | Type::RustVec(_) |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 933 | | Type::SliceRefU8(_) |
| 934 | | Type::Fn(_) => write!(out, " "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 935 | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 936 | Type::Void(_) | Type::Slice(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
| David Tolnay | cd08c44 | 2020-04-25 10:16:33 -0700 | [diff] [blame] | 940 | // Only called for legal referent types of unique_ptr and element types of |
| 941 | // std::vector and Vec. |
| David Tolnay | 2eca4a0 | 2020-04-24 19:50:51 -0700 | [diff] [blame] | 942 | fn to_typename(namespace: &Namespace, ty: &Type) -> String { |
| 943 | match ty { |
| 944 | Type::Ident(ident) => { |
| David Tolnay | cd08c44 | 2020-04-25 10:16:33 -0700 | [diff] [blame] | 945 | let mut path = String::new(); |
| 946 | for name in namespace { |
| David Tolnay | 63f92e8 | 2020-04-30 20:40:20 -0700 | [diff] [blame] | 947 | path += &name.to_string(); |
| David Tolnay | cd08c44 | 2020-04-25 10:16:33 -0700 | [diff] [blame] | 948 | path += "::"; |
| David Tolnay | 2eca4a0 | 2020-04-24 19:50:51 -0700 | [diff] [blame] | 949 | } |
| David Tolnay | cd08c44 | 2020-04-25 10:16:33 -0700 | [diff] [blame] | 950 | path += &ident.to_string(); |
| 951 | path |
| David Tolnay | 2eca4a0 | 2020-04-24 19:50:51 -0700 | [diff] [blame] | 952 | } |
| 953 | Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)), |
| David Tolnay | cd08c44 | 2020-04-25 10:16:33 -0700 | [diff] [blame] | 954 | _ => unreachable!(), |
| David Tolnay | 2eca4a0 | 2020-04-24 19:50:51 -0700 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
| David Tolnay | acdf20a | 2020-04-25 12:40:53 -0700 | [diff] [blame] | 958 | // Only called for legal referent types of unique_ptr and element types of |
| 959 | // std::vector and Vec. |
| David Tolnay | bae50ef | 2020-04-25 12:38:41 -0700 | [diff] [blame] | 960 | fn to_mangled(namespace: &Namespace, ty: &Type) -> String { |
| 961 | match ty { |
| David Tolnay | acdf20a | 2020-04-25 12:40:53 -0700 | [diff] [blame] | 962 | Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"), |
| David Tolnay | bae50ef | 2020-04-25 12:38:41 -0700 | [diff] [blame] | 963 | Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)), |
| David Tolnay | acdf20a | 2020-04-25 12:40:53 -0700 | [diff] [blame] | 964 | _ => unreachable!(), |
| David Tolnay | bae50ef | 2020-04-25 12:38:41 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 968 | fn write_generic_instantiations(out: &mut OutFile, types: &Types) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 969 | out.begin_block("extern \"C\""); |
| 970 | for ty in types { |
| 971 | if let Type::RustBox(ty) = ty { |
| 972 | if let Type::Ident(inner) = &ty.inner { |
| 973 | out.next_section(); |
| 974 | write_rust_box_extern(out, inner); |
| 975 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 976 | } else if let Type::RustVec(ty) = ty { |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 977 | if let Type::Ident(inner) = &ty.inner { |
| 978 | if Atom::from(inner).is_none() { |
| 979 | out.next_section(); |
| 980 | write_rust_vec_extern(out, inner); |
| 981 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 982 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 983 | } else if let Type::UniquePtr(ptr) = ty { |
| 984 | if let Type::Ident(inner) = &ptr.inner { |
| David Tolnay | 2b821e6 | 2020-05-07 22:55:28 -0700 | [diff] [blame] | 985 | if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 986 | out.next_section(); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 987 | write_unique_ptr(out, inner, types); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 988 | } |
| 989 | } |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 990 | } else if let Type::CxxVector(ptr) = ty { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 991 | if let Type::Ident(inner) = &ptr.inner { |
| David Tolnay | 2b821e6 | 2020-05-07 22:55:28 -0700 | [diff] [blame] | 992 | if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 993 | out.next_section(); |
| David Tolnay | 92105da | 2020-04-25 11:15:31 -0700 | [diff] [blame] | 994 | write_cxx_vector(out, ty, inner, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | } |
| 998 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 999 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1000 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 1001 | out.begin_block("namespace rust"); |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1002 | out.begin_block("inline namespace cxxbridge03"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1003 | for ty in types { |
| 1004 | if let Type::RustBox(ty) = ty { |
| 1005 | if let Type::Ident(inner) = &ty.inner { |
| 1006 | write_rust_box_impl(out, inner); |
| 1007 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1008 | } else if let Type::RustVec(ty) = ty { |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1009 | if let Type::Ident(inner) = &ty.inner { |
| 1010 | if Atom::from(inner).is_none() { |
| 1011 | write_rust_vec_impl(out, inner); |
| 1012 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1013 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1014 | } |
| 1015 | } |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1016 | out.end_block("namespace cxxbridge03"); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 1017 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) { |
| 1021 | let mut inner = String::new(); |
| 1022 | for name in &out.namespace { |
| David Tolnay | 63f92e8 | 2020-04-30 20:40:20 -0700 | [diff] [blame] | 1023 | inner += &name.to_string(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1024 | inner += "::"; |
| 1025 | } |
| 1026 | inner += &ident.to_string(); |
| 1027 | let instance = inner.replace("::", "$"); |
| 1028 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1029 | writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance); |
| 1030 | writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1031 | writeln!( |
| 1032 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1033 | "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1034 | instance, inner, |
| 1035 | ); |
| 1036 | writeln!( |
| 1037 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1038 | "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1039 | instance, inner, |
| 1040 | ); |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1041 | writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1042 | } |
| 1043 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1044 | fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) { |
| 1045 | let element = Type::Ident(element.clone()); |
| 1046 | let inner = to_typename(&out.namespace, &element); |
| 1047 | let instance = to_mangled(&out.namespace, &element); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1048 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1049 | writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance); |
| 1050 | writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1051 | writeln!( |
| 1052 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1053 | "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;", |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1054 | instance, inner, |
| 1055 | ); |
| 1056 | writeln!( |
| 1057 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1058 | "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1059 | instance, inner, |
| 1060 | ); |
| 1061 | writeln!( |
| 1062 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1063 | "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1064 | instance, inner, |
| 1065 | ); |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1066 | writeln!( |
| 1067 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1068 | "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;", |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1069 | inner, instance, |
| 1070 | ); |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1071 | writeln!( |
| 1072 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1073 | "size_t cxxbridge03$rust_vec${}$stride() noexcept;", |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1074 | instance, |
| 1075 | ); |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1076 | writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1079 | fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) { |
| 1080 | let mut inner = String::new(); |
| 1081 | for name in &out.namespace { |
| David Tolnay | 63f92e8 | 2020-04-30 20:40:20 -0700 | [diff] [blame] | 1082 | inner += &name.to_string(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1083 | inner += "::"; |
| 1084 | } |
| 1085 | inner += &ident.to_string(); |
| 1086 | let instance = inner.replace("::", "$"); |
| 1087 | |
| 1088 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 1089 | writeln!(out, "void Box<{}>::uninit() noexcept {{", inner); |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1090 | writeln!(out, " cxxbridge03$box${}$uninit(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1091 | writeln!(out, "}}"); |
| 1092 | |
| 1093 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 1094 | writeln!(out, "void Box<{}>::drop() noexcept {{", inner); |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1095 | writeln!(out, " cxxbridge03$box${}$drop(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1096 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1097 | } |
| 1098 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1099 | fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) { |
| 1100 | let element = Type::Ident(element.clone()); |
| 1101 | let inner = to_typename(&out.namespace, &element); |
| 1102 | let instance = to_mangled(&out.namespace, &element); |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 1103 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1104 | writeln!(out, "template <>"); |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1105 | writeln!(out, "Vec<{}>::Vec() noexcept {{", inner); |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1106 | writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance); |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1107 | writeln!(out, "}}"); |
| 1108 | |
| 1109 | writeln!(out, "template <>"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1110 | writeln!(out, "void Vec<{}>::drop() noexcept {{", inner); |
| 1111 | writeln!( |
| 1112 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1113 | " return cxxbridge03$rust_vec${}$drop(this);", |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 1114 | instance, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1115 | ); |
| 1116 | writeln!(out, "}}"); |
| 1117 | |
| 1118 | writeln!(out, "template <>"); |
| 1119 | writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner); |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1120 | writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1121 | writeln!(out, "}}"); |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1122 | |
| 1123 | writeln!(out, "template <>"); |
| 1124 | writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner); |
| 1125 | writeln!( |
| 1126 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1127 | " return cxxbridge03$rust_vec${}$data(this);", |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1128 | instance, |
| 1129 | ); |
| 1130 | writeln!(out, "}}"); |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1131 | |
| 1132 | writeln!(out, "template <>"); |
| 1133 | writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner); |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1134 | writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance); |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1135 | writeln!(out, "}}"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1138 | fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) { |
| 1139 | let ty = Type::Ident(ident.clone()); |
| 1140 | let instance = to_mangled(&out.namespace, &ty); |
| 1141 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1142 | writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance); |
| 1143 | writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1144 | |
| 1145 | write_unique_ptr_common(out, &ty, types); |
| 1146 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1147 | writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | // Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>. |
| 1151 | fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1152 | out.include.utility = true; |
| David Tolnay | 4c4b550 | 2020-04-24 18:41:36 -0700 | [diff] [blame] | 1153 | let inner = to_typename(&out.namespace, ty); |
| David Tolnay | f12e983 | 2020-04-24 18:46:44 -0700 | [diff] [blame] | 1154 | let instance = to_mangled(&out.namespace, ty); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1155 | |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1156 | let can_construct_from_value = match ty { |
| 1157 | Type::Ident(ident) => types.structs.contains_key(ident), |
| 1158 | _ => false, |
| 1159 | }; |
| 1160 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1161 | writeln!( |
| 1162 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1163 | "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1164 | inner, |
| 1165 | ); |
| 1166 | writeln!( |
| 1167 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1168 | "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1169 | inner, |
| 1170 | ); |
| 1171 | writeln!( |
| 1172 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1173 | "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1174 | instance, inner, |
| 1175 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1176 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1177 | writeln!(out, "}}"); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1178 | if can_construct_from_value { |
| 1179 | writeln!( |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1180 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1181 | "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{", |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1182 | instance, inner, inner, |
| 1183 | ); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1184 | writeln!( |
| 1185 | out, |
| 1186 | " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));", |
| 1187 | inner, inner, |
| 1188 | ); |
| 1189 | writeln!(out, "}}"); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1190 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1191 | writeln!( |
| 1192 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1193 | "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1194 | instance, inner, inner, |
| 1195 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1196 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1197 | writeln!(out, "}}"); |
| 1198 | writeln!( |
| 1199 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1200 | "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1201 | inner, instance, inner, |
| 1202 | ); |
| 1203 | writeln!(out, " return ptr.get();"); |
| 1204 | writeln!(out, "}}"); |
| 1205 | writeln!( |
| 1206 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1207 | "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1208 | inner, instance, inner, |
| 1209 | ); |
| 1210 | writeln!(out, " return ptr.release();"); |
| 1211 | writeln!(out, "}}"); |
| 1212 | writeln!( |
| 1213 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1214 | "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1215 | instance, inner, |
| 1216 | ); |
| 1217 | writeln!(out, " ptr->~unique_ptr();"); |
| 1218 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1219 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1220 | |
| David Tolnay | 92105da | 2020-04-25 11:15:31 -0700 | [diff] [blame] | 1221 | 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] | 1222 | let element = Type::Ident(element.clone()); |
| 1223 | let inner = to_typename(&out.namespace, &element); |
| 1224 | let instance = to_mangled(&out.namespace, &element); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1225 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1226 | writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance); |
| 1227 | writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1228 | writeln!( |
| 1229 | out, |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1230 | "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1231 | instance, inner, |
| 1232 | ); |
| 1233 | writeln!(out, " return s.size();"); |
| 1234 | writeln!(out, "}}"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1235 | writeln!( |
| 1236 | out, |
| David Tolnay | b3fcf7b | 2020-04-30 22:58:28 -0700 | [diff] [blame] | 1237 | "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] | 1238 | inner, instance, inner, |
| 1239 | ); |
| David Tolnay | b3fcf7b | 2020-04-30 22:58:28 -0700 | [diff] [blame] | 1240 | writeln!(out, " return &s[pos];"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1241 | writeln!(out, "}}"); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1242 | |
| 1243 | write_unique_ptr_common(out, vector_ty, types); |
| 1244 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 1245 | writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1246 | } |