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