| David Tolnay | b725d5a | 2020-12-20 20:27:10 -0800 | [diff] [blame] | 1 | use crate::gen::block::Block; |
| David Tolnay | f7b81fb | 2020-11-01 22:39:04 -0800 | [diff] [blame] | 2 | use crate::gen::nested::NamespaceEntries; |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 3 | use crate::gen::out::OutFile; |
| David Tolnay | 942cffd | 2020-11-03 18:27:10 -0800 | [diff] [blame] | 4 | use crate::gen::{builtin, include, Opt}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 5 | use crate::syntax::atom::Atom::{self, *}; |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 6 | use crate::syntax::instantiate::ImplKey; |
| David Tolnay | e352c1e | 2020-12-31 16:41:05 -0800 | [diff] [blame] | 7 | use crate::syntax::map::UnorderedMap as Map; |
| David Tolnay | 14dc792 | 2021-01-02 01:07:16 -0800 | [diff] [blame] | 8 | use crate::syntax::set::{OrderedSet, UnorderedSet}; |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 9 | use crate::syntax::symbol::Symbol; |
| David Tolnay | 5b1d863 | 2020-12-20 22:05:11 -0800 | [diff] [blame] | 10 | use crate::syntax::trivial::{self, TrivialReason}; |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 11 | use crate::syntax::{ |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 12 | derive, mangle, Api, Enum, ExternFn, ExternType, Pair, Signature, Struct, Trait, Type, |
| 13 | TypeAlias, Types, Var, |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 14 | }; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 15 | use proc_macro2::Ident; |
| 16 | |
| David Tolnay | ac7188c | 2020-11-01 16:58:16 -0800 | [diff] [blame] | 17 | pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> { |
| David Tolnay | e1476af | 2020-11-01 13:47:25 -0800 | [diff] [blame] | 18 | let mut out_file = OutFile::new(header, opt, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 19 | let out = &mut out_file; |
| 20 | |
| David Tolnay | dfb82d7 | 2020-11-02 00:10:10 -0800 | [diff] [blame] | 21 | pick_includes_and_builtins(out, apis); |
| David Tolnay | 4aae7c0 | 2020-10-28 12:35:42 -0700 | [diff] [blame] | 22 | out.include.extend(&opt.include); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 23 | |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 24 | write_forward_declarations(out, apis); |
| David Tolnay | e1109d9 | 2020-11-01 20:51:56 -0800 | [diff] [blame] | 25 | write_data_structures(out, apis); |
| 26 | write_functions(out, apis); |
| David Tolnay | 169bb47 | 2020-11-01 21:04:24 -0800 | [diff] [blame] | 27 | write_generic_instantiations(out); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 28 | |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 29 | builtin::write(out); |
| David Tolnay | 2f3e90b | 2020-10-31 22:16:51 -0700 | [diff] [blame] | 30 | include::write(out); |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 31 | |
| David Tolnay | ac7188c | 2020-11-01 16:58:16 -0800 | [diff] [blame] | 32 | out_file.content() |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 35 | fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) { |
| 36 | let needs_forward_declaration = |api: &&Api| match api { |
| David Tolnay | 4bfca11 | 2020-11-01 23:59:11 -0800 | [diff] [blame] | 37 | Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true, |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 38 | Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust), |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 39 | _ => false, |
| 40 | }; |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 41 | |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 42 | let apis_by_namespace = |
| 43 | NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect()); |
| 44 | |
| David Tolnay | d920be5 | 2020-11-01 23:34:30 -0800 | [diff] [blame] | 45 | write(out, &apis_by_namespace, 0); |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 46 | |
| David Tolnay | d920be5 | 2020-11-01 23:34:30 -0800 | [diff] [blame] | 47 | fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) { |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 48 | let apis = ns_entries.direct_content(); |
| 49 | |
| 50 | for api in apis { |
| David Tolnay | d920be5 | 2020-11-01 23:34:30 -0800 | [diff] [blame] | 51 | write!(out, "{:1$}", "", indent); |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 52 | match api { |
| David Tolnay | ed6ba4a | 2021-01-01 14:59:40 -0800 | [diff] [blame] | 53 | Api::Struct(strct) => write_struct_decl(out, &strct.name), |
| David Tolnay | 0e3ee8e | 2020-11-01 23:46:52 -0800 | [diff] [blame] | 54 | Api::Enum(enm) => write_enum_decl(out, enm), |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 55 | Api::CxxType(ety) => write_struct_using(out, &ety.name), |
| David Tolnay | ed6ba4a | 2021-01-01 14:59:40 -0800 | [diff] [blame] | 56 | Api::RustType(ety) => write_struct_decl(out, &ety.name), |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 57 | _ => unreachable!(), |
| 58 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 59 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 60 | |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 61 | for (namespace, nested_ns_entries) in ns_entries.nested_content() { |
| David Tolnay | d920be5 | 2020-11-01 23:34:30 -0800 | [diff] [blame] | 62 | writeln!(out, "{:2$}namespace {} {{", "", namespace, indent); |
| 63 | write(out, nested_ns_entries, indent + 2); |
| 64 | writeln!(out, "{:1$}}}", "", indent); |
| David Tolnay | 7b0e510 | 2020-11-01 23:22:12 -0800 | [diff] [blame] | 65 | } |
| Adrian Taylor | f921362 | 2020-10-31 22:25:42 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| David Tolnay | e1109d9 | 2020-11-01 20:51:56 -0800 | [diff] [blame] | 69 | fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) { |
| David Tolnay | e352c1e | 2020-12-31 16:41:05 -0800 | [diff] [blame] | 70 | let mut methods_for_type = Map::new(); |
| David Tolnay | 630af88 | 2020-10-31 22:03:47 -0700 | [diff] [blame] | 71 | for api in apis { |
| David Tolnay | 102c7ea | 2020-11-08 18:58:09 -0800 | [diff] [blame] | 72 | if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api { |
| David Tolnay | f94bef1 | 2020-04-17 14:46:42 -0700 | [diff] [blame] | 73 | if let Some(receiver) = &efn.sig.receiver { |
| 74 | methods_for_type |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 75 | .entry(&receiver.ty.rust) |
| David Tolnay | f94bef1 | 2020-04-17 14:46:42 -0700 | [diff] [blame] | 76 | .or_insert_with(Vec::new) |
| 77 | .push(efn); |
| 78 | } |
| 79 | } |
| 80 | } |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 81 | |
| David Tolnay | e352c1e | 2020-12-31 16:41:05 -0800 | [diff] [blame] | 82 | let mut structs_written = UnorderedSet::new(); |
| David Tolnay | 5439fa1 | 2020-11-03 18:45:01 -0800 | [diff] [blame] | 83 | let mut toposorted_structs = out.types.toposorted_structs.iter(); |
| David Tolnay | 9622b46 | 2020-11-02 21:34:42 -0800 | [diff] [blame] | 84 | for api in apis { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 85 | match api { |
| David Tolnay | 5439fa1 | 2020-11-03 18:45:01 -0800 | [diff] [blame] | 86 | Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => { |
| 87 | for next in &mut toposorted_structs { |
| 88 | if !out.types.cxx.contains(&strct.name.rust) { |
| 89 | out.next_section(); |
| David Tolnay | 102c7ea | 2020-11-08 18:58:09 -0800 | [diff] [blame] | 90 | let methods = methods_for_type |
| 91 | .get(&strct.name.rust) |
| 92 | .map(Vec::as_slice) |
| 93 | .unwrap_or_default(); |
| 94 | write_struct(out, next, methods); |
| David Tolnay | 5439fa1 | 2020-11-03 18:45:01 -0800 | [diff] [blame] | 95 | } |
| 96 | structs_written.insert(&next.name.rust); |
| 97 | if next.name.rust == strct.name.rust { |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 102 | Api::Enum(enm) => { |
| 103 | out.next_section(); |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 104 | if out.types.cxx.contains(&enm.name.rust) { |
| Joel Galenson | 905eb2e | 2020-05-04 14:58:14 -0700 | [diff] [blame] | 105 | check_enum(out, enm); |
| 106 | } else { |
| 107 | write_enum(out, enm); |
| 108 | } |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 109 | } |
| David Tolnay | c1fe005 | 2020-04-17 15:15:06 -0700 | [diff] [blame] | 110 | Api::RustType(ety) => { |
| David Tolnay | 8d067de | 2020-12-26 16:18:23 -0800 | [diff] [blame] | 111 | out.next_section(); |
| 112 | let methods = methods_for_type |
| 113 | .get(&ety.name.rust) |
| 114 | .map(Vec::as_slice) |
| 115 | .unwrap_or_default(); |
| 116 | write_opaque_type(out, ety, methods); |
| David Tolnay | c1fe005 | 2020-04-17 15:15:06 -0700 | [diff] [blame] | 117 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 118 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| David Tolnay | 8b22953 | 2021-01-02 18:30:45 -0800 | [diff] [blame^] | 122 | if out.header { |
| 123 | return; |
| 124 | } |
| 125 | |
| David Tolnay | 440e24a | 2021-01-01 23:25:34 -0800 | [diff] [blame] | 126 | out.set_namespace(Default::default()); |
| 127 | |
| David Tolnay | 8b22953 | 2021-01-02 18:30:45 -0800 | [diff] [blame^] | 128 | // MSVC workaround for "C linkage function cannot return C++ class" error. |
| 129 | // Apparently the compiler fails to perform implicit instantiations as part |
| 130 | // of an extern declaration return type. Instead we instantiate explicitly. |
| 131 | // See https://stackoverflow.com/a/57429504/6086311. |
| 132 | out.next_section(); |
| 133 | let mut slice_in_return_position = OrderedSet::new(); |
| 134 | for api in apis { |
| 135 | if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api { |
| 136 | if let Some(ty @ Type::SliceRef(_)) = &efn.ret { |
| 137 | slice_in_return_position.insert(ty); |
| David Tolnay | 14dc792 | 2021-01-02 01:07:16 -0800 | [diff] [blame] | 138 | } |
| David Tolnay | 440e24a | 2021-01-01 23:25:34 -0800 | [diff] [blame] | 139 | } |
| David Tolnay | 8b22953 | 2021-01-02 18:30:45 -0800 | [diff] [blame^] | 140 | } |
| 141 | for ty in &slice_in_return_position { |
| 142 | write!(out, "template class "); |
| 143 | write_type(out, ty); |
| 144 | writeln!(out, ";"); |
| 145 | } |
| David Tolnay | 440e24a | 2021-01-01 23:25:34 -0800 | [diff] [blame] | 146 | |
| David Tolnay | 8b22953 | 2021-01-02 18:30:45 -0800 | [diff] [blame^] | 147 | out.next_section(); |
| 148 | for api in apis { |
| 149 | if let Api::TypeAlias(ety) = api { |
| 150 | if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) { |
| 151 | check_trivial_extern_type(out, ety, reasons) |
| David Tolnay | fabca77 | 2020-10-03 21:25:41 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| David Tolnay | 1b0339c | 2020-11-01 20:37:50 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| David Tolnay | e1109d9 | 2020-11-01 20:51:56 -0800 | [diff] [blame] | 157 | fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) { |
| David Tolnay | ce5a91f | 2020-10-31 22:42:08 -0700 | [diff] [blame] | 158 | if !out.header { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 159 | for api in apis { |
| David Tolnay | 0477074 | 2020-11-01 13:50:50 -0800 | [diff] [blame] | 160 | match api { |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 161 | Api::Struct(strct) => write_struct_operator_decls(out, strct), |
| David Tolnay | 358bc4b | 2020-12-26 22:37:57 -0800 | [diff] [blame] | 162 | Api::RustType(ety) => write_opaque_type_layout_decls(out, ety), |
| David Tolnay | 0477074 | 2020-11-01 13:50:50 -0800 | [diff] [blame] | 163 | Api::CxxFunction(efn) => write_cxx_function_shim(out, efn), |
| 164 | Api::RustFunction(efn) => write_rust_function_decl(out, efn), |
| 165 | _ => {} |
| 166 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 167 | } |
| David Tolnay | 7da3820 | 2020-11-27 17:36:16 -0800 | [diff] [blame] | 168 | |
| 169 | write_std_specializations(out, apis); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | for api in apis { |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 173 | match api { |
| 174 | Api::Struct(strct) => write_struct_operators(out, strct), |
| David Tolnay | 358bc4b | 2020-12-26 22:37:57 -0800 | [diff] [blame] | 175 | Api::RustType(ety) => write_opaque_type_layout(out, ety), |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 176 | Api::RustFunction(efn) => { |
| 177 | out.next_section(); |
| 178 | write_rust_function_shim(out, efn); |
| 179 | } |
| 180 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 181 | } |
| 182 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| David Tolnay | 7da3820 | 2020-11-27 17:36:16 -0800 | [diff] [blame] | 185 | fn write_std_specializations(out: &mut OutFile, apis: &[Api]) { |
| 186 | out.set_namespace(Default::default()); |
| 187 | out.begin_block(Block::Namespace("std")); |
| 188 | |
| 189 | for api in apis { |
| 190 | if let Api::Struct(strct) = api { |
| 191 | if derive::contains(&strct.derives, Trait::Hash) { |
| 192 | out.next_section(); |
| David Tolnay | 12320e1 | 2020-12-09 23:09:36 -0800 | [diff] [blame] | 193 | out.include.cstddef = true; |
| David Tolnay | 08a03db | 2020-12-09 23:04:36 -0800 | [diff] [blame] | 194 | out.include.functional = true; |
| David Tolnay | 7da3820 | 2020-11-27 17:36:16 -0800 | [diff] [blame] | 195 | let qualified = strct.name.to_fully_qualified(); |
| 196 | writeln!(out, "template <> struct hash<{}> {{", qualified); |
| 197 | writeln!( |
| 198 | out, |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 199 | " ::std::size_t operator()(const {} &self) const noexcept {{", |
| David Tolnay | 7da3820 | 2020-11-27 17:36:16 -0800 | [diff] [blame] | 200 | qualified, |
| 201 | ); |
| 202 | let link_name = mangle::operator(&strct.name, "hash"); |
| 203 | write!(out, " return ::"); |
| 204 | for name in &strct.name.namespace { |
| 205 | write!(out, "{}::", name); |
| 206 | } |
| 207 | writeln!(out, "{}(self);", link_name); |
| 208 | writeln!(out, " }}"); |
| 209 | writeln!(out, "}};"); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | out.end_block(Block::Namespace("std")); |
| 215 | } |
| 216 | |
| David Tolnay | dfb82d7 | 2020-11-02 00:10:10 -0800 | [diff] [blame] | 217 | fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) { |
| 218 | for api in apis { |
| 219 | if let Api::Include(include) = api { |
| 220 | out.include.insert(include); |
| 221 | } |
| 222 | } |
| 223 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 224 | for ty in out.types { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 225 | match ty { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 226 | Type::Ident(ident) => match Atom::from(&ident.rust) { |
| David Tolnay | 89e386d | 2020-10-03 19:02:19 -0700 | [diff] [blame] | 227 | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32) |
| 228 | | Some(I64) => out.include.cstdint = true, |
| 229 | Some(Usize) => out.include.cstddef = true, |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 230 | Some(Isize) => out.builtin.rust_isize = true, |
| David Tolnay | 89e386d | 2020-10-03 19:02:19 -0700 | [diff] [blame] | 231 | Some(CxxString) => out.include.string = true, |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 232 | Some(RustString) => out.builtin.rust_string = true, |
| David Tolnay | b3873dc | 2020-11-25 19:47:49 -0800 | [diff] [blame] | 233 | Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {} |
| David Tolnay | 89e386d | 2020-10-03 19:02:19 -0700 | [diff] [blame] | 234 | }, |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 235 | Type::RustBox(_) => out.builtin.rust_box = true, |
| 236 | Type::RustVec(_) => out.builtin.rust_vec = true, |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 237 | Type::UniquePtr(_) => out.include.memory = true, |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 238 | Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true, |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 239 | Type::Str(_) => out.builtin.rust_str = true, |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 240 | Type::CxxVector(_) => out.include.vector = true, |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 241 | Type::Fn(_) => out.builtin.rust_fn = true, |
| David Tolnay | 5515a9e | 2020-11-25 19:07:54 -0800 | [diff] [blame] | 242 | Type::SliceRef(_) => out.builtin.rust_slice = true, |
| David Tolnay | e8b1bb4 | 2020-11-24 20:37:37 -0800 | [diff] [blame] | 243 | Type::Array(_) => out.include.array = true, |
| David Tolnay | 11bd7ff | 2020-11-01 19:44:58 -0800 | [diff] [blame] | 244 | Type::Ref(_) | Type::Void(_) => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 245 | } |
| 246 | } |
| David Tolnay | ec66d11 | 2020-10-31 21:00:26 -0700 | [diff] [blame] | 247 | } |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 248 | |
| David Tolnay | 102c7ea | 2020-11-08 18:58:09 -0800 | [diff] [blame] | 249 | fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) { |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 250 | let operator_eq = derive::contains(&strct.derives, Trait::PartialEq); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 251 | let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd); |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 252 | |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 253 | out.set_namespace(&strct.name.namespace); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 254 | let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol()); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 255 | writeln!(out, "#ifndef {}", guard); |
| 256 | writeln!(out, "#define {}", guard); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 257 | for line in strct.doc.to_string().lines() { |
| 258 | writeln!(out, "//{}", line); |
| 259 | } |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 260 | writeln!(out, "struct {} final {{", strct.name.cxx); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 261 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 262 | for field in &strct.fields { |
| David Tolnay | 5573e52 | 2020-12-31 11:07:38 -0800 | [diff] [blame] | 263 | for line in field.doc.to_string().lines() { |
| 264 | writeln!(out, " //{}", line); |
| 265 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 266 | write!(out, " "); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 267 | write_type_space(out, &field.ty); |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 268 | writeln!(out, "{};", field.name.cxx); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 269 | } |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 270 | |
| David Tolnay | 5554ebd | 2020-12-10 11:34:41 -0800 | [diff] [blame] | 271 | writeln!(out); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 272 | |
| David Tolnay | 102c7ea | 2020-11-08 18:58:09 -0800 | [diff] [blame] | 273 | for method in methods { |
| 274 | write!(out, " "); |
| 275 | let sig = &method.sig; |
| 276 | let local_name = method.name.cxx.to_string(); |
| 277 | write_rust_function_shim_decl(out, &local_name, sig, false); |
| 278 | writeln!(out, ";"); |
| 279 | } |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 280 | |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 281 | if operator_eq { |
| 282 | writeln!( |
| 283 | out, |
| 284 | " bool operator==(const {} &) const noexcept;", |
| 285 | strct.name.cxx, |
| 286 | ); |
| 287 | writeln!( |
| 288 | out, |
| 289 | " bool operator!=(const {} &) const noexcept;", |
| 290 | strct.name.cxx, |
| 291 | ); |
| 292 | } |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 293 | |
| 294 | if operator_ord { |
| 295 | writeln!( |
| 296 | out, |
| 297 | " bool operator<(const {} &) const noexcept;", |
| 298 | strct.name.cxx, |
| 299 | ); |
| 300 | writeln!( |
| 301 | out, |
| 302 | " bool operator<=(const {} &) const noexcept;", |
| 303 | strct.name.cxx, |
| 304 | ); |
| 305 | writeln!( |
| 306 | out, |
| 307 | " bool operator>(const {} &) const noexcept;", |
| 308 | strct.name.cxx, |
| 309 | ); |
| 310 | writeln!( |
| 311 | out, |
| 312 | " bool operator>=(const {} &) const noexcept;", |
| 313 | strct.name.cxx, |
| 314 | ); |
| 315 | } |
| 316 | |
| David Tolnay | 5554ebd | 2020-12-10 11:34:41 -0800 | [diff] [blame] | 317 | out.include.type_traits = true; |
| 318 | writeln!(out, " using IsRelocatable = ::std::true_type;"); |
| 319 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 320 | writeln!(out, "}};"); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 321 | writeln!(out, "#endif // {}", guard); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 322 | } |
| 323 | |
| David Tolnay | ed6ba4a | 2021-01-01 14:59:40 -0800 | [diff] [blame] | 324 | fn write_struct_decl(out: &mut OutFile, ident: &Pair) { |
| 325 | writeln!(out, "struct {};", ident.cxx); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 326 | } |
| 327 | |
| David Tolnay | 0e3ee8e | 2020-11-01 23:46:52 -0800 | [diff] [blame] | 328 | fn write_enum_decl(out: &mut OutFile, enm: &Enum) { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 329 | write!(out, "enum class {} : ", enm.name.cxx); |
| David Tolnay | 0e3ee8e | 2020-11-01 23:46:52 -0800 | [diff] [blame] | 330 | write_atom(out, enm.repr); |
| 331 | writeln!(out, ";"); |
| 332 | } |
| 333 | |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 334 | fn write_struct_using(out: &mut OutFile, ident: &Pair) { |
| 335 | writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified()); |
| David Tolnay | 8861bee | 2020-01-20 18:39:24 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| David Tolnay | 8d067de | 2020-12-26 16:18:23 -0800 | [diff] [blame] | 338 | fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 339 | out.set_namespace(&ety.name.namespace); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 340 | let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol()); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 341 | writeln!(out, "#ifndef {}", guard); |
| 342 | writeln!(out, "#define {}", guard); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 343 | for line in ety.doc.to_string().lines() { |
| 344 | writeln!(out, "//{}", line); |
| 345 | } |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 346 | |
| David Tolnay | 365fc7c | 2020-11-25 16:08:13 -0800 | [diff] [blame] | 347 | out.builtin.opaque = true; |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 348 | writeln!( |
| David Tolnay | 7c06b86 | 2020-11-25 16:59:09 -0800 | [diff] [blame] | 349 | out, |
| 350 | "struct {} final : public ::rust::Opaque {{", |
| 351 | ety.name.cxx, |
| 352 | ); |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 353 | |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 354 | for method in methods { |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 355 | write!(out, " "); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 356 | let sig = &method.sig; |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 357 | let local_name = method.name.cxx.to_string(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 358 | write_rust_function_shim_decl(out, &local_name, sig, false); |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 359 | writeln!(out, ";"); |
| David Tolnay | 8d067de | 2020-12-26 16:18:23 -0800 | [diff] [blame] | 360 | } |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 361 | |
| David Tolnay | 8d067de | 2020-12-26 16:18:23 -0800 | [diff] [blame] | 362 | if !methods.is_empty() { |
| 363 | writeln!(out); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 364 | } |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 365 | |
| David Tolnay | ee6ecfc | 2020-12-26 21:54:37 -0800 | [diff] [blame] | 366 | out.builtin.layout = true; |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 367 | out.include.cstddef = true; |
| 368 | writeln!(out, "private:"); |
| David Tolnay | ee6ecfc | 2020-12-26 21:54:37 -0800 | [diff] [blame] | 369 | writeln!(out, " friend ::rust::layout;"); |
| David Tolnay | 6ba262f | 2020-12-26 22:23:50 -0800 | [diff] [blame] | 370 | writeln!(out, " struct layout {{"); |
| 371 | writeln!(out, " static ::std::size_t size() noexcept;"); |
| 372 | writeln!(out, " static ::std::size_t align() noexcept;"); |
| 373 | writeln!(out, " }};"); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 374 | writeln!(out, "}};"); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 375 | writeln!(out, "#endif // {}", guard); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| David Tolnay | 0b9b9f8 | 2020-11-01 20:41:00 -0800 | [diff] [blame] | 378 | fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 379 | out.set_namespace(&enm.name.namespace); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 380 | let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol()); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 381 | writeln!(out, "#ifndef {}", guard); |
| 382 | writeln!(out, "#define {}", guard); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 383 | for line in enm.doc.to_string().lines() { |
| 384 | writeln!(out, "//{}", line); |
| 385 | } |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 386 | write!(out, "enum class {} : ", enm.name.cxx); |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 387 | write_atom(out, enm.repr); |
| 388 | writeln!(out, " {{"); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 389 | for variant in &enm.variants { |
| David Tolnay | 4486f72 | 2020-12-30 00:01:26 -0800 | [diff] [blame] | 390 | for line in variant.doc.to_string().lines() { |
| 391 | writeln!(out, " //{}", line); |
| 392 | } |
| David Tolnay | e6f6214 | 2020-12-21 16:00:41 -0800 | [diff] [blame] | 393 | writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 394 | } |
| 395 | writeln!(out, "}};"); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 396 | writeln!(out, "#endif // {}", guard); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| David Tolnay | 0b9b9f8 | 2020-11-01 20:41:00 -0800 | [diff] [blame] | 399 | fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 400 | out.set_namespace(&enm.name.namespace); |
| David Tolnay | 06711bc | 2020-11-19 19:25:14 -0800 | [diff] [blame] | 401 | out.include.type_traits = true; |
| 402 | writeln!( |
| 403 | out, |
| 404 | "static_assert(::std::is_enum<{}>::value, \"expected enum\");", |
| 405 | enm.name.cxx, |
| 406 | ); |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 407 | write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx); |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 408 | write_atom(out, enm.repr); |
| 409 | writeln!(out, "), \"incorrect size\");"); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 410 | for variant in &enm.variants { |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 411 | write!(out, "static_assert(static_cast<"); |
| 412 | write_atom(out, enm.repr); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 413 | writeln!( |
| 414 | out, |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 415 | ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");", |
| David Tolnay | e6f6214 | 2020-12-21 16:00:41 -0800 | [diff] [blame] | 416 | enm.name.cxx, variant.name.cxx, variant.discriminant, |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 417 | ); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 418 | } |
| Joel Galenson | 905eb2e | 2020-05-04 14:58:14 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| David Tolnay | 5b1d863 | 2020-12-20 22:05:11 -0800 | [diff] [blame] | 421 | fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) { |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 422 | // NOTE: The following static assertion is just nice-to-have and not |
| David Tolnay | fd0034e | 2020-10-04 13:15:34 -0700 | [diff] [blame] | 423 | // necessary for soundness. That's because triviality is always declared by |
| 424 | // the user in the form of an unsafe impl of cxx::ExternType: |
| 425 | // |
| 426 | // unsafe impl ExternType for MyType { |
| 427 | // type Id = cxx::type_id!("..."); |
| 428 | // type Kind = cxx::kind::Trivial; |
| 429 | // } |
| 430 | // |
| 431 | // Since the user went on the record with their unsafe impl to unsafely |
| 432 | // claim they KNOW that the type is trivial, it's fine for that to be on |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 433 | // them if that were wrong. However, in practice correctly reasoning about |
| 434 | // the relocatability of C++ types is challenging, particularly if the type |
| 435 | // definition were to change over time, so for now we add this check. |
| David Tolnay | fd0034e | 2020-10-04 13:15:34 -0700 | [diff] [blame] | 436 | // |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 437 | // There may be legitimate reasons to opt out of this assertion for support |
| 438 | // of types that the programmer knows are soundly Rust-movable despite not |
| 439 | // being recognized as such by the C++ type system due to a move constructor |
| 440 | // or destructor. To opt out of the relocatability check, they need to do |
| 441 | // one of the following things in any header used by `include!` in their |
| 442 | // bridge. |
| 443 | // |
| 444 | // --- if they define the type: |
| 445 | // struct MyType { |
| 446 | // ... |
| 447 | // + using IsRelocatable = std::true_type; |
| 448 | // }; |
| 449 | // |
| 450 | // --- otherwise: |
| 451 | // + template <> |
| 452 | // + struct rust::IsRelocatable<MyType> : std::true_type {}; |
| 453 | // |
| David Tolnay | fd0034e | 2020-10-04 13:15:34 -0700 | [diff] [blame] | 454 | |
| David Tolnay | 5b1d863 | 2020-12-20 22:05:11 -0800 | [diff] [blame] | 455 | let id = alias.name.to_fully_qualified(); |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 456 | out.builtin.relocatable = true; |
| David Tolnay | 5b1d863 | 2020-12-20 22:05:11 -0800 | [diff] [blame] | 457 | writeln!(out, "static_assert("); |
| 458 | writeln!(out, " ::rust::IsRelocatable<{}>::value,", id); |
| 459 | writeln!( |
| 460 | out, |
| 461 | " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");", |
| 462 | id.trim_start_matches("::"), |
| 463 | trivial::as_what(&alias.name, reasons), |
| 464 | ); |
| Adrian Taylor | 405e874 | 2020-09-25 14:01:25 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 467 | fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) { |
| 468 | out.set_namespace(&strct.name.namespace); |
| 469 | out.begin_block(Block::ExternC); |
| 470 | |
| 471 | if derive::contains(&strct.derives, Trait::PartialEq) { |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 472 | let link_name = mangle::operator(&strct.name, "eq"); |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 473 | writeln!( |
| 474 | out, |
| 475 | "bool {}(const {1} &, const {1} &) noexcept;", |
| 476 | link_name, strct.name.cxx, |
| 477 | ); |
| David Tolnay | a6f3b6f | 2020-11-27 16:47:30 -0800 | [diff] [blame] | 478 | |
| 479 | if !derive::contains(&strct.derives, Trait::Eq) { |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 480 | let link_name = mangle::operator(&strct.name, "ne"); |
| David Tolnay | a6f3b6f | 2020-11-27 16:47:30 -0800 | [diff] [blame] | 481 | writeln!( |
| 482 | out, |
| 483 | "bool {}(const {1} &, const {1} &) noexcept;", |
| 484 | link_name, strct.name.cxx, |
| 485 | ); |
| 486 | } |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 489 | if derive::contains(&strct.derives, Trait::PartialOrd) { |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 490 | let link_name = mangle::operator(&strct.name, "lt"); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 491 | writeln!( |
| 492 | out, |
| 493 | "bool {}(const {1} &, const {1} &) noexcept;", |
| 494 | link_name, strct.name.cxx, |
| 495 | ); |
| 496 | |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 497 | let link_name = mangle::operator(&strct.name, "le"); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 498 | writeln!( |
| 499 | out, |
| 500 | "bool {}(const {1} &, const {1} &) noexcept;", |
| 501 | link_name, strct.name.cxx, |
| 502 | ); |
| 503 | |
| 504 | if !derive::contains(&strct.derives, Trait::Ord) { |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 505 | let link_name = mangle::operator(&strct.name, "gt"); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 506 | writeln!( |
| 507 | out, |
| 508 | "bool {}(const {1} &, const {1} &) noexcept;", |
| 509 | link_name, strct.name.cxx, |
| 510 | ); |
| 511 | |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 512 | let link_name = mangle::operator(&strct.name, "ge"); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 513 | writeln!( |
| 514 | out, |
| 515 | "bool {}(const {1} &, const {1} &) noexcept;", |
| 516 | link_name, strct.name.cxx, |
| 517 | ); |
| 518 | } |
| 519 | } |
| 520 | |
| David Tolnay | 7da3820 | 2020-11-27 17:36:16 -0800 | [diff] [blame] | 521 | if derive::contains(&strct.derives, Trait::Hash) { |
| David Tolnay | 12320e1 | 2020-12-09 23:09:36 -0800 | [diff] [blame] | 522 | out.include.cstddef = true; |
| David Tolnay | 7da3820 | 2020-11-27 17:36:16 -0800 | [diff] [blame] | 523 | let link_name = mangle::operator(&strct.name, "hash"); |
| 524 | writeln!( |
| 525 | out, |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 526 | "::std::size_t {}(const {} &) noexcept;", |
| David Tolnay | 7da3820 | 2020-11-27 17:36:16 -0800 | [diff] [blame] | 527 | link_name, strct.name.cxx, |
| 528 | ); |
| 529 | } |
| 530 | |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 531 | out.end_block(Block::ExternC); |
| 532 | } |
| 533 | |
| 534 | fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) { |
| 535 | if out.header { |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | out.set_namespace(&strct.name.namespace); |
| 540 | |
| 541 | if derive::contains(&strct.derives, Trait::PartialEq) { |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 542 | out.next_section(); |
| 543 | writeln!( |
| 544 | out, |
| 545 | "bool {0}::operator==(const {0} &rhs) const noexcept {{", |
| 546 | strct.name.cxx, |
| 547 | ); |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 548 | let link_name = mangle::operator(&strct.name, "eq"); |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 549 | writeln!(out, " return {}(*this, rhs);", link_name); |
| 550 | writeln!(out, "}}"); |
| David Tolnay | a6f3b6f | 2020-11-27 16:47:30 -0800 | [diff] [blame] | 551 | |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 552 | out.next_section(); |
| 553 | writeln!( |
| 554 | out, |
| 555 | "bool {0}::operator!=(const {0} &rhs) const noexcept {{", |
| 556 | strct.name.cxx, |
| 557 | ); |
| David Tolnay | a6f3b6f | 2020-11-27 16:47:30 -0800 | [diff] [blame] | 558 | if derive::contains(&strct.derives, Trait::Eq) { |
| 559 | writeln!(out, " return !(*this == rhs);"); |
| 560 | } else { |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 561 | let link_name = mangle::operator(&strct.name, "ne"); |
| David Tolnay | a6f3b6f | 2020-11-27 16:47:30 -0800 | [diff] [blame] | 562 | writeln!(out, " return {}(*this, rhs);", link_name); |
| 563 | } |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 564 | writeln!(out, "}}"); |
| 565 | } |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 566 | |
| 567 | if derive::contains(&strct.derives, Trait::PartialOrd) { |
| 568 | out.next_section(); |
| 569 | writeln!( |
| 570 | out, |
| 571 | "bool {0}::operator<(const {0} &rhs) const noexcept {{", |
| 572 | strct.name.cxx, |
| 573 | ); |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 574 | let link_name = mangle::operator(&strct.name, "lt"); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 575 | writeln!(out, " return {}(*this, rhs);", link_name); |
| 576 | writeln!(out, "}}"); |
| 577 | |
| 578 | out.next_section(); |
| 579 | writeln!( |
| 580 | out, |
| 581 | "bool {0}::operator<=(const {0} &rhs) const noexcept {{", |
| 582 | strct.name.cxx, |
| 583 | ); |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 584 | let link_name = mangle::operator(&strct.name, "le"); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 585 | writeln!(out, " return {}(*this, rhs);", link_name); |
| 586 | writeln!(out, "}}"); |
| 587 | |
| 588 | out.next_section(); |
| 589 | writeln!( |
| 590 | out, |
| 591 | "bool {0}::operator>(const {0} &rhs) const noexcept {{", |
| 592 | strct.name.cxx, |
| 593 | ); |
| 594 | if derive::contains(&strct.derives, Trait::Ord) { |
| 595 | writeln!(out, " return !(*this <= rhs);"); |
| 596 | } else { |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 597 | let link_name = mangle::operator(&strct.name, "gt"); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 598 | writeln!(out, " return {}(*this, rhs);", link_name); |
| 599 | } |
| 600 | writeln!(out, "}}"); |
| 601 | |
| 602 | out.next_section(); |
| 603 | writeln!( |
| 604 | out, |
| 605 | "bool {0}::operator>=(const {0} &rhs) const noexcept {{", |
| 606 | strct.name.cxx, |
| 607 | ); |
| 608 | if derive::contains(&strct.derives, Trait::Ord) { |
| 609 | writeln!(out, " return !(*this < rhs);"); |
| 610 | } else { |
| David Tolnay | a05f940 | 2020-11-27 17:44:05 -0800 | [diff] [blame] | 611 | let link_name = mangle::operator(&strct.name, "ge"); |
| David Tolnay | 8438935 | 2020-11-27 17:12:10 -0800 | [diff] [blame] | 612 | writeln!(out, " return {}(*this, rhs);", link_name); |
| 613 | } |
| 614 | writeln!(out, "}}"); |
| 615 | } |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| David Tolnay | 358bc4b | 2020-12-26 22:37:57 -0800 | [diff] [blame] | 618 | fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) { |
| 619 | out.set_namespace(&ety.name.namespace); |
| 620 | out.begin_block(Block::ExternC); |
| 621 | |
| 622 | let link_name = mangle::operator(&ety.name, "sizeof"); |
| 623 | writeln!(out, "::std::size_t {}() noexcept;", link_name); |
| 624 | |
| 625 | let link_name = mangle::operator(&ety.name, "alignof"); |
| 626 | writeln!(out, "::std::size_t {}() noexcept;", link_name); |
| 627 | |
| 628 | out.end_block(Block::ExternC); |
| 629 | } |
| 630 | |
| 631 | fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) { |
| 632 | if out.header { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | out.set_namespace(&ety.name.namespace); |
| 637 | |
| 638 | out.next_section(); |
| 639 | let link_name = mangle::operator(&ety.name, "sizeof"); |
| 640 | writeln!( |
| 641 | out, |
| 642 | "::std::size_t {}::layout::size() noexcept {{", |
| 643 | ety.name.cxx, |
| 644 | ); |
| 645 | writeln!(out, " return {}();", link_name); |
| 646 | writeln!(out, "}}"); |
| 647 | |
| 648 | out.next_section(); |
| 649 | let link_name = mangle::operator(&ety.name, "alignof"); |
| 650 | writeln!( |
| 651 | out, |
| 652 | "::std::size_t {}::layout::align() noexcept {{", |
| 653 | ety.name.cxx, |
| 654 | ); |
| 655 | writeln!(out, " return {}();", link_name); |
| 656 | writeln!(out, "}}"); |
| 657 | } |
| 658 | |
| David Tolnay | 1eadd26 | 2020-12-29 16:42:08 -0800 | [diff] [blame] | 659 | fn begin_function_definition(out: &mut OutFile) { |
| 660 | if let Some(annotation) = &out.opt.cxx_impl_annotations { |
| 661 | write!(out, "{} ", annotation); |
| 662 | } |
| 663 | } |
| 664 | |
| David Tolnay | 0b9b9f8 | 2020-11-01 20:41:00 -0800 | [diff] [blame] | 665 | fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) { |
| David Tolnay | 0477074 | 2020-11-01 13:50:50 -0800 | [diff] [blame] | 666 | out.next_section(); |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 667 | out.set_namespace(&efn.name.namespace); |
| David Tolnay | ca563ee | 2020-11-01 20:12:27 -0800 | [diff] [blame] | 668 | out.begin_block(Block::ExternC); |
| David Tolnay | 1eadd26 | 2020-12-29 16:42:08 -0800 | [diff] [blame] | 669 | begin_function_definition(out); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 670 | if efn.throws { |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 671 | out.builtin.ptr_len = true; |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 672 | write!(out, "::rust::repr::PtrLen "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 673 | } else { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 674 | write_extern_return_type_space(out, &efn.ret); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 675 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 676 | let mangled = mangle::extern_fn(efn, out.types); |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 677 | write!(out, "{}(", mangled); |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 678 | if let Some(receiver) = &efn.receiver { |
| David Tolnay | 9c4ac2e | 2020-11-15 21:14:03 -0800 | [diff] [blame] | 679 | if !receiver.mutable { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 680 | write!(out, "const "); |
| 681 | } |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 682 | write!( |
| 683 | out, |
| 684 | "{} &self", |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 685 | out.types.resolve(&receiver.ty).name.to_fully_qualified(), |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 686 | ); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 687 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 688 | for (i, arg) in efn.args.iter().enumerate() { |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 689 | if i > 0 || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 690 | write!(out, ", "); |
| 691 | } |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 692 | if arg.ty == RustString { |
| 693 | write!(out, "const "); |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 694 | } else if let Type::RustVec(_) = arg.ty { |
| 695 | write!(out, "const "); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 696 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 697 | write_extern_arg(out, arg); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 698 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 699 | let indirect_return = indirect_return(efn, out.types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 700 | if indirect_return { |
| myronahn | e3b78ea | 2020-05-23 01:08:13 +0700 | [diff] [blame] | 701 | if !efn.args.is_empty() || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 702 | write!(out, ", "); |
| 703 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 704 | write_indirect_return_type_space(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 705 | write!(out, "*return$"); |
| 706 | } |
| 707 | writeln!(out, ") noexcept {{"); |
| 708 | write!(out, " "); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 709 | write_return_type(out, &efn.ret); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 710 | match &efn.receiver { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 711 | None => write!(out, "(*{}$)(", efn.name.rust), |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 712 | Some(receiver) => write!( |
| 713 | out, |
| 714 | "({}::*{}$)(", |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 715 | out.types.resolve(&receiver.ty).name.to_fully_qualified(), |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 716 | efn.name.rust, |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 717 | ), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 718 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 719 | for (i, arg) in efn.args.iter().enumerate() { |
| 720 | if i > 0 { |
| 721 | write!(out, ", "); |
| 722 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 723 | write_type(out, &arg.ty); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 724 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 725 | write!(out, ")"); |
| David Tolnay | 4e7123f | 2020-04-19 21:11:37 -0700 | [diff] [blame] | 726 | if let Some(receiver) = &efn.receiver { |
| David Tolnay | 9c4ac2e | 2020-11-15 21:14:03 -0800 | [diff] [blame] | 727 | if !receiver.mutable { |
| David Tolnay | 4e7123f | 2020-04-19 21:11:37 -0700 | [diff] [blame] | 728 | write!(out, " const"); |
| 729 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 730 | } |
| 731 | write!(out, " = "); |
| 732 | match &efn.receiver { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 733 | None => write!(out, "{}", efn.name.to_fully_qualified()), |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 734 | Some(receiver) => write!( |
| 735 | out, |
| 736 | "&{}::{}", |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 737 | out.types.resolve(&receiver.ty).name.to_fully_qualified(), |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 738 | efn.name.cxx, |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 739 | ), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 740 | } |
| 741 | writeln!(out, ";"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 742 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 743 | if efn.throws { |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 744 | out.builtin.ptr_len = true; |
| David Tolnay | 74d6d51 | 2020-10-31 22:22:03 -0700 | [diff] [blame] | 745 | out.builtin.trycatch = true; |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 746 | writeln!(out, "::rust::repr::PtrLen throw$;"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 747 | writeln!(out, " ::rust::behavior::trycatch("); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 748 | writeln!(out, " [&] {{"); |
| 749 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 750 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 751 | if indirect_return { |
| David Tolnay | 0ecd05a | 2020-07-29 16:32:03 -0700 | [diff] [blame] | 752 | out.include.new = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 753 | write!(out, "new (return$) "); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 754 | write_indirect_return_type(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 755 | write!(out, "("); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 756 | } else if efn.ret.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 757 | write!(out, "return "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 758 | } |
| David Tolnay | 681f5c8 | 2021-01-01 22:56:27 -0800 | [diff] [blame] | 759 | if let Some(Type::Ref(_)) = efn.ret { |
| 760 | write!(out, "&"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 761 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 762 | match &efn.receiver { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 763 | None => write!(out, "{}$(", efn.name.rust), |
| 764 | Some(_) => write!(out, "(self.*{}$)(", efn.name.rust), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 765 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 766 | for (i, arg) in efn.args.iter().enumerate() { |
| 767 | if i > 0 { |
| 768 | write!(out, ", "); |
| 769 | } |
| 770 | if let Type::RustBox(_) = &arg.ty { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 771 | write_type(out, &arg.ty); |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 772 | write!(out, "::from_raw({})", arg.name.cxx); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 773 | } else if let Type::UniquePtr(_) = &arg.ty { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 774 | write_type(out, &arg.ty); |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 775 | write!(out, "({})", arg.name.cxx); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 776 | } else if arg.ty == RustString { |
| David Tolnay | 74d6d51 | 2020-10-31 22:22:03 -0700 | [diff] [blame] | 777 | out.builtin.unsafe_bitcopy = true; |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 778 | write!( |
| 779 | out, |
| 780 | "::rust::String(::rust::unsafe_bitcopy, *{})", |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 781 | arg.name.cxx, |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 782 | ); |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 783 | } else if let Type::RustVec(_) = arg.ty { |
| David Tolnay | 74d6d51 | 2020-10-31 22:22:03 -0700 | [diff] [blame] | 784 | out.builtin.unsafe_bitcopy = true; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 785 | write_type(out, &arg.ty); |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 786 | write!(out, "(::rust::unsafe_bitcopy, *{})", arg.name.cxx); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 787 | } else if out.types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 788 | out.include.utility = true; |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 789 | write!(out, "::std::move(*{})", arg.name.cxx); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 790 | } else { |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 791 | write!(out, "{}", arg.name.cxx); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | write!(out, ")"); |
| 795 | match &efn.ret { |
| 796 | Some(Type::RustBox(_)) => write!(out, ".into_raw()"), |
| 797 | Some(Type::UniquePtr(_)) => write!(out, ".release()"), |
| 798 | _ => {} |
| 799 | } |
| 800 | if indirect_return { |
| 801 | write!(out, ")"); |
| 802 | } |
| 803 | writeln!(out, ";"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 804 | if efn.throws { |
| 805 | out.include.cstring = true; |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 806 | out.builtin.exception = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 807 | writeln!(out, " throw$.ptr = nullptr;"); |
| 808 | writeln!(out, " }},"); |
| David Tolnay | 82c1617 | 2020-03-17 22:54:12 -0700 | [diff] [blame] | 809 | writeln!(out, " [&](const char *catch$) noexcept {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 810 | writeln!(out, " throw$.len = ::std::strlen(catch$);"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 811 | writeln!( |
| 812 | out, |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 813 | " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 814 | ); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 815 | writeln!(out, " }});"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 816 | writeln!(out, " return throw$;"); |
| 817 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 818 | writeln!(out, "}}"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 819 | for arg in &efn.args { |
| 820 | if let Type::Fn(f) = &arg.ty { |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 821 | let var = &arg.name; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 822 | write_function_pointer_trampoline(out, efn, var, f); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
| David Tolnay | ca563ee | 2020-11-01 20:12:27 -0800 | [diff] [blame] | 825 | out.end_block(Block::ExternC); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 828 | fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 829 | let r_trampoline = mangle::r_trampoline(efn, var, out.types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 830 | let indirect_call = true; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 831 | write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 832 | |
| 833 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 834 | let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string(); |
| 835 | write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 836 | } |
| 837 | |
| David Tolnay | 0b9b9f8 | 2020-11-01 20:41:00 -0800 | [diff] [blame] | 838 | fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 839 | out.set_namespace(&efn.name.namespace); |
| David Tolnay | ca563ee | 2020-11-01 20:12:27 -0800 | [diff] [blame] | 840 | out.begin_block(Block::ExternC); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 841 | let link_name = mangle::extern_fn(efn, out.types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 842 | let indirect_call = false; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 843 | write_rust_function_decl_impl(out, &link_name, efn, indirect_call); |
| David Tolnay | ca563ee | 2020-11-01 20:12:27 -0800 | [diff] [blame] | 844 | out.end_block(Block::ExternC); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | fn write_rust_function_decl_impl( |
| 848 | out: &mut OutFile, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 849 | link_name: &Symbol, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 850 | sig: &Signature, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 851 | indirect_call: bool, |
| 852 | ) { |
| David Tolnay | 0477074 | 2020-11-01 13:50:50 -0800 | [diff] [blame] | 853 | out.next_section(); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 854 | if sig.throws { |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 855 | out.builtin.ptr_len = true; |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 856 | write!(out, "::rust::repr::PtrLen "); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 857 | } else { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 858 | write_extern_return_type_space(out, &sig.ret); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 859 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 860 | write!(out, "{}(", link_name); |
| 861 | let mut needs_comma = false; |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 862 | if let Some(receiver) = &sig.receiver { |
| David Tolnay | 9c4ac2e | 2020-11-15 21:14:03 -0800 | [diff] [blame] | 863 | if !receiver.mutable { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 864 | write!(out, "const "); |
| 865 | } |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 866 | write!( |
| 867 | out, |
| 868 | "{} &self", |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 869 | out.types.resolve(&receiver.ty).name.to_fully_qualified(), |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 870 | ); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 871 | needs_comma = true; |
| 872 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 873 | for arg in &sig.args { |
| 874 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 875 | write!(out, ", "); |
| 876 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 877 | write_extern_arg(out, arg); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 878 | needs_comma = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 879 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 880 | if indirect_return(sig, out.types) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 881 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 882 | write!(out, ", "); |
| 883 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 884 | write_return_type(out, &sig.ret); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 885 | write!(out, "*return$"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 886 | needs_comma = true; |
| 887 | } |
| 888 | if indirect_call { |
| 889 | if needs_comma { |
| 890 | write!(out, ", "); |
| 891 | } |
| 892 | write!(out, "void *"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 893 | } |
| 894 | writeln!(out, ") noexcept;"); |
| 895 | } |
| 896 | |
| David Tolnay | 0b9b9f8 | 2020-11-01 20:41:00 -0800 | [diff] [blame] | 897 | fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 898 | out.set_namespace(&efn.name.namespace); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 899 | for line in efn.doc.to_string().lines() { |
| 900 | writeln!(out, "//{}", line); |
| 901 | } |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 902 | let local_name = match &efn.sig.receiver { |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 903 | None => efn.name.cxx.to_string(), |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 904 | Some(receiver) => format!( |
| 905 | "{}::{}", |
| 906 | out.types.resolve(&receiver.ty).name.cxx, |
| 907 | efn.name.cxx, |
| 908 | ), |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 909 | }; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 910 | let invoke = mangle::extern_fn(efn, out.types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 911 | let indirect_call = false; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 912 | write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 915 | fn write_rust_function_shim_decl( |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 916 | out: &mut OutFile, |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 917 | local_name: &str, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 918 | sig: &Signature, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 919 | indirect_call: bool, |
| 920 | ) { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 921 | write_return_type(out, &sig.ret); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 922 | write!(out, "{}(", local_name); |
| 923 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 924 | if i > 0 { |
| 925 | write!(out, ", "); |
| 926 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 927 | write_type_space(out, &arg.ty); |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 928 | write!(out, "{}", arg.name.cxx); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 929 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 930 | if indirect_call { |
| 931 | if !sig.args.is_empty() { |
| 932 | write!(out, ", "); |
| 933 | } |
| 934 | write!(out, "void *extern$"); |
| 935 | } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 936 | write!(out, ")"); |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 937 | if let Some(receiver) = &sig.receiver { |
| David Tolnay | 9c4ac2e | 2020-11-15 21:14:03 -0800 | [diff] [blame] | 938 | if !receiver.mutable { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 939 | write!(out, " const"); |
| 940 | } |
| 941 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 942 | if !sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 943 | write!(out, " noexcept"); |
| 944 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | fn write_rust_function_shim_impl( |
| 948 | out: &mut OutFile, |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 949 | local_name: &str, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 950 | sig: &Signature, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 951 | invoke: &Symbol, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 952 | indirect_call: bool, |
| 953 | ) { |
| 954 | if out.header && sig.receiver.is_some() { |
| 955 | // We've already defined this inside the struct. |
| 956 | return; |
| 957 | } |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 958 | if !out.header { |
| 959 | begin_function_definition(out); |
| 960 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 961 | write_rust_function_shim_decl(out, local_name, sig, indirect_call); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 962 | if out.header { |
| 963 | writeln!(out, ";"); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 964 | return; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 965 | } |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 966 | writeln!(out, " {{"); |
| 967 | for arg in &sig.args { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 968 | if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 969 | out.include.utility = true; |
| David Tolnay | 74d6d51 | 2020-10-31 22:22:03 -0700 | [diff] [blame] | 970 | out.builtin.manually_drop = true; |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 971 | write!(out, " ::rust::ManuallyDrop<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 972 | write_type(out, &arg.ty); |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 973 | writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | write!(out, " "); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 977 | let indirect_return = indirect_return(sig, out.types); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 978 | if indirect_return { |
| David Tolnay | 74d6d51 | 2020-10-31 22:22:03 -0700 | [diff] [blame] | 979 | out.builtin.maybe_uninit = true; |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 980 | write!(out, "::rust::MaybeUninit<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 981 | write_type(out, sig.ret.as_ref().unwrap()); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 982 | writeln!(out, "> return$;"); |
| 983 | write!(out, " "); |
| 984 | } else if let Some(ret) = &sig.ret { |
| 985 | write!(out, "return "); |
| 986 | match ret { |
| 987 | Type::RustBox(_) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 988 | write_type(out, ret); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 989 | write!(out, "::from_raw("); |
| 990 | } |
| 991 | Type::UniquePtr(_) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 992 | write_type(out, ret); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 993 | write!(out, "("); |
| 994 | } |
| 995 | Type::Ref(_) => write!(out, "*"), |
| 996 | _ => {} |
| 997 | } |
| 998 | } |
| 999 | if sig.throws { |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 1000 | out.builtin.ptr_len = true; |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 1001 | write!(out, "::rust::repr::PtrLen error$ = "); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1002 | } |
| 1003 | write!(out, "{}(", invoke); |
| David Tolnay | 9d84036 | 2020-11-10 08:50:40 -0800 | [diff] [blame] | 1004 | let mut needs_comma = false; |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1005 | if sig.receiver.is_some() { |
| 1006 | write!(out, "*this"); |
| David Tolnay | 9d84036 | 2020-11-10 08:50:40 -0800 | [diff] [blame] | 1007 | needs_comma = true; |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1008 | } |
| David Tolnay | 9d84036 | 2020-11-10 08:50:40 -0800 | [diff] [blame] | 1009 | for arg in &sig.args { |
| 1010 | if needs_comma { |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1011 | write!(out, ", "); |
| 1012 | } |
| David Tolnay | 681f5c8 | 2021-01-01 22:56:27 -0800 | [diff] [blame] | 1013 | if out.types.needs_indirect_abi(&arg.ty) { |
| 1014 | write!(out, "&"); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1015 | } |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 1016 | write!(out, "{}", arg.name.cxx); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1017 | match &arg.ty { |
| 1018 | Type::RustBox(_) => write!(out, ".into_raw()"), |
| 1019 | Type::UniquePtr(_) => write!(out, ".release()"), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1020 | ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"), |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1021 | _ => {} |
| 1022 | } |
| David Tolnay | 9d84036 | 2020-11-10 08:50:40 -0800 | [diff] [blame] | 1023 | needs_comma = true; |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1024 | } |
| 1025 | if indirect_return { |
| David Tolnay | 9d84036 | 2020-11-10 08:50:40 -0800 | [diff] [blame] | 1026 | if needs_comma { |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1027 | write!(out, ", "); |
| 1028 | } |
| 1029 | write!(out, "&return$.value"); |
| David Tolnay | 9d84036 | 2020-11-10 08:50:40 -0800 | [diff] [blame] | 1030 | needs_comma = true; |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1031 | } |
| 1032 | if indirect_call { |
| David Tolnay | 9d84036 | 2020-11-10 08:50:40 -0800 | [diff] [blame] | 1033 | if needs_comma { |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1034 | write!(out, ", "); |
| 1035 | } |
| 1036 | write!(out, "extern$"); |
| 1037 | } |
| 1038 | write!(out, ")"); |
| David Tolnay | 22602b4 | 2020-09-21 18:04:05 -0400 | [diff] [blame] | 1039 | if !indirect_return { |
| 1040 | if let Some(ret) = &sig.ret { |
| David Tolnay | 681f5c8 | 2021-01-01 22:56:27 -0800 | [diff] [blame] | 1041 | if let Type::RustBox(_) | Type::UniquePtr(_) = ret { |
| David Tolnay | 22602b4 | 2020-09-21 18:04:05 -0400 | [diff] [blame] | 1042 | write!(out, ")"); |
| 1043 | } |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | writeln!(out, ";"); |
| 1047 | if sig.throws { |
| David Tolnay | 74d6d51 | 2020-10-31 22:22:03 -0700 | [diff] [blame] | 1048 | out.builtin.rust_error = true; |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 1049 | writeln!(out, " if (error$.ptr) {{"); |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 1050 | writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);"); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 1051 | writeln!(out, " }}"); |
| 1052 | } |
| 1053 | if indirect_return { |
| 1054 | out.include.utility = true; |
| 1055 | writeln!(out, " return ::std::move(return$.value);"); |
| 1056 | } |
| 1057 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1058 | } |
| 1059 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1060 | fn write_return_type(out: &mut OutFile, ty: &Option<Type>) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1061 | match ty { |
| 1062 | None => write!(out, "void "), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1063 | Some(ty) => write_type_space(out, ty), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1067 | fn indirect_return(sig: &Signature, types: &Types) -> bool { |
| 1068 | sig.ret |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 1069 | .as_ref() |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1070 | .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret)) |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1073 | fn write_indirect_return_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1074 | match ty { |
| 1075 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1076 | write_type_space(out, &ty.inner); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1077 | write!(out, "*"); |
| 1078 | } |
| 1079 | Type::Ref(ty) => { |
| David Tolnay | 9c4ac2e | 2020-11-15 21:14:03 -0800 | [diff] [blame] | 1080 | if !ty.mutable { |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1081 | write!(out, "const "); |
| 1082 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1083 | write_type(out, &ty.inner); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1084 | write!(out, " *"); |
| 1085 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1086 | _ => write_type(out, ty), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1090 | fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) { |
| 1091 | write_indirect_return_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1092 | match ty { |
| 1093 | Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {} |
| David Tolnay | 73b7264 | 2020-11-25 17:44:05 -0800 | [diff] [blame] | 1094 | Type::Str(_) | Type::SliceRef(_) => write!(out, " "), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1095 | _ => write_space_after_type(out, ty), |
| 1096 | } |
| 1097 | } |
| 1098 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1099 | fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1100 | match ty { |
| 1101 | Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1102 | write_type_space(out, &ty.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1103 | write!(out, "*"); |
| 1104 | } |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 1105 | Some(Type::Ref(ty)) => { |
| David Tolnay | 9c4ac2e | 2020-11-15 21:14:03 -0800 | [diff] [blame] | 1106 | if !ty.mutable { |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 1107 | write!(out, "const "); |
| 1108 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1109 | write_type(out, &ty.inner); |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 1110 | write!(out, " *"); |
| 1111 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1112 | Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "), |
| 1113 | _ => write_return_type(out, ty), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1117 | fn write_extern_arg(out: &mut OutFile, arg: &Var) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1118 | match &arg.ty { |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 1119 | Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1120 | write_type_space(out, &ty.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1121 | write!(out, "*"); |
| 1122 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1123 | _ => write_type_space(out, &arg.ty), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1124 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1125 | if out.types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1126 | write!(out, "*"); |
| 1127 | } |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 1128 | write!(out, "{}", arg.name.cxx); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1129 | } |
| 1130 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1131 | fn write_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1132 | match ty { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1133 | Type::Ident(ident) => match Atom::from(&ident.rust) { |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 1134 | Some(atom) => write_atom(out, atom), |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 1135 | None => write!( |
| 1136 | out, |
| 1137 | "{}", |
| 1138 | out.types.resolve(ident).name.to_fully_qualified(), |
| 1139 | ), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1140 | }, |
| 1141 | Type::RustBox(ty) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 1142 | write!(out, "::rust::Box<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1143 | write_type(out, &ty.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1144 | write!(out, ">"); |
| 1145 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1146 | Type::RustVec(ty) => { |
| 1147 | write!(out, "::rust::Vec<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1148 | write_type(out, &ty.inner); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1149 | write!(out, ">"); |
| 1150 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1151 | Type::UniquePtr(ptr) => { |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1152 | write!(out, "::std::unique_ptr<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1153 | write_type(out, &ptr.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1154 | write!(out, ">"); |
| 1155 | } |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1156 | Type::SharedPtr(ptr) => { |
| 1157 | write!(out, "::std::shared_ptr<"); |
| 1158 | write_type(out, &ptr.inner); |
| 1159 | write!(out, ">"); |
| 1160 | } |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 1161 | Type::WeakPtr(ptr) => { |
| 1162 | write!(out, "::std::weak_ptr<"); |
| 1163 | write_type(out, &ptr.inner); |
| 1164 | write!(out, ">"); |
| 1165 | } |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 1166 | Type::CxxVector(ty) => { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1167 | write!(out, "::std::vector<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1168 | write_type(out, &ty.inner); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1169 | write!(out, ">"); |
| 1170 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1171 | Type::Ref(r) => { |
| David Tolnay | 9c4ac2e | 2020-11-15 21:14:03 -0800 | [diff] [blame] | 1172 | if !r.mutable { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1173 | write!(out, "const "); |
| 1174 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1175 | write_type(out, &r.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1176 | write!(out, " &"); |
| 1177 | } |
| 1178 | Type::Str(_) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 1179 | write!(out, "::rust::Str"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1180 | } |
| David Tolnay | 5515a9e | 2020-11-25 19:07:54 -0800 | [diff] [blame] | 1181 | Type::SliceRef(slice) => { |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 1182 | write!(out, "::rust::Slice<"); |
| David Tolnay | 5515a9e | 2020-11-25 19:07:54 -0800 | [diff] [blame] | 1183 | if slice.mutability.is_none() { |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 1184 | write!(out, "const "); |
| 1185 | } |
| David Tolnay | 5515a9e | 2020-11-25 19:07:54 -0800 | [diff] [blame] | 1186 | write_type(out, &slice.inner); |
| 1187 | write!(out, ">"); |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 1188 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1189 | Type::Fn(f) => { |
| David Tolnay | f031c32 | 2020-11-29 19:41:33 -0800 | [diff] [blame] | 1190 | write!(out, "::rust::Fn<"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1191 | match &f.ret { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1192 | Some(ret) => write_type(out, ret), |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1193 | None => write!(out, "void"), |
| 1194 | } |
| 1195 | write!(out, "("); |
| 1196 | for (i, arg) in f.args.iter().enumerate() { |
| 1197 | if i > 0 { |
| 1198 | write!(out, ", "); |
| 1199 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1200 | write_type(out, &arg.ty); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1201 | } |
| 1202 | write!(out, ")>"); |
| 1203 | } |
| Xiangpeng Hao | 7876235 | 2020-11-12 10:24:18 +0800 | [diff] [blame] | 1204 | Type::Array(a) => { |
| 1205 | write!(out, "::std::array<"); |
| 1206 | write_type(out, &a.inner); |
| 1207 | write!(out, ", {}>", &a.len); |
| 1208 | } |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 1209 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 1213 | fn write_atom(out: &mut OutFile, atom: Atom) { |
| 1214 | match atom { |
| 1215 | Bool => write!(out, "bool"), |
| David Tolnay | b3873dc | 2020-11-25 19:47:49 -0800 | [diff] [blame] | 1216 | Char => write!(out, "char"), |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1217 | U8 => write!(out, "::std::uint8_t"), |
| 1218 | U16 => write!(out, "::std::uint16_t"), |
| 1219 | U32 => write!(out, "::std::uint32_t"), |
| 1220 | U64 => write!(out, "::std::uint64_t"), |
| 1221 | Usize => write!(out, "::std::size_t"), |
| 1222 | I8 => write!(out, "::std::int8_t"), |
| 1223 | I16 => write!(out, "::std::int16_t"), |
| 1224 | I32 => write!(out, "::std::int32_t"), |
| 1225 | I64 => write!(out, "::std::int64_t"), |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 1226 | Isize => write!(out, "::rust::isize"), |
| 1227 | F32 => write!(out, "float"), |
| 1228 | F64 => write!(out, "double"), |
| 1229 | CxxString => write!(out, "::std::string"), |
| 1230 | RustString => write!(out, "::rust::String"), |
| 1231 | } |
| 1232 | } |
| 1233 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1234 | fn write_type_space(out: &mut OutFile, ty: &Type) { |
| 1235 | write_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1236 | write_space_after_type(out, ty); |
| 1237 | } |
| 1238 | |
| 1239 | fn write_space_after_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1240 | match ty { |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 1241 | Type::Ident(_) |
| 1242 | | Type::RustBox(_) |
| 1243 | | Type::UniquePtr(_) |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1244 | | Type::SharedPtr(_) |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 1245 | | Type::WeakPtr(_) |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 1246 | | Type::Str(_) |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 1247 | | Type::CxxVector(_) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1248 | | Type::RustVec(_) |
| David Tolnay | 73b7264 | 2020-11-25 17:44:05 -0800 | [diff] [blame] | 1249 | | Type::SliceRef(_) |
| Xiangpeng Hao | 7876235 | 2020-11-12 10:24:18 +0800 | [diff] [blame] | 1250 | | Type::Fn(_) |
| 1251 | | Type::Array(_) => write!(out, " "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1252 | Type::Ref(_) => {} |
| David Tolnay | e0dca7b | 2020-11-25 17:18:57 -0800 | [diff] [blame] | 1253 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1257 | #[derive(Copy, Clone)] |
| 1258 | enum UniquePtr<'a> { |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1259 | Ident(&'a Ident), |
| 1260 | CxxVector(&'a Ident), |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | trait ToTypename { |
| 1264 | fn to_typename(&self, types: &Types) -> String; |
| 1265 | } |
| 1266 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1267 | impl ToTypename for Ident { |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1268 | fn to_typename(&self, types: &Types) -> String { |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 1269 | types.resolve(self).name.to_fully_qualified() |
| David Tolnay | 2eca4a0 | 2020-04-24 19:50:51 -0700 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1273 | impl<'a> ToTypename for UniquePtr<'a> { |
| 1274 | fn to_typename(&self, types: &Types) -> String { |
| 1275 | match self { |
| 1276 | UniquePtr::Ident(ident) => ident.to_typename(types), |
| 1277 | UniquePtr::CxxVector(element) => { |
| 1278 | format!("::std::vector<{}>", element.to_typename(types)) |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | trait ToMangled { |
| 1285 | fn to_mangled(&self, types: &Types) -> Symbol; |
| 1286 | } |
| 1287 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1288 | impl ToMangled for Ident { |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1289 | fn to_mangled(&self, types: &Types) -> Symbol { |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 1290 | types.resolve(self).name.to_symbol() |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | impl<'a> ToMangled for UniquePtr<'a> { |
| 1295 | fn to_mangled(&self, types: &Types) -> Symbol { |
| 1296 | match self { |
| 1297 | UniquePtr::Ident(ident) => ident.to_mangled(types), |
| 1298 | UniquePtr::CxxVector(element) => element.to_mangled(types).prefix_with("std$vector$"), |
| 1299 | } |
| David Tolnay | bae50ef | 2020-04-25 12:38:41 -0700 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1303 | fn write_generic_instantiations(out: &mut OutFile) { |
| David Tolnay | 169bb47 | 2020-11-01 21:04:24 -0800 | [diff] [blame] | 1304 | if out.header { |
| 1305 | return; |
| 1306 | } |
| 1307 | |
| 1308 | out.next_section(); |
| David Tolnay | 078c90f | 2020-11-01 13:31:08 -0800 | [diff] [blame] | 1309 | out.set_namespace(Default::default()); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 1310 | out.begin_block(Block::ExternC); |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1311 | for impl_key in out.types.impls.keys() { |
| 1312 | out.next_section(); |
| 1313 | match impl_key { |
| 1314 | ImplKey::RustBox(ident) => write_rust_box_extern(out, ident), |
| 1315 | ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident), |
| 1316 | ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident), |
| 1317 | ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident), |
| 1318 | ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident), |
| 1319 | ImplKey::CxxVector(ident) => write_cxx_vector(out, ident), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1320 | } |
| 1321 | } |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 1322 | out.end_block(Block::ExternC); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1323 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 1324 | out.begin_block(Block::Namespace("rust")); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1325 | out.begin_block(Block::InlineNamespace("cxxbridge1")); |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1326 | for impl_key in out.types.impls.keys() { |
| 1327 | match impl_key { |
| 1328 | ImplKey::RustBox(ident) => write_rust_box_impl(out, ident), |
| 1329 | ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident), |
| 1330 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1331 | } |
| 1332 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1333 | out.end_block(Block::InlineNamespace("cxxbridge1")); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 1334 | out.end_block(Block::Namespace("rust")); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1337 | fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) { |
| 1338 | let resolve = out.types.resolve(ident); |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 1339 | let inner = resolve.name.to_fully_qualified(); |
| 1340 | let instance = resolve.name.to_symbol(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1341 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1342 | writeln!( |
| 1343 | out, |
| David Tolnay | c4b3422 | 2020-12-12 13:06:26 -0800 | [diff] [blame] | 1344 | "{} *cxxbridge1$box${}$alloc() noexcept;", |
| 1345 | inner, instance, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1346 | ); |
| 1347 | writeln!( |
| 1348 | out, |
| David Tolnay | 25b3c1d | 2020-12-12 15:50:10 -0800 | [diff] [blame] | 1349 | "void cxxbridge1$box${}$dealloc({} *) noexcept;", |
| 1350 | instance, inner, |
| 1351 | ); |
| 1352 | writeln!( |
| 1353 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1354 | "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1355 | instance, inner, |
| 1356 | ); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1357 | } |
| 1358 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1359 | fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) { |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1360 | let inner = element.to_typename(out.types); |
| 1361 | let instance = element.to_mangled(out.types); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1362 | |
| David Tolnay | 12320e1 | 2020-12-09 23:09:36 -0800 | [diff] [blame] | 1363 | out.include.cstddef = true; |
| 1364 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1365 | writeln!( |
| 1366 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1367 | "void cxxbridge1$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;", |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1368 | instance, inner, |
| 1369 | ); |
| 1370 | writeln!( |
| 1371 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1372 | "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1373 | instance, inner, |
| 1374 | ); |
| 1375 | writeln!( |
| 1376 | out, |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1377 | "::std::size_t cxxbridge1$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1378 | instance, inner, |
| 1379 | ); |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1380 | writeln!( |
| 1381 | out, |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1382 | "::std::size_t cxxbridge1$rust_vec${}$capacity(const ::rust::Vec<{}> *ptr) noexcept;", |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame] | 1383 | instance, inner, |
| 1384 | ); |
| 1385 | writeln!( |
| 1386 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1387 | "const {} *cxxbridge1$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;", |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1388 | inner, instance, |
| 1389 | ); |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1390 | writeln!( |
| 1391 | out, |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1392 | "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;", |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 1393 | instance, inner, |
| 1394 | ); |
| 1395 | writeln!( |
| 1396 | out, |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1397 | "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;", |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 1398 | instance, inner, |
| 1399 | ); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1400 | } |
| 1401 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1402 | fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) { |
| 1403 | let resolve = out.types.resolve(ident); |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 1404 | let inner = resolve.name.to_fully_qualified(); |
| 1405 | let instance = resolve.name.to_symbol(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1406 | |
| 1407 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1408 | begin_function_definition(out); |
| David Tolnay | e570316 | 2020-12-12 16:26:35 -0800 | [diff] [blame] | 1409 | writeln!( |
| 1410 | out, |
| 1411 | "{} *Box<{}>::allocation::alloc() noexcept {{", |
| 1412 | inner, inner, |
| 1413 | ); |
| David Tolnay | c4b3422 | 2020-12-12 13:06:26 -0800 | [diff] [blame] | 1414 | writeln!(out, " return cxxbridge1$box${}$alloc();", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1415 | writeln!(out, "}}"); |
| 1416 | |
| 1417 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1418 | begin_function_definition(out); |
| David Tolnay | 25b3c1d | 2020-12-12 15:50:10 -0800 | [diff] [blame] | 1419 | writeln!( |
| 1420 | out, |
| David Tolnay | e570316 | 2020-12-12 16:26:35 -0800 | [diff] [blame] | 1421 | "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{", |
| David Tolnay | 25b3c1d | 2020-12-12 15:50:10 -0800 | [diff] [blame] | 1422 | inner, inner, |
| 1423 | ); |
| 1424 | writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance); |
| 1425 | writeln!(out, "}}"); |
| 1426 | |
| 1427 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1428 | begin_function_definition(out); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 1429 | writeln!(out, "void Box<{}>::drop() noexcept {{", inner); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1430 | writeln!(out, " cxxbridge1$box${}$drop(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1431 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1432 | } |
| 1433 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1434 | fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) { |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1435 | let inner = element.to_typename(out.types); |
| 1436 | let instance = element.to_mangled(out.types); |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 1437 | |
| David Tolnay | 12320e1 | 2020-12-09 23:09:36 -0800 | [diff] [blame] | 1438 | out.include.cstddef = true; |
| 1439 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1440 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1441 | begin_function_definition(out); |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1442 | writeln!(out, "Vec<{}>::Vec() noexcept {{", inner); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1443 | writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance); |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1444 | writeln!(out, "}}"); |
| 1445 | |
| 1446 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1447 | begin_function_definition(out); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1448 | writeln!(out, "void Vec<{}>::drop() noexcept {{", inner); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1449 | writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1450 | writeln!(out, "}}"); |
| 1451 | |
| 1452 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1453 | begin_function_definition(out); |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1454 | writeln!( |
| 1455 | out, |
| 1456 | "::std::size_t Vec<{}>::size() const noexcept {{", |
| 1457 | inner, |
| 1458 | ); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1459 | writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1460 | writeln!(out, "}}"); |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1461 | |
| 1462 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1463 | begin_function_definition(out); |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1464 | writeln!( |
| 1465 | out, |
| 1466 | "::std::size_t Vec<{}>::capacity() const noexcept {{", |
| 1467 | inner, |
| 1468 | ); |
| David Tolnay | 381d953 | 2020-12-11 13:59:45 -0800 | [diff] [blame] | 1469 | writeln!( |
| 1470 | out, |
| 1471 | " return cxxbridge1$rust_vec${}$capacity(this);", |
| 1472 | instance, |
| 1473 | ); |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame] | 1474 | writeln!(out, "}}"); |
| 1475 | |
| 1476 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1477 | begin_function_definition(out); |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1478 | writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1479 | writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance); |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1480 | writeln!(out, "}}"); |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1481 | |
| 1482 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1483 | begin_function_definition(out); |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 1484 | writeln!( |
| 1485 | out, |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1486 | "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{", |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 1487 | inner, |
| 1488 | ); |
| 1489 | writeln!( |
| 1490 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1491 | " return cxxbridge1$rust_vec${}$reserve_total(this, cap);", |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 1492 | instance, |
| 1493 | ); |
| 1494 | writeln!(out, "}}"); |
| 1495 | |
| 1496 | writeln!(out, "template <>"); |
| David Tolnay | b478fcb | 2020-12-29 16:48:02 -0800 | [diff] [blame] | 1497 | begin_function_definition(out); |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1498 | writeln!( |
| 1499 | out, |
| 1500 | "void Vec<{}>::set_len(::std::size_t len) noexcept {{", |
| 1501 | inner, |
| 1502 | ); |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 1503 | writeln!( |
| 1504 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1505 | " return cxxbridge1$rust_vec${}$set_len(this, len);", |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 1506 | instance, |
| 1507 | ); |
| 1508 | writeln!(out, "}}"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1511 | fn write_unique_ptr(out: &mut OutFile, ident: &Ident) { |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1512 | let ty = UniquePtr::Ident(ident); |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1513 | write_unique_ptr_common(out, ty); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | // Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>. |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1517 | fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) { |
| David Tolnay | 0ecd05a | 2020-07-29 16:32:03 -0700 | [diff] [blame] | 1518 | out.include.new = true; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1519 | out.include.utility = true; |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1520 | let inner = ty.to_typename(out.types); |
| 1521 | let instance = ty.to_mangled(out.types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1522 | |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1523 | let can_construct_from_value = match ty { |
| David Tolnay | ca0f9da | 2020-10-16 13:16:17 -0700 | [diff] [blame] | 1524 | // Some aliases are to opaque types; some are to trivial types. We can't |
| 1525 | // know at code generation time, so we generate both C++ and Rust side |
| 1526 | // bindings for a "new" method anyway. But the Rust code can't be called |
| 1527 | // for Opaque types because the 'new' method is not implemented. |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1528 | UniquePtr::Ident(ident) => { |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1529 | out.types.structs.contains_key(ident) |
| 1530 | || out.types.enums.contains_key(ident) |
| 1531 | || out.types.aliases.contains_key(ident) |
| David Tolnay | ca0f9da | 2020-10-16 13:16:17 -0700 | [diff] [blame] | 1532 | } |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1533 | UniquePtr::CxxVector(_) => false, |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1534 | }; |
| 1535 | |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 1536 | let conditional_delete = match ty { |
| 1537 | UniquePtr::Ident(ident) => { |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1538 | !out.types.structs.contains_key(ident) && !out.types.enums.contains_key(ident) |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 1539 | } |
| 1540 | UniquePtr::CxxVector(_) => false, |
| 1541 | }; |
| 1542 | |
| 1543 | if conditional_delete { |
| 1544 | out.builtin.is_complete = true; |
| 1545 | let definition = match ty { |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 1546 | UniquePtr::Ident(ty) => &out.types.resolve(ty).name.cxx, |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 1547 | UniquePtr::CxxVector(_) => unreachable!(), |
| 1548 | }; |
| 1549 | writeln!( |
| 1550 | out, |
| David Tolnay | 7506863 | 2020-12-26 22:15:17 -0800 | [diff] [blame] | 1551 | "static_assert(::rust::detail::is_complete<{}>::value, \"definition of {} is required\");", |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 1552 | inner, definition, |
| 1553 | ); |
| 1554 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1555 | writeln!( |
| 1556 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1557 | "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1558 | inner, |
| 1559 | ); |
| 1560 | writeln!( |
| 1561 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1562 | "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1563 | inner, |
| 1564 | ); |
| 1565 | writeln!( |
| 1566 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1567 | "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1568 | instance, inner, |
| 1569 | ); |
| David Tolnay | 718ca0f | 2020-12-09 23:01:11 -0800 | [diff] [blame] | 1570 | writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1571 | writeln!(out, "}}"); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1572 | if can_construct_from_value { |
| David Tolnay | 0b88140 | 2020-12-01 21:05:08 -0800 | [diff] [blame] | 1573 | out.builtin.maybe_uninit = true; |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1574 | writeln!( |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1575 | out, |
| David Tolnay | 0b88140 | 2020-12-01 21:05:08 -0800 | [diff] [blame] | 1576 | "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{", |
| 1577 | inner, instance, inner, |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1578 | ); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1579 | writeln!( |
| 1580 | out, |
| David Tolnay | 0b88140 | 2020-12-01 21:05:08 -0800 | [diff] [blame] | 1581 | " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);", |
| 1582 | inner, inner, inner, |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1583 | ); |
| David Tolnay | 718ca0f | 2020-12-09 23:01:11 -0800 | [diff] [blame] | 1584 | writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner); |
| David Tolnay | 0b88140 | 2020-12-01 21:05:08 -0800 | [diff] [blame] | 1585 | writeln!(out, " return uninit;"); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1586 | writeln!(out, "}}"); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1587 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1588 | writeln!( |
| 1589 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1590 | "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1591 | instance, inner, inner, |
| 1592 | ); |
| David Tolnay | 718ca0f | 2020-12-09 23:01:11 -0800 | [diff] [blame] | 1593 | writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1594 | writeln!(out, "}}"); |
| 1595 | writeln!( |
| 1596 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1597 | "const {} *cxxbridge1$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1598 | inner, instance, inner, |
| 1599 | ); |
| 1600 | writeln!(out, " return ptr.get();"); |
| 1601 | writeln!(out, "}}"); |
| 1602 | writeln!( |
| 1603 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1604 | "{} *cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1605 | inner, instance, inner, |
| 1606 | ); |
| 1607 | writeln!(out, " return ptr.release();"); |
| 1608 | writeln!(out, "}}"); |
| 1609 | writeln!( |
| 1610 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 1611 | "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1612 | instance, inner, |
| 1613 | ); |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 1614 | if conditional_delete { |
| 1615 | out.builtin.deleter_if = true; |
| 1616 | writeln!( |
| 1617 | out, |
| David Tolnay | 7506863 | 2020-12-26 22:15:17 -0800 | [diff] [blame] | 1618 | " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);", |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 1619 | inner, |
| 1620 | ); |
| 1621 | } else { |
| 1622 | writeln!(out, " ptr->~unique_ptr();"); |
| 1623 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1624 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1625 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1626 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1627 | fn write_shared_ptr(out: &mut OutFile, ident: &Ident) { |
| David Tolnay | 95bc57f | 2021-01-01 13:29:44 -0800 | [diff] [blame] | 1628 | let resolve = out.types.resolve(ident); |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 1629 | let inner = resolve.name.to_fully_qualified(); |
| 1630 | let instance = resolve.name.to_symbol(); |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1631 | |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1632 | out.include.new = true; |
| 1633 | out.include.utility = true; |
| 1634 | |
| 1635 | // Some aliases are to opaque types; some are to trivial types. We can't |
| 1636 | // know at code generation time, so we generate both C++ and Rust side |
| 1637 | // bindings for a "new" method anyway. But the Rust code can't be called for |
| 1638 | // Opaque types because the 'new' method is not implemented. |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1639 | let can_construct_from_value = out.types.structs.contains_key(ident) |
| 1640 | || out.types.enums.contains_key(ident) |
| 1641 | || out.types.aliases.contains_key(ident); |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1642 | |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1643 | writeln!( |
| 1644 | out, |
| 1645 | "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");", |
| 1646 | inner, |
| 1647 | ); |
| 1648 | writeln!( |
| 1649 | out, |
| 1650 | "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");", |
| 1651 | inner, |
| 1652 | ); |
| 1653 | writeln!( |
| 1654 | out, |
| 1655 | "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{", |
| 1656 | instance, inner, |
| 1657 | ); |
| David Tolnay | 718ca0f | 2020-12-09 23:01:11 -0800 | [diff] [blame] | 1658 | writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner); |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1659 | writeln!(out, "}}"); |
| 1660 | if can_construct_from_value { |
| David Tolnay | 0b88140 | 2020-12-01 21:05:08 -0800 | [diff] [blame] | 1661 | out.builtin.maybe_uninit = true; |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1662 | writeln!( |
| 1663 | out, |
| David Tolnay | 0b88140 | 2020-12-01 21:05:08 -0800 | [diff] [blame] | 1664 | "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{", |
| 1665 | inner, instance, inner, |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1666 | ); |
| 1667 | writeln!( |
| 1668 | out, |
| David Tolnay | 0b88140 | 2020-12-01 21:05:08 -0800 | [diff] [blame] | 1669 | " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);", |
| 1670 | inner, inner, inner, |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1671 | ); |
| David Tolnay | 718ca0f | 2020-12-09 23:01:11 -0800 | [diff] [blame] | 1672 | writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner); |
| David Tolnay | 0b88140 | 2020-12-01 21:05:08 -0800 | [diff] [blame] | 1673 | writeln!(out, " return uninit;"); |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1674 | writeln!(out, "}}"); |
| 1675 | } |
| 1676 | writeln!( |
| 1677 | out, |
| 1678 | "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{", |
| 1679 | instance, inner, inner, |
| 1680 | ); |
| David Tolnay | 718ca0f | 2020-12-09 23:01:11 -0800 | [diff] [blame] | 1681 | writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner); |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1682 | writeln!(out, "}}"); |
| 1683 | writeln!( |
| 1684 | out, |
| 1685 | "const {} *cxxbridge1$shared_ptr${}$get(const ::std::shared_ptr<{}>& self) noexcept {{", |
| 1686 | inner, instance, inner, |
| 1687 | ); |
| 1688 | writeln!(out, " return self.get();"); |
| 1689 | writeln!(out, "}}"); |
| 1690 | writeln!( |
| 1691 | out, |
| 1692 | "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{", |
| 1693 | instance, inner, |
| 1694 | ); |
| 1695 | writeln!(out, " self->~shared_ptr();"); |
| 1696 | writeln!(out, "}}"); |
| David Tolnay | b3b24a1 | 2020-12-01 15:27:43 -0800 | [diff] [blame] | 1697 | } |
| 1698 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1699 | fn write_weak_ptr(out: &mut OutFile, ident: &Ident) { |
| David Tolnay | 95bc57f | 2021-01-01 13:29:44 -0800 | [diff] [blame] | 1700 | let resolve = out.types.resolve(ident); |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 1701 | let inner = resolve.name.to_fully_qualified(); |
| 1702 | let instance = resolve.name.to_symbol(); |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 1703 | |
| 1704 | out.include.new = true; |
| 1705 | out.include.utility = true; |
| 1706 | |
| 1707 | writeln!( |
| 1708 | out, |
| 1709 | "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");", |
| 1710 | inner, |
| 1711 | ); |
| 1712 | writeln!( |
| 1713 | out, |
| 1714 | "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");", |
| 1715 | inner, |
| 1716 | ); |
| 1717 | writeln!( |
| 1718 | out, |
| 1719 | "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{", |
| 1720 | instance, inner, |
| 1721 | ); |
| 1722 | writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner); |
| 1723 | writeln!(out, "}}"); |
| 1724 | writeln!( |
| 1725 | out, |
| 1726 | "void cxxbridge1$weak_ptr${}$clone(const ::std::weak_ptr<{}>& self, ::std::weak_ptr<{}> *ptr) noexcept {{", |
| 1727 | instance, inner, inner, |
| 1728 | ); |
| 1729 | writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner); |
| 1730 | writeln!(out, "}}"); |
| 1731 | writeln!( |
| 1732 | out, |
| David Tolnay | 85b6bc4 | 2020-12-28 17:47:51 -0800 | [diff] [blame] | 1733 | "void cxxbridge1$weak_ptr${}$downgrade(const ::std::shared_ptr<{}>& shared, ::std::weak_ptr<{}> *weak) noexcept {{", |
| 1734 | instance, inner, inner, |
| 1735 | ); |
| 1736 | writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner); |
| 1737 | writeln!(out, "}}"); |
| 1738 | writeln!( |
| 1739 | out, |
| David Tolnay | 7a48785 | 2020-12-28 18:02:25 -0800 | [diff] [blame] | 1740 | "void cxxbridge1$weak_ptr${}$upgrade(const ::std::weak_ptr<{}>& weak, ::std::shared_ptr<{}> *shared) noexcept {{", |
| 1741 | instance, inner, inner, |
| 1742 | ); |
| 1743 | writeln!( |
| 1744 | out, |
| 1745 | " ::new (shared) ::std::shared_ptr<{}>(weak.lock());", |
| 1746 | inner, |
| 1747 | ); |
| 1748 | writeln!(out, "}}"); |
| 1749 | writeln!( |
| 1750 | out, |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 1751 | "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{", |
| 1752 | instance, inner, |
| 1753 | ); |
| 1754 | writeln!(out, " self->~weak_ptr();"); |
| 1755 | writeln!(out, "}}"); |
| 1756 | } |
| 1757 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 1758 | fn write_cxx_vector(out: &mut OutFile, element: &Ident) { |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1759 | let inner = element.to_typename(out.types); |
| 1760 | let instance = element.to_mangled(out.types); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1761 | |
| David Tolnay | 12320e1 | 2020-12-09 23:09:36 -0800 | [diff] [blame] | 1762 | out.include.cstddef = true; |
| 1763 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1764 | writeln!( |
| 1765 | out, |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 1766 | "::std::size_t cxxbridge1$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1767 | instance, inner, |
| 1768 | ); |
| 1769 | writeln!(out, " return s.size();"); |
| 1770 | writeln!(out, "}}"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1771 | writeln!( |
| 1772 | out, |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 1773 | "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1774 | inner, instance, inner, |
| 1775 | ); |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 1776 | writeln!(out, " return &(*s)[pos];"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1777 | writeln!(out, "}}"); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1778 | |
| David Tolnay | 7082067 | 2020-12-07 11:15:14 -0800 | [diff] [blame] | 1779 | out.include.memory = true; |
| David Tolnay | 1bdb471 | 2020-11-25 07:27:54 -0800 | [diff] [blame] | 1780 | write_unique_ptr_common(out, UniquePtr::CxxVector(element)); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1781 | } |