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