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