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