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