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