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