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