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