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