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