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