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