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