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