| Adrian Taylor | 565ddf0 | 2020-10-29 21:12:36 -0700 | [diff] [blame] | 1 | use crate::gen::namespace_organizer::NamespaceEntries; |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 2 | use crate::gen::out::OutFile; |
| 3 | use crate::gen::{include, Opt}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 4 | use crate::syntax::atom::Atom::{self, *}; |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 5 | use crate::syntax::symbol::Symbol; |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 6 | use crate::syntax::{ |
| David Tolnay | 12af7e4 | 2020-10-31 21:09:23 -0700 | [diff] [blame] | 7 | mangle, Api, CppName, Enum, ExternFn, ExternType, IncludeKind, ResolvableName, Signature, |
| 8 | Struct, Type, Types, Var, |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 9 | }; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 10 | use proc_macro2::Ident; |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 11 | use std::collections::HashMap; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 12 | |
| David Tolnay | b560a0f | 2020-10-30 21:28:45 -0700 | [diff] [blame] | 13 | pub(super) fn gen<'a>(apis: &[Api], types: &'a Types, opt: &Opt, header: bool) -> OutFile<'a> { |
| 14 | let mut out_file = OutFile::new(header, types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 15 | let out = &mut out_file; |
| 16 | |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 17 | if header { |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 18 | writeln!(out.include, "#pragma once"); |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| David Tolnay | 4aae7c0 | 2020-10-28 12:35:42 -0700 | [diff] [blame] | 21 | out.include.extend(&opt.include); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 22 | for api in apis { |
| 23 | if let Api::Include(include) = api { |
| David Tolnay | 353d98c | 2020-10-28 15:43:35 -0700 | [diff] [blame] | 24 | out.include.insert(include); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | |
| David Tolnay | 528200f | 2020-10-31 21:02:22 -0700 | [diff] [blame] | 28 | pick_includes_and_builtins(out, apis); |
| David Tolnay | ec66d11 | 2020-10-31 21:00:26 -0700 | [diff] [blame] | 29 | write_builtins(out); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 30 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 31 | out.next_section(); |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 32 | |
| Adrian Taylor | 565ddf0 | 2020-10-29 21:12:36 -0700 | [diff] [blame] | 33 | let apis_by_namespace = NamespaceEntries::new(apis); |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 34 | |
| David Tolnay | 04b8165 | 2020-10-31 22:43:25 -0700 | [diff] [blame] | 35 | gen_namespace_forward_declarations(out, &apis_by_namespace); |
| 36 | gen_namespace_contents(out, &apis_by_namespace, opt); |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 37 | |
| 38 | if !header { |
| 39 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 40 | write_generic_instantiations(out); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 43 | write_includes(out); |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 44 | |
| 45 | out_file |
| 46 | } |
| 47 | |
| David Tolnay | 04b8165 | 2020-10-31 22:43:25 -0700 | [diff] [blame] | 48 | fn gen_namespace_forward_declarations(out: &mut OutFile, ns_entries: &NamespaceEntries) { |
| Adrian Taylor | 565ddf0 | 2020-10-29 21:12:36 -0700 | [diff] [blame] | 49 | let apis = ns_entries.entries(); |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 50 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 51 | out.next_section(); |
| David Tolnay | 630af88 | 2020-10-31 22:03:47 -0700 | [diff] [blame] | 52 | for api in apis { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 53 | match api { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 54 | Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident), |
| 55 | Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx), |
| 56 | Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident), |
| David Tolnay | 7c29546 | 2020-04-25 12:45:07 -0700 | [diff] [blame] | 57 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| Adrian Taylor | f921362 | 2020-10-31 22:25:42 -0700 | [diff] [blame] | 61 | out.next_section(); |
| 62 | |
| 63 | for (child_ns, child_ns_entries) in ns_entries.children() { |
| 64 | writeln!(out, "namespace {} {{", child_ns); |
| David Tolnay | ef0473a | 2020-10-31 22:44:52 -0700 | [diff] [blame] | 65 | gen_namespace_forward_declarations(out, child_ns_entries); |
| Adrian Taylor | f921362 | 2020-10-31 22:25:42 -0700 | [diff] [blame] | 66 | writeln!(out, "}} // namespace {}", child_ns); |
| 67 | } |
| 68 | } |
| 69 | |
| David Tolnay | 04b8165 | 2020-10-31 22:43:25 -0700 | [diff] [blame] | 70 | fn gen_namespace_contents(out: &mut OutFile, ns_entries: &NamespaceEntries, opt: &Opt) { |
| Adrian Taylor | f921362 | 2020-10-31 22:25:42 -0700 | [diff] [blame] | 71 | let apis = ns_entries.entries(); |
| 72 | |
| David Tolnay | f94bef1 | 2020-04-17 14:46:42 -0700 | [diff] [blame] | 73 | let mut methods_for_type = HashMap::new(); |
| David Tolnay | 630af88 | 2020-10-31 22:03:47 -0700 | [diff] [blame] | 74 | for api in apis { |
| David Tolnay | f94bef1 | 2020-04-17 14:46:42 -0700 | [diff] [blame] | 75 | if let Api::RustFunction(efn) = api { |
| 76 | if let Some(receiver) = &efn.sig.receiver { |
| 77 | methods_for_type |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 78 | .entry(&receiver.ty.rust) |
| David Tolnay | f94bef1 | 2020-04-17 14:46:42 -0700 | [diff] [blame] | 79 | .or_insert_with(Vec::new) |
| 80 | .push(efn); |
| 81 | } |
| 82 | } |
| 83 | } |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 84 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 85 | for api in apis { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 86 | match api { |
| 87 | Api::Struct(strct) => { |
| 88 | out.next_section(); |
| David Tolnay | 4d14842 | 2020-10-31 22:41:37 -0700 | [diff] [blame] | 89 | if !out.types.cxx.contains(&strct.ident.rust) { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 90 | write_struct(out, strct); |
| David Tolnay | a593d6e | 2020-08-29 19:48:08 -0700 | [diff] [blame] | 91 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 92 | } |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 93 | Api::Enum(enm) => { |
| 94 | out.next_section(); |
| David Tolnay | 4d14842 | 2020-10-31 22:41:37 -0700 | [diff] [blame] | 95 | if out.types.cxx.contains(&enm.ident.rust) { |
| Joel Galenson | 905eb2e | 2020-05-04 14:58:14 -0700 | [diff] [blame] | 96 | check_enum(out, enm); |
| 97 | } else { |
| 98 | write_enum(out, enm); |
| 99 | } |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 100 | } |
| David Tolnay | c1fe005 | 2020-04-17 15:15:06 -0700 | [diff] [blame] | 101 | Api::RustType(ety) => { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 102 | if let Some(methods) = methods_for_type.get(&ety.ident.rust) { |
| David Tolnay | 46a54e7 | 2020-04-17 14:48:21 -0700 | [diff] [blame] | 103 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 104 | write_struct_with_methods(out, ety, methods); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 105 | } |
| David Tolnay | c1fe005 | 2020-04-17 15:15:06 -0700 | [diff] [blame] | 106 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 107 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| David Tolnay | fabca77 | 2020-10-03 21:25:41 -0700 | [diff] [blame] | 111 | out.next_section(); |
| 112 | for api in apis { |
| 113 | if let Api::TypeAlias(ety) = api { |
| David Tolnay | 4d14842 | 2020-10-31 22:41:37 -0700 | [diff] [blame] | 114 | if out.types.required_trivial.contains_key(&ety.ident.rust) { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 115 | check_trivial_extern_type(out, &ety.ident.cxx) |
| David Tolnay | fabca77 | 2020-10-03 21:25:41 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| David Tolnay | ce5a91f | 2020-10-31 22:42:08 -0700 | [diff] [blame] | 120 | if !out.header { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 121 | out.begin_block("extern \"C\""); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 122 | write_exception_glue(out, apis); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 123 | for api in apis { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 124 | let (efn, write): (_, fn(_, _, _)) = match api { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 125 | Api::CxxFunction(efn) => (efn, write_cxx_function_shim), |
| 126 | Api::RustFunction(efn) => (efn, write_rust_function_decl), |
| 127 | _ => continue, |
| 128 | }; |
| 129 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 130 | write(out, efn, &opt.cxx_impl_annotations); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 131 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 132 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | for api in apis { |
| 136 | if let Api::RustFunction(efn) = api { |
| 137 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 138 | write_rust_function_shim(out, efn); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | out.next_section(); |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 143 | |
| Adrian Taylor | 565ddf0 | 2020-10-29 21:12:36 -0700 | [diff] [blame] | 144 | for (child_ns, child_ns_entries) in ns_entries.children() { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 145 | writeln!(out, "namespace {} {{", child_ns); |
| David Tolnay | ef0473a | 2020-10-31 22:44:52 -0700 | [diff] [blame] | 146 | gen_namespace_contents(out, child_ns_entries, opt); |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 147 | writeln!(out, "}} // namespace {}", child_ns); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 148 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| David Tolnay | 528200f | 2020-10-31 21:02:22 -0700 | [diff] [blame] | 151 | fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 152 | for ty in out.types { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 153 | match ty { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 154 | Type::Ident(ident) => match Atom::from(&ident.rust) { |
| David Tolnay | 89e386d | 2020-10-03 19:02:19 -0700 | [diff] [blame] | 155 | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32) |
| 156 | | Some(I64) => out.include.cstdint = true, |
| 157 | Some(Usize) => out.include.cstddef = true, |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 158 | Some(Isize) => { |
| 159 | out.include.basetsd = true; |
| 160 | out.builtin.rust_isize = true; |
| 161 | } |
| David Tolnay | 89e386d | 2020-10-03 19:02:19 -0700 | [diff] [blame] | 162 | Some(CxxString) => out.include.string = true, |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 163 | Some(RustString) => { |
| 164 | out.include.array = true; |
| 165 | out.include.cstdint = true; |
| 166 | out.include.string = true; |
| 167 | out.builtin.rust_string = true; |
| 168 | } |
| 169 | Some(Bool) | Some(F32) | Some(F64) | None => {} |
| David Tolnay | 89e386d | 2020-10-03 19:02:19 -0700 | [diff] [blame] | 170 | }, |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 171 | Type::RustBox(_) => { |
| David Tolnay | 0ecd05a | 2020-07-29 16:32:03 -0700 | [diff] [blame] | 172 | out.include.new = true; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 173 | out.include.type_traits = true; |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 174 | out.builtin.rust_box = true; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 175 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 176 | Type::RustVec(_) => { |
| David Tolnay | 9c6bf2d | 2020-04-24 15:27:07 -0700 | [diff] [blame] | 177 | out.include.array = true; |
| David Tolnay | 0ecd05a | 2020-07-29 16:32:03 -0700 | [diff] [blame] | 178 | out.include.new = true; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 179 | out.include.type_traits = true; |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 180 | out.builtin.panic = true; |
| 181 | out.builtin.rust_vec = true; |
| 182 | out.builtin.unsafe_bitcopy = true; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 183 | } |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 184 | Type::UniquePtr(_) => out.include.memory = true, |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 185 | Type::Str(_) => { |
| 186 | out.include.cstdint = true; |
| 187 | out.include.string = true; |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 188 | out.builtin.rust_str = true; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 189 | } |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 190 | Type::CxxVector(_) => out.include.vector = true, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 191 | Type::Fn(_) => { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 192 | out.builtin.rust_fn = true; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 193 | } |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 194 | Type::Slice(_) => { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 195 | out.builtin.rust_slice = true; |
| David Tolnay | 4770b47 | 2020-04-14 16:32:59 -0700 | [diff] [blame] | 196 | } |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 197 | Type::SliceRefU8(_) => { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 198 | out.include.cstdint = true; |
| David Tolnay | b9da146 | 2020-10-31 21:03:41 -0700 | [diff] [blame] | 199 | out.builtin.rust_slice = true; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 200 | } |
| 201 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 205 | for api in apis { |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 206 | match api { |
| 207 | Api::CxxFunction(efn) if !out.header => { |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 208 | if efn.throws { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 209 | out.builtin.trycatch = true; |
| David Tolnay | a4eb943 | 2020-10-31 20:32:19 -0700 | [diff] [blame] | 210 | } else if let Some(Type::Str(_)) = efn.ret { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 211 | out.builtin.rust_str_repr = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 212 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 213 | for arg in &efn.args { |
| David Tolnay | a4eb943 | 2020-10-31 20:32:19 -0700 | [diff] [blame] | 214 | match arg.ty { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 215 | Type::Str(_) => out.builtin.rust_str_new_unchecked = true, |
| 216 | Type::RustVec(_) => out.builtin.unsafe_bitcopy = true, |
| 217 | _ => out.builtin.unsafe_bitcopy |= arg.ty == RustString, |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 218 | } |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 221 | Api::RustFunction(efn) if !out.header => { |
| 222 | if efn.throws { |
| 223 | out.include.exception = true; |
| David Tolnay | c8870b8 | 2020-09-09 08:44:33 -0700 | [diff] [blame] | 224 | out.include.string = true; |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 225 | out.builtin.rust_str = true; |
| 226 | out.builtin.rust_error = true; |
| 227 | out.builtin.maybe_uninit = true; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 228 | } |
| 229 | for arg in &efn.args { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 230 | if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 231 | out.builtin.manually_drop = true; |
| David Tolnay | a4eb943 | 2020-10-31 20:32:19 -0700 | [diff] [blame] | 232 | } |
| 233 | if let Type::Str(_) = arg.ty { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 234 | out.builtin.rust_str_repr = true; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | if let Some(ret) = &efn.ret { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 238 | if out.types.needs_indirect_abi(ret) { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 239 | out.builtin.maybe_uninit = true; |
| David Tolnay | a4eb943 | 2020-10-31 20:32:19 -0700 | [diff] [blame] | 240 | } else if let Type::Str(_) = ret { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 241 | out.builtin.rust_str_new_unchecked = true; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 242 | } |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 245 | _ => {} |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
| David Tolnay | ec66d11 | 2020-10-31 21:00:26 -0700 | [diff] [blame] | 248 | } |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 249 | |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 250 | fn write_includes(out: &mut OutFile) { |
| 251 | let include = &mut out.include; |
| 252 | let out = &mut include.content; |
| 253 | |
| David Tolnay | 12af7e4 | 2020-10-31 21:09:23 -0700 | [diff] [blame] | 254 | for include in &include.custom { |
| 255 | match include.kind { |
| 256 | IncludeKind::Quoted => { |
| 257 | writeln!(out, "#include \"{}\"", include.path.escape_default()); |
| 258 | } |
| 259 | IncludeKind::Bracketed => { |
| 260 | writeln!(out, "#include <{}>", include.path); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if include.array { |
| 266 | writeln!(out, "#include <array>"); |
| 267 | } |
| 268 | if include.cstddef { |
| 269 | writeln!(out, "#include <cstddef>"); |
| 270 | } |
| 271 | if include.cstdint { |
| 272 | writeln!(out, "#include <cstdint>"); |
| 273 | } |
| 274 | if include.cstring { |
| 275 | writeln!(out, "#include <cstring>"); |
| 276 | } |
| 277 | if include.exception { |
| 278 | writeln!(out, "#include <exception>"); |
| 279 | } |
| 280 | if include.memory { |
| 281 | writeln!(out, "#include <memory>"); |
| 282 | } |
| 283 | if include.new { |
| 284 | writeln!(out, "#include <new>"); |
| 285 | } |
| 286 | if include.string { |
| 287 | writeln!(out, "#include <string>"); |
| 288 | } |
| 289 | if include.type_traits { |
| 290 | writeln!(out, "#include <type_traits>"); |
| 291 | } |
| 292 | if include.utility { |
| 293 | writeln!(out, "#include <utility>"); |
| 294 | } |
| 295 | if include.vector { |
| 296 | writeln!(out, "#include <vector>"); |
| 297 | } |
| 298 | if include.basetsd { |
| 299 | writeln!(out, "#if defined(_WIN32)"); |
| 300 | writeln!(out, "#include <basetsd.h>"); |
| 301 | writeln!(out, "#endif"); |
| 302 | } |
| 303 | } |
| 304 | |
| David Tolnay | ec66d11 | 2020-10-31 21:00:26 -0700 | [diff] [blame] | 305 | fn write_builtins(out: &mut OutFile) { |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 306 | if out.builtin == Default::default() { |
| 307 | return; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 310 | let include = &mut out.include; |
| 311 | let builtin = &mut out.builtin; |
| 312 | let out = &mut builtin.content; |
| David Tolnay | 16ab146 | 2020-09-02 15:10:09 -0700 | [diff] [blame] | 313 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 314 | out.begin_block("namespace rust"); |
| 315 | out.begin_block("inline namespace cxxbridge05"); |
| 316 | writeln!(out, "// #include \"rust/cxx.h\""); |
| 317 | |
| 318 | include::write(out, builtin.panic, "CXXBRIDGE05_PANIC"); |
| 319 | |
| 320 | if builtin.rust_string { |
| David Tolnay | d140274 | 2020-03-25 22:21:42 -0700 | [diff] [blame] | 321 | out.next_section(); |
| 322 | writeln!(out, "struct unsafe_bitcopy_t;"); |
| 323 | } |
| 324 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 325 | if builtin.rust_error { |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 326 | out.begin_block("namespace"); |
| 327 | writeln!(out, "template <typename T>"); |
| 328 | writeln!(out, "class impl;"); |
| 329 | out.end_block("namespace"); |
| 330 | } |
| 331 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 332 | include::write(out, builtin.rust_string, "CXXBRIDGE05_RUST_STRING"); |
| 333 | include::write(out, builtin.rust_str, "CXXBRIDGE05_RUST_STR"); |
| 334 | include::write(out, builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE"); |
| 335 | include::write(out, builtin.rust_box, "CXXBRIDGE05_RUST_BOX"); |
| 336 | include::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY"); |
| 337 | include::write(out, builtin.rust_vec, "CXXBRIDGE05_RUST_VEC"); |
| 338 | include::write(out, builtin.rust_fn, "CXXBRIDGE05_RUST_FN"); |
| 339 | include::write(out, builtin.rust_error, "CXXBRIDGE05_RUST_ERROR"); |
| 340 | include::write(out, builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE"); |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 341 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 342 | if builtin.manually_drop { |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 343 | out.next_section(); |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 344 | include.utility = true; |
| David Tolnay | f51447e | 2020-03-06 14:14:27 -0800 | [diff] [blame] | 345 | writeln!(out, "template <typename T>"); |
| 346 | writeln!(out, "union ManuallyDrop {{"); |
| 347 | writeln!(out, " T value;"); |
| 348 | writeln!( |
| 349 | out, |
| 350 | " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}", |
| 351 | ); |
| 352 | writeln!(out, " ~ManuallyDrop() {{}}"); |
| 353 | writeln!(out, "}};"); |
| 354 | } |
| 355 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 356 | if builtin.maybe_uninit { |
| David Tolnay | 09011c3 | 2020-03-06 14:40:28 -0800 | [diff] [blame] | 357 | out.next_section(); |
| 358 | writeln!(out, "template <typename T>"); |
| 359 | writeln!(out, "union MaybeUninit {{"); |
| 360 | writeln!(out, " T value;"); |
| 361 | writeln!(out, " MaybeUninit() {{}}"); |
| 362 | writeln!(out, " ~MaybeUninit() {{}}"); |
| 363 | writeln!(out, "}};"); |
| 364 | } |
| 365 | |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 366 | out.begin_block("namespace"); |
| 367 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 368 | if builtin.trycatch |
| 369 | || builtin.rust_error |
| 370 | || builtin.rust_str_new_unchecked |
| 371 | || builtin.rust_str_repr |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 372 | { |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 373 | out.begin_block("namespace repr"); |
| 374 | writeln!(out, "struct PtrLen final {{"); |
| David Tolnay | 54742b7 | 2020-10-31 19:43:13 -0700 | [diff] [blame] | 375 | writeln!(out, " const void *ptr;"); |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 376 | writeln!(out, " size_t len;"); |
| 377 | writeln!(out, "}};"); |
| 378 | out.end_block("namespace repr"); |
| 379 | } |
| 380 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 381 | if builtin.rust_str_new_unchecked || builtin.rust_str_repr { |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 382 | out.next_section(); |
| 383 | writeln!(out, "template <>"); |
| 384 | writeln!(out, "class impl<Str> final {{"); |
| 385 | writeln!(out, "public:"); |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 386 | if builtin.rust_str_new_unchecked { |
| David Tolnay | a4eb943 | 2020-10-31 20:32:19 -0700 | [diff] [blame] | 387 | writeln!( |
| 388 | out, |
| 389 | " static Str new_unchecked(repr::PtrLen repr) noexcept {{", |
| 390 | ); |
| 391 | writeln!(out, " Str str;"); |
| 392 | writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);"); |
| 393 | writeln!(out, " str.len = repr.len;"); |
| 394 | writeln!(out, " return str;"); |
| 395 | writeln!(out, " }}"); |
| 396 | } |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 397 | if builtin.rust_str_repr { |
| David Tolnay | a4eb943 | 2020-10-31 20:32:19 -0700 | [diff] [blame] | 398 | writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{"); |
| 399 | writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};"); |
| 400 | writeln!(out, " }}"); |
| 401 | } |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 402 | writeln!(out, "}};"); |
| 403 | } |
| 404 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 405 | if builtin.rust_error { |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 406 | out.next_section(); |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 407 | writeln!(out, "template <>"); |
| 408 | writeln!(out, "class impl<Error> final {{"); |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 409 | writeln!(out, "public:"); |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 410 | writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{"); |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 411 | writeln!(out, " Error error;"); |
| David Tolnay | 54742b7 | 2020-10-31 19:43:13 -0700 | [diff] [blame] | 412 | writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);"); |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 413 | writeln!(out, " error.len = repr.len;"); |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 414 | writeln!(out, " return error;"); |
| 415 | writeln!(out, " }}"); |
| 416 | writeln!(out, "}};"); |
| 417 | } |
| 418 | |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 419 | out.end_block("namespace"); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 420 | out.end_block("namespace cxxbridge05"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 421 | |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 422 | if builtin.trycatch { |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 423 | out.begin_block("namespace behavior"); |
| David Tolnay | fd68e56 | 2020-10-31 21:59:56 -0700 | [diff] [blame^] | 424 | include.exception = true; |
| 425 | include.type_traits = true; |
| 426 | include.utility = true; |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 427 | writeln!(out, "class missing {{}};"); |
| 428 | writeln!(out, "missing trycatch(...);"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 429 | writeln!(out); |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 430 | writeln!(out, "template <typename Try, typename Fail>"); |
| David Tolnay | cc44c7a | 2020-10-04 13:20:03 -0700 | [diff] [blame] | 431 | writeln!(out, "static typename ::std::enable_if<"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 432 | writeln!( |
| 433 | out, |
| David Tolnay | cc44c7a | 2020-10-04 13:20:03 -0700 | [diff] [blame] | 434 | " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),", |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 435 | ); |
| David Tolnay | 0472233 | 2020-03-18 11:31:54 -0700 | [diff] [blame] | 436 | writeln!(out, " missing>::value>::type"); |
| 437 | writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 438 | writeln!(out, " func();"); |
| 439 | writeln!(out, "}} catch (const ::std::exception &e) {{"); |
| 440 | writeln!(out, " fail(e.what());"); |
| 441 | writeln!(out, "}}"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 442 | out.end_block("namespace behavior"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 445 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 446 | } |
| 447 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 448 | fn write_struct(out: &mut OutFile, strct: &Struct) { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 449 | let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol()); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 450 | writeln!(out, "#ifndef {}", guard); |
| 451 | writeln!(out, "#define {}", guard); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 452 | for line in strct.doc.to_string().lines() { |
| 453 | writeln!(out, "//{}", line); |
| 454 | } |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 455 | writeln!(out, "struct {} final {{", strct.ident.cxx.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 456 | for field in &strct.fields { |
| 457 | write!(out, " "); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 458 | write_type_space(out, &field.ty); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 459 | writeln!(out, "{};", field.ident); |
| 460 | } |
| 461 | writeln!(out, "}};"); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 462 | writeln!(out, "#endif // {}", guard); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | fn write_struct_decl(out: &mut OutFile, ident: &Ident) { |
| 466 | writeln!(out, "struct {};", ident); |
| 467 | } |
| 468 | |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 469 | fn write_struct_using(out: &mut OutFile, ident: &CppName) { |
| 470 | writeln!( |
| 471 | out, |
| 472 | "using {} = {};", |
| 473 | ident.ident, |
| 474 | ident.to_fully_qualified() |
| 475 | ); |
| David Tolnay | 8861bee | 2020-01-20 18:39:24 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 478 | fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 479 | let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol()); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 480 | writeln!(out, "#ifndef {}", guard); |
| 481 | writeln!(out, "#define {}", guard); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 482 | for line in ety.doc.to_string().lines() { |
| 483 | writeln!(out, "//{}", line); |
| 484 | } |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 485 | writeln!(out, "struct {} final {{", ety.ident.cxx.ident); |
| 486 | writeln!(out, " {}() = delete;", ety.ident.cxx.ident); |
| 487 | writeln!( |
| 488 | out, |
| 489 | " {}(const {} &) = delete;", |
| 490 | ety.ident.cxx.ident, ety.ident.cxx.ident |
| 491 | ); |
| Joel Galenson | 968738f | 2020-04-15 14:19:33 -0700 | [diff] [blame] | 492 | for method in methods { |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 493 | write!(out, " "); |
| 494 | let sig = &method.sig; |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 495 | let local_name = method.ident.cxx.ident.to_string(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 496 | write_rust_function_shim_decl(out, &local_name, sig, false); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 497 | writeln!(out, ";"); |
| 498 | } |
| 499 | writeln!(out, "}};"); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 500 | writeln!(out, "#endif // {}", guard); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 503 | fn write_enum(out: &mut OutFile, enm: &Enum) { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 504 | let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol()); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 505 | writeln!(out, "#ifndef {}", guard); |
| 506 | writeln!(out, "#define {}", guard); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 507 | for line in enm.doc.to_string().lines() { |
| 508 | writeln!(out, "//{}", line); |
| 509 | } |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 510 | write!(out, "enum class {} : ", enm.ident.cxx.ident); |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 511 | write_atom(out, enm.repr); |
| 512 | writeln!(out, " {{"); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 513 | for variant in &enm.variants { |
| Joel Galenson | 8854773 | 2020-05-05 08:23:42 -0700 | [diff] [blame] | 514 | writeln!(out, " {} = {},", variant.ident, variant.discriminant); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 515 | } |
| 516 | writeln!(out, "}};"); |
| David Tolnay | a25ea9c | 2020-08-27 22:59:38 -0700 | [diff] [blame] | 517 | writeln!(out, "#endif // {}", guard); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| Joel Galenson | 905eb2e | 2020-05-04 14:58:14 -0700 | [diff] [blame] | 520 | fn check_enum(out: &mut OutFile, enm: &Enum) { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 521 | write!( |
| 522 | out, |
| 523 | "static_assert(sizeof({}) == sizeof(", |
| 524 | enm.ident.cxx.ident |
| 525 | ); |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 526 | write_atom(out, enm.repr); |
| 527 | writeln!(out, "), \"incorrect size\");"); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 528 | for variant in &enm.variants { |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 529 | write!(out, "static_assert(static_cast<"); |
| 530 | write_atom(out, enm.repr); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 531 | writeln!( |
| 532 | out, |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 533 | ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");", |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 534 | enm.ident.cxx.ident, variant.ident, variant.discriminant, |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 535 | ); |
| Joel Galenson | 0f654ff | 2020-05-04 20:04:21 -0700 | [diff] [blame] | 536 | } |
| Joel Galenson | 905eb2e | 2020-05-04 14:58:14 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 539 | fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) { |
| David Tolnay | fd0034e | 2020-10-04 13:15:34 -0700 | [diff] [blame] | 540 | // NOTE: The following two static assertions are just nice-to-have and not |
| 541 | // necessary for soundness. That's because triviality is always declared by |
| 542 | // the user in the form of an unsafe impl of cxx::ExternType: |
| 543 | // |
| 544 | // unsafe impl ExternType for MyType { |
| 545 | // type Id = cxx::type_id!("..."); |
| 546 | // type Kind = cxx::kind::Trivial; |
| 547 | // } |
| 548 | // |
| 549 | // Since the user went on the record with their unsafe impl to unsafely |
| 550 | // claim they KNOW that the type is trivial, it's fine for that to be on |
| 551 | // them if that were wrong. |
| 552 | // |
| 553 | // There may be a legitimate reason we'll want to remove these assertions |
| 554 | // for support of types that the programmer knows are Rust-movable despite |
| 555 | // not being recognized as such by the C++ type system due to a move |
| 556 | // constructor or destructor. |
| 557 | |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 558 | let id = &id.to_fully_qualified(); |
| David Tolnay | f57f756 | 2020-10-04 19:56:26 -0700 | [diff] [blame] | 559 | out.include.type_traits = true; |
| David Tolnay | 7426cc1 | 2020-10-03 19:04:04 -0700 | [diff] [blame] | 560 | writeln!(out, "static_assert("); |
| 561 | writeln!( |
| 562 | out, |
| David Tolnay | cc44c7a | 2020-10-04 13:20:03 -0700 | [diff] [blame] | 563 | " ::std::is_trivially_move_constructible<{}>::value,", |
| David Tolnay | 7426cc1 | 2020-10-03 19:04:04 -0700 | [diff] [blame] | 564 | id, |
| 565 | ); |
| 566 | writeln!( |
| 567 | out, |
| 568 | " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");", |
| 569 | id, |
| 570 | ); |
| 571 | writeln!(out, "static_assert("); |
| David Tolnay | cc44c7a | 2020-10-04 13:20:03 -0700 | [diff] [blame] | 572 | writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id); |
| David Tolnay | 7426cc1 | 2020-10-03 19:04:04 -0700 | [diff] [blame] | 573 | writeln!( |
| 574 | out, |
| 575 | " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");", |
| 576 | id, |
| 577 | ); |
| Adrian Taylor | 405e874 | 2020-09-25 14:01:25 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 580 | fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) { |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 581 | let mut has_cxx_throws = false; |
| 582 | for api in apis { |
| 583 | if let Api::CxxFunction(efn) = api { |
| 584 | if efn.throws { |
| 585 | has_cxx_throws = true; |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if has_cxx_throws { |
| 592 | out.next_section(); |
| David Tolnay | e68634c | 2020-03-18 12:03:40 -0700 | [diff] [blame] | 593 | writeln!( |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 594 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 595 | "const char *cxxbridge05$exception(const char *, size_t);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 596 | ); |
| 597 | } |
| 598 | } |
| 599 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 600 | fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) { |
| David Tolnay | cc1ae76 | 2020-10-31 15:53:50 -0700 | [diff] [blame] | 601 | if let Some(annotation) = impl_annotations { |
| 602 | write!(out, "{} ", annotation); |
| Adrian Taylor | 21f0ff0 | 2020-07-21 16:21:48 -0700 | [diff] [blame] | 603 | } |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 604 | if efn.throws { |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 605 | write!(out, "::rust::repr::PtrLen "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 606 | } else { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 607 | write_extern_return_type_space(out, &efn.ret); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 608 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 609 | let mangled = mangle::extern_fn(efn, out.types); |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 610 | write!(out, "{}(", mangled); |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 611 | if let Some(receiver) = &efn.receiver { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 612 | if receiver.mutability.is_none() { |
| 613 | write!(out, "const "); |
| 614 | } |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 615 | write!( |
| 616 | out, |
| 617 | "{} &self", |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 618 | out.types.resolve(&receiver.ty).to_fully_qualified() |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 619 | ); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 620 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 621 | for (i, arg) in efn.args.iter().enumerate() { |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 622 | if i > 0 || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 623 | write!(out, ", "); |
| 624 | } |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 625 | if arg.ty == RustString { |
| 626 | write!(out, "const "); |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 627 | } else if let Type::RustVec(_) = arg.ty { |
| 628 | write!(out, "const "); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 629 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 630 | write_extern_arg(out, arg); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 631 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 632 | let indirect_return = indirect_return(efn, out.types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 633 | if indirect_return { |
| myronahn | e3b78ea | 2020-05-23 01:08:13 +0700 | [diff] [blame] | 634 | if !efn.args.is_empty() || efn.receiver.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 635 | write!(out, ", "); |
| 636 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 637 | write_indirect_return_type_space(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 638 | write!(out, "*return$"); |
| 639 | } |
| 640 | writeln!(out, ") noexcept {{"); |
| 641 | write!(out, " "); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 642 | write_return_type(out, &efn.ret); |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 643 | match &efn.receiver { |
| David Tolnay | a4641c7 | 2020-09-08 14:05:53 -0700 | [diff] [blame] | 644 | None => write!(out, "(*{}$)(", efn.ident.rust), |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 645 | Some(receiver) => write!( |
| 646 | out, |
| 647 | "({}::*{}$)(", |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 648 | out.types.resolve(&receiver.ty).to_fully_qualified(), |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 649 | efn.ident.rust |
| 650 | ), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 651 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 652 | for (i, arg) in efn.args.iter().enumerate() { |
| 653 | if i > 0 { |
| 654 | write!(out, ", "); |
| 655 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 656 | write_type(out, &arg.ty); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 657 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 658 | write!(out, ")"); |
| David Tolnay | 4e7123f | 2020-04-19 21:11:37 -0700 | [diff] [blame] | 659 | if let Some(receiver) = &efn.receiver { |
| 660 | if receiver.mutability.is_none() { |
| 661 | write!(out, " const"); |
| 662 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 663 | } |
| 664 | write!(out, " = "); |
| 665 | match &efn.receiver { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 666 | None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()), |
| 667 | Some(receiver) => write!( |
| 668 | out, |
| 669 | "&{}::{}", |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 670 | out.types.resolve(&receiver.ty).to_fully_qualified(), |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 671 | efn.ident.cxx.ident |
| 672 | ), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 673 | } |
| 674 | writeln!(out, ";"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 675 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 676 | if efn.throws { |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 677 | writeln!(out, "::rust::repr::PtrLen throw$;"); |
| David Tolnay | 3e3e0af | 2020-03-17 22:42:49 -0700 | [diff] [blame] | 678 | writeln!(out, " ::rust::behavior::trycatch("); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 679 | writeln!(out, " [&] {{"); |
| 680 | write!(out, " "); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 681 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 682 | if indirect_return { |
| David Tolnay | 0ecd05a | 2020-07-29 16:32:03 -0700 | [diff] [blame] | 683 | out.include.new = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 684 | write!(out, "new (return$) "); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 685 | write_indirect_return_type(out, efn.ret.as_ref().unwrap()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 686 | write!(out, "("); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 687 | } else if efn.ret.is_some() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 688 | write!(out, "return "); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 689 | } |
| 690 | match &efn.ret { |
| 691 | Some(Type::Ref(_)) => write!(out, "&"), |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 692 | Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("), |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 693 | Some(Type::SliceRefU8(_)) if !indirect_return => { |
| 694 | write!(out, "::rust::Slice<uint8_t>::Repr(") |
| 695 | } |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 696 | _ => {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 697 | } |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 698 | match &efn.receiver { |
| David Tolnay | a4641c7 | 2020-09-08 14:05:53 -0700 | [diff] [blame] | 699 | None => write!(out, "{}$(", efn.ident.rust), |
| 700 | Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust), |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 701 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 702 | for (i, arg) in efn.args.iter().enumerate() { |
| 703 | if i > 0 { |
| 704 | write!(out, ", "); |
| 705 | } |
| 706 | if let Type::RustBox(_) = &arg.ty { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 707 | write_type(out, &arg.ty); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 708 | write!(out, "::from_raw({})", arg.ident); |
| 709 | } else if let Type::UniquePtr(_) = &arg.ty { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 710 | write_type(out, &arg.ty); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 711 | write!(out, "({})", arg.ident); |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 712 | } else if let Type::Str(_) = arg.ty { |
| 713 | write!( |
| 714 | out, |
| 715 | "::rust::impl<::rust::Str>::new_unchecked({})", |
| 716 | arg.ident, |
| 717 | ); |
| David Tolnay | a46a237 | 2020-03-06 10:03:48 -0800 | [diff] [blame] | 718 | } else if arg.ty == RustString { |
| David Tolnay | cc3767f | 2020-03-06 10:41:51 -0800 | [diff] [blame] | 719 | write!( |
| 720 | out, |
| 721 | "::rust::String(::rust::unsafe_bitcopy, *{})", |
| 722 | arg.ident, |
| 723 | ); |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 724 | } else if let Type::RustVec(_) = arg.ty { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 725 | write_type(out, &arg.ty); |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 726 | write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 727 | } else if out.types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 728 | out.include.utility = true; |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 729 | write!(out, "::std::move(*{})", arg.ident); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 730 | } else { |
| 731 | write!(out, "{}", arg.ident); |
| 732 | } |
| 733 | } |
| 734 | write!(out, ")"); |
| 735 | match &efn.ret { |
| 736 | Some(Type::RustBox(_)) => write!(out, ".into_raw()"), |
| 737 | Some(Type::UniquePtr(_)) => write!(out, ".release()"), |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 738 | Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 739 | _ => {} |
| 740 | } |
| 741 | if indirect_return { |
| 742 | write!(out, ")"); |
| 743 | } |
| 744 | writeln!(out, ";"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 745 | if efn.throws { |
| 746 | out.include.cstring = true; |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 747 | writeln!(out, " throw$.ptr = nullptr;"); |
| 748 | writeln!(out, " }},"); |
| David Tolnay | 82c1617 | 2020-03-17 22:54:12 -0700 | [diff] [blame] | 749 | writeln!(out, " [&](const char *catch$) noexcept {{"); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 750 | writeln!(out, " throw$.len = ::std::strlen(catch$);"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 751 | writeln!( |
| 752 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 753 | " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);", |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 754 | ); |
| David Tolnay | 5d12144 | 2020-03-17 22:14:40 -0700 | [diff] [blame] | 755 | writeln!(out, " }});"); |
| David Tolnay | ebef4a2 | 2020-03-17 15:33:47 -0700 | [diff] [blame] | 756 | writeln!(out, " return throw$;"); |
| 757 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 758 | writeln!(out, "}}"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 759 | for arg in &efn.args { |
| 760 | if let Type::Fn(f) = &arg.ty { |
| 761 | let var = &arg.ident; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 762 | write_function_pointer_trampoline(out, efn, var, f); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | fn write_function_pointer_trampoline( |
| 768 | out: &mut OutFile, |
| 769 | efn: &ExternFn, |
| 770 | var: &Ident, |
| 771 | f: &Signature, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 772 | ) { |
| 773 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 774 | let r_trampoline = mangle::r_trampoline(efn, var, out.types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 775 | let indirect_call = true; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 776 | write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 777 | |
| 778 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 779 | let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string(); |
| 780 | 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] | 781 | } |
| 782 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 783 | fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) { |
| 784 | let link_name = mangle::extern_fn(efn, out.types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 785 | let indirect_call = false; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 786 | write_rust_function_decl_impl(out, &link_name, efn, indirect_call); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | fn write_rust_function_decl_impl( |
| 790 | out: &mut OutFile, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 791 | link_name: &Symbol, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 792 | sig: &Signature, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 793 | indirect_call: bool, |
| 794 | ) { |
| 795 | if sig.throws { |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 796 | write!(out, "::rust::repr::PtrLen "); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 797 | } else { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 798 | write_extern_return_type_space(out, &sig.ret); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 799 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 800 | write!(out, "{}(", link_name); |
| 801 | let mut needs_comma = false; |
| David Tolnay | e439c77 | 2020-04-20 00:23:55 -0700 | [diff] [blame] | 802 | if let Some(receiver) = &sig.receiver { |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 803 | if receiver.mutability.is_none() { |
| 804 | write!(out, "const "); |
| 805 | } |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 806 | write!( |
| 807 | out, |
| 808 | "{} &self", |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 809 | out.types.resolve(&receiver.ty).to_fully_qualified() |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 810 | ); |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 811 | needs_comma = true; |
| 812 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 813 | for arg in &sig.args { |
| 814 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 815 | write!(out, ", "); |
| 816 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 817 | write_extern_arg(out, arg); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 818 | needs_comma = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 819 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 820 | if indirect_return(sig, out.types) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 821 | if needs_comma { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 822 | write!(out, ", "); |
| 823 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 824 | write_return_type(out, &sig.ret); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 825 | write!(out, "*return$"); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 826 | needs_comma = true; |
| 827 | } |
| 828 | if indirect_call { |
| 829 | if needs_comma { |
| 830 | write!(out, ", "); |
| 831 | } |
| 832 | write!(out, "void *"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 833 | } |
| 834 | writeln!(out, ") noexcept;"); |
| 835 | } |
| 836 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 837 | fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 838 | for line in efn.doc.to_string().lines() { |
| 839 | writeln!(out, "//{}", line); |
| 840 | } |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 841 | let local_name = match &efn.sig.receiver { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 842 | None => efn.ident.cxx.ident.to_string(), |
| 843 | Some(receiver) => format!( |
| 844 | "{}::{}", |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 845 | out.types.resolve(&receiver.ty).ident, |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 846 | efn.ident.cxx.ident |
| 847 | ), |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 848 | }; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 849 | let invoke = mangle::extern_fn(efn, out.types); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 850 | let indirect_call = false; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 851 | write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 854 | fn write_rust_function_shim_decl( |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 855 | out: &mut OutFile, |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 856 | local_name: &str, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 857 | sig: &Signature, |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 858 | indirect_call: bool, |
| 859 | ) { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 860 | write_return_type(out, &sig.ret); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 861 | write!(out, "{}(", local_name); |
| 862 | for (i, arg) in sig.args.iter().enumerate() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 863 | if i > 0 { |
| 864 | write!(out, ", "); |
| 865 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 866 | write_type_space(out, &arg.ty); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 867 | write!(out, "{}", arg.ident); |
| 868 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 869 | if indirect_call { |
| 870 | if !sig.args.is_empty() { |
| 871 | write!(out, ", "); |
| 872 | } |
| 873 | write!(out, "void *extern$"); |
| 874 | } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 875 | write!(out, ")"); |
| David Tolnay | 8671061 | 2020-04-20 00:30:32 -0700 | [diff] [blame] | 876 | if let Some(receiver) = &sig.receiver { |
| 877 | if receiver.mutability.is_none() { |
| 878 | write!(out, " const"); |
| 879 | } |
| 880 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 881 | if !sig.throws { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 882 | write!(out, " noexcept"); |
| 883 | } |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | fn write_rust_function_shim_impl( |
| 887 | out: &mut OutFile, |
| David Tolnay | a73853b | 2020-04-20 01:19:56 -0700 | [diff] [blame] | 888 | local_name: &str, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 889 | sig: &Signature, |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 890 | invoke: &Symbol, |
| Joel Galenson | c1c4e7a | 2020-04-15 10:21:00 -0700 | [diff] [blame] | 891 | indirect_call: bool, |
| 892 | ) { |
| 893 | if out.header && sig.receiver.is_some() { |
| 894 | // We've already defined this inside the struct. |
| 895 | return; |
| 896 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 897 | write_rust_function_shim_decl(out, local_name, sig, indirect_call); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 898 | if out.header { |
| 899 | writeln!(out, ";"); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 900 | return; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 901 | } |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 902 | writeln!(out, " {{"); |
| 903 | for arg in &sig.args { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 904 | if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 905 | out.include.utility = true; |
| 906 | write!(out, " ::rust::ManuallyDrop<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 907 | write_type(out, &arg.ty); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 908 | writeln!(out, "> {}$(::std::move({0}));", arg.ident); |
| 909 | } |
| 910 | } |
| 911 | write!(out, " "); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 912 | let indirect_return = indirect_return(sig, out.types); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 913 | if indirect_return { |
| 914 | write!(out, "::rust::MaybeUninit<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 915 | write_type(out, sig.ret.as_ref().unwrap()); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 916 | writeln!(out, "> return$;"); |
| 917 | write!(out, " "); |
| 918 | } else if let Some(ret) = &sig.ret { |
| 919 | write!(out, "return "); |
| 920 | match ret { |
| 921 | Type::RustBox(_) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 922 | write_type(out, ret); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 923 | write!(out, "::from_raw("); |
| 924 | } |
| 925 | Type::UniquePtr(_) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 926 | write_type(out, ret); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 927 | write!(out, "("); |
| 928 | } |
| 929 | Type::Ref(_) => write!(out, "*"), |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 930 | Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("), |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 931 | _ => {} |
| 932 | } |
| 933 | } |
| 934 | if sig.throws { |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 935 | write!(out, "::rust::repr::PtrLen error$ = "); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 936 | } |
| 937 | write!(out, "{}(", invoke); |
| 938 | if sig.receiver.is_some() { |
| 939 | write!(out, "*this"); |
| 940 | } |
| 941 | for (i, arg) in sig.args.iter().enumerate() { |
| 942 | if i > 0 || sig.receiver.is_some() { |
| 943 | write!(out, ", "); |
| 944 | } |
| 945 | match &arg.ty { |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 946 | Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("), |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 947 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 948 | ty if out.types.needs_indirect_abi(ty) => write!(out, "&"), |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 949 | _ => {} |
| 950 | } |
| 951 | write!(out, "{}", arg.ident); |
| 952 | match &arg.ty { |
| 953 | Type::RustBox(_) => write!(out, ".into_raw()"), |
| 954 | Type::UniquePtr(_) => write!(out, ".release()"), |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 955 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 956 | 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] | 957 | _ => {} |
| 958 | } |
| 959 | } |
| 960 | if indirect_return { |
| 961 | if !sig.args.is_empty() { |
| 962 | write!(out, ", "); |
| 963 | } |
| 964 | write!(out, "&return$.value"); |
| 965 | } |
| 966 | if indirect_call { |
| 967 | if !sig.args.is_empty() || indirect_return { |
| 968 | write!(out, ", "); |
| 969 | } |
| 970 | write!(out, "extern$"); |
| 971 | } |
| 972 | write!(out, ")"); |
| David Tolnay | 22602b4 | 2020-09-21 18:04:05 -0400 | [diff] [blame] | 973 | if !indirect_return { |
| 974 | if let Some(ret) = &sig.ret { |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 975 | if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret { |
| David Tolnay | 22602b4 | 2020-09-21 18:04:05 -0400 | [diff] [blame] | 976 | write!(out, ")"); |
| 977 | } |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | writeln!(out, ";"); |
| 981 | if sig.throws { |
| David Tolnay | d68dfa8 | 2020-10-31 16:01:24 -0700 | [diff] [blame] | 982 | writeln!(out, " if (error$.ptr) {{"); |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 983 | writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);"); |
| David Tolnay | 439cde2 | 2020-04-20 00:46:25 -0700 | [diff] [blame] | 984 | writeln!(out, " }}"); |
| 985 | } |
| 986 | if indirect_return { |
| 987 | out.include.utility = true; |
| 988 | writeln!(out, " return ::std::move(return$.value);"); |
| 989 | } |
| 990 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 991 | } |
| 992 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 993 | fn write_return_type(out: &mut OutFile, ty: &Option<Type>) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 994 | match ty { |
| 995 | None => write!(out, "void "), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 996 | Some(ty) => write_type_space(out, ty), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1000 | fn indirect_return(sig: &Signature, types: &Types) -> bool { |
| 1001 | sig.ret |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 1002 | .as_ref() |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1003 | .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret)) |
| David Tolnay | 277e3cc | 2020-03-17 00:11:01 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1006 | fn write_indirect_return_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1007 | match ty { |
| 1008 | Type::RustBox(ty) | Type::UniquePtr(ty) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1009 | write_type_space(out, &ty.inner); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1010 | write!(out, "*"); |
| 1011 | } |
| 1012 | Type::Ref(ty) => { |
| 1013 | if ty.mutability.is_none() { |
| 1014 | write!(out, "const "); |
| 1015 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1016 | write_type(out, &ty.inner); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1017 | write!(out, " *"); |
| 1018 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 1019 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1020 | _ => write_type(out, ty), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1024 | fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) { |
| 1025 | write_indirect_return_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1026 | match ty { |
| 1027 | Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 1028 | Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "), |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1029 | _ => write_space_after_type(out, ty), |
| 1030 | } |
| 1031 | } |
| 1032 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1033 | fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1034 | match ty { |
| 1035 | Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1036 | write_type_space(out, &ty.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1037 | write!(out, "*"); |
| 1038 | } |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 1039 | Some(Type::Ref(ty)) => { |
| 1040 | if ty.mutability.is_none() { |
| 1041 | write!(out, "const "); |
| 1042 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1043 | write_type(out, &ty.inner); |
| David Tolnay | 4a44122 | 2020-01-25 16:24:27 -0800 | [diff] [blame] | 1044 | write!(out, " *"); |
| 1045 | } |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 1046 | Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 1047 | Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1048 | Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "), |
| 1049 | _ => write_return_type(out, ty), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1053 | fn write_extern_arg(out: &mut OutFile, arg: &Var) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1054 | match &arg.ty { |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 1055 | Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1056 | write_type_space(out, &ty.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1057 | write!(out, "*"); |
| 1058 | } |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 1059 | Type::Str(_) => write!(out, "::rust::repr::PtrLen "), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 1060 | Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1061 | _ => write_type_space(out, &arg.ty), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1062 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1063 | if out.types.needs_indirect_abi(&arg.ty) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1064 | write!(out, "*"); |
| 1065 | } |
| 1066 | write!(out, "{}", arg.ident); |
| 1067 | } |
| 1068 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1069 | fn write_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1070 | match ty { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1071 | Type::Ident(ident) => match Atom::from(&ident.rust) { |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 1072 | Some(atom) => write_atom(out, atom), |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1073 | None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1074 | }, |
| 1075 | Type::RustBox(ty) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 1076 | write!(out, "::rust::Box<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1077 | write_type(out, &ty.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1078 | write!(out, ">"); |
| 1079 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1080 | Type::RustVec(ty) => { |
| 1081 | write!(out, "::rust::Vec<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1082 | write_type(out, &ty.inner); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1083 | write!(out, ">"); |
| 1084 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1085 | Type::UniquePtr(ptr) => { |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1086 | write!(out, "::std::unique_ptr<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1087 | write_type(out, &ptr.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1088 | write!(out, ">"); |
| 1089 | } |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 1090 | Type::CxxVector(ty) => { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1091 | write!(out, "::std::vector<"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1092 | write_type(out, &ty.inner); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1093 | write!(out, ">"); |
| 1094 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1095 | Type::Ref(r) => { |
| 1096 | if r.mutability.is_none() { |
| 1097 | write!(out, "const "); |
| 1098 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1099 | write_type(out, &r.inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1100 | write!(out, " &"); |
| 1101 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 1102 | Type::Slice(_) => { |
| 1103 | // For now, only U8 slices are supported, which are covered separately below |
| 1104 | unreachable!() |
| 1105 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1106 | Type::Str(_) => { |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 1107 | write!(out, "::rust::Str"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1108 | } |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 1109 | Type::SliceRefU8(_) => { |
| 1110 | write!(out, "::rust::Slice<uint8_t>"); |
| 1111 | } |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1112 | Type::Fn(f) => { |
| 1113 | write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" }); |
| 1114 | match &f.ret { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1115 | Some(ret) => write_type(out, ret), |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1116 | None => write!(out, "void"), |
| 1117 | } |
| 1118 | write!(out, "("); |
| 1119 | for (i, arg) in f.args.iter().enumerate() { |
| 1120 | if i > 0 { |
| 1121 | write!(out, ", "); |
| 1122 | } |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1123 | write_type(out, &arg.ty); |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 1124 | } |
| 1125 | write!(out, ")>"); |
| 1126 | } |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 1127 | Type::Void(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
| David Tolnay | f6a89f2 | 2020-05-10 23:39:27 -0700 | [diff] [blame] | 1131 | fn write_atom(out: &mut OutFile, atom: Atom) { |
| 1132 | match atom { |
| 1133 | Bool => write!(out, "bool"), |
| 1134 | U8 => write!(out, "uint8_t"), |
| 1135 | U16 => write!(out, "uint16_t"), |
| 1136 | U32 => write!(out, "uint32_t"), |
| 1137 | U64 => write!(out, "uint64_t"), |
| 1138 | Usize => write!(out, "size_t"), |
| 1139 | I8 => write!(out, "int8_t"), |
| 1140 | I16 => write!(out, "int16_t"), |
| 1141 | I32 => write!(out, "int32_t"), |
| 1142 | I64 => write!(out, "int64_t"), |
| 1143 | Isize => write!(out, "::rust::isize"), |
| 1144 | F32 => write!(out, "float"), |
| 1145 | F64 => write!(out, "double"), |
| 1146 | CxxString => write!(out, "::std::string"), |
| 1147 | RustString => write!(out, "::rust::String"), |
| 1148 | } |
| 1149 | } |
| 1150 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1151 | fn write_type_space(out: &mut OutFile, ty: &Type) { |
| 1152 | write_type(out, ty); |
| David Tolnay | 9964262 | 2020-03-25 13:07:35 -0700 | [diff] [blame] | 1153 | write_space_after_type(out, ty); |
| 1154 | } |
| 1155 | |
| 1156 | fn write_space_after_type(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1157 | match ty { |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 1158 | Type::Ident(_) |
| 1159 | | Type::RustBox(_) |
| 1160 | | Type::UniquePtr(_) |
| 1161 | | Type::Str(_) |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 1162 | | Type::CxxVector(_) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1163 | | Type::RustVec(_) |
| David Tolnay | eb952ba | 2020-04-14 15:02:24 -0700 | [diff] [blame] | 1164 | | Type::SliceRefU8(_) |
| 1165 | | Type::Fn(_) => write!(out, " "), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1166 | Type::Ref(_) => {} |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 1167 | Type::Void(_) | Type::Slice(_) => unreachable!(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
| David Tolnay | cd08c44 | 2020-04-25 10:16:33 -0700 | [diff] [blame] | 1171 | // Only called for legal referent types of unique_ptr and element types of |
| 1172 | // std::vector and Vec. |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1173 | fn to_typename(ty: &Type, types: &Types) -> String { |
| David Tolnay | 2eca4a0 | 2020-04-24 19:50:51 -0700 | [diff] [blame] | 1174 | match ty { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1175 | Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(), |
| 1176 | Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)), |
| David Tolnay | cd08c44 | 2020-04-25 10:16:33 -0700 | [diff] [blame] | 1177 | _ => unreachable!(), |
| David Tolnay | 2eca4a0 | 2020-04-24 19:50:51 -0700 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| David Tolnay | acdf20a | 2020-04-25 12:40:53 -0700 | [diff] [blame] | 1181 | // Only called for legal referent types of unique_ptr and element types of |
| 1182 | // std::vector and Vec. |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1183 | fn to_mangled(ty: &Type, types: &Types) -> Symbol { |
| David Tolnay | bae50ef | 2020-04-25 12:38:41 -0700 | [diff] [blame] | 1184 | match ty { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1185 | Type::Ident(ident) => ident.to_symbol(types), |
| 1186 | Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"), |
| David Tolnay | acdf20a | 2020-04-25 12:40:53 -0700 | [diff] [blame] | 1187 | _ => unreachable!(), |
| David Tolnay | bae50ef | 2020-04-25 12:38:41 -0700 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1191 | fn write_generic_instantiations(out: &mut OutFile) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1192 | out.begin_block("extern \"C\""); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1193 | for ty in out.types { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1194 | if let Type::RustBox(ty) = ty { |
| 1195 | if let Type::Ident(inner) = &ty.inner { |
| 1196 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1197 | write_rust_box_extern(out, &out.types.resolve(&inner)); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1198 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1199 | } else if let Type::RustVec(ty) = ty { |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1200 | if let Type::Ident(inner) = &ty.inner { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1201 | if Atom::from(&inner.rust).is_none() { |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1202 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1203 | write_rust_vec_extern(out, inner); |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1204 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1205 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1206 | } else if let Type::UniquePtr(ptr) = ty { |
| 1207 | if let Type::Ident(inner) = &ptr.inner { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1208 | if Atom::from(&inner.rust).is_none() |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1209 | && (!out.types.aliases.contains_key(&inner.rust) |
| 1210 | || out.types.explicit_impls.contains(ty)) |
| David Tolnay | 7e69f89 | 2020-10-03 22:20:22 -0700 | [diff] [blame] | 1211 | { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1212 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1213 | write_unique_ptr(out, inner); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1214 | } |
| 1215 | } |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 1216 | } else if let Type::CxxVector(ptr) = ty { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1217 | if let Type::Ident(inner) = &ptr.inner { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1218 | if Atom::from(&inner.rust).is_none() |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1219 | && (!out.types.aliases.contains_key(&inner.rust) |
| 1220 | || out.types.explicit_impls.contains(ty)) |
| David Tolnay | 7e69f89 | 2020-10-03 22:20:22 -0700 | [diff] [blame] | 1221 | { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1222 | out.next_section(); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1223 | write_cxx_vector(out, ty, inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 1228 | out.end_block("extern \"C\""); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1229 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 1230 | out.begin_block("namespace rust"); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1231 | out.begin_block("inline namespace cxxbridge05"); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1232 | for ty in out.types { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1233 | if let Type::RustBox(ty) = ty { |
| 1234 | if let Type::Ident(inner) = &ty.inner { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1235 | write_rust_box_impl(out, &out.types.resolve(&inner)); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1236 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1237 | } else if let Type::RustVec(ty) = ty { |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1238 | if let Type::Ident(inner) = &ty.inner { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1239 | if Atom::from(&inner.rust).is_none() { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1240 | write_rust_vec_impl(out, inner); |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1241 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1242 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1243 | } |
| 1244 | } |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1245 | out.end_block("namespace cxxbridge05"); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 1246 | out.end_block("namespace rust"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1247 | } |
| 1248 | |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1249 | fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) { |
| 1250 | let inner = ident.to_fully_qualified(); |
| 1251 | let instance = ident.to_symbol(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1252 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1253 | writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance); |
| 1254 | writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1255 | writeln!( |
| 1256 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1257 | "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1258 | instance, inner, |
| 1259 | ); |
| 1260 | writeln!( |
| 1261 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1262 | "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1263 | instance, inner, |
| 1264 | ); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1265 | writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1266 | } |
| 1267 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1268 | fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) { |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1269 | let element = Type::Ident(element.clone()); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1270 | let inner = to_typename(&element, out.types); |
| 1271 | let instance = to_mangled(&element, out.types); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1272 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1273 | writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance); |
| 1274 | writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1275 | writeln!( |
| 1276 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1277 | "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;", |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1278 | instance, inner, |
| 1279 | ); |
| 1280 | writeln!( |
| 1281 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1282 | "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1283 | instance, inner, |
| 1284 | ); |
| 1285 | writeln!( |
| 1286 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1287 | "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1288 | instance, inner, |
| 1289 | ); |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1290 | writeln!( |
| 1291 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1292 | "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;", |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1293 | inner, instance, |
| 1294 | ); |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1295 | writeln!( |
| 1296 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1297 | "size_t cxxbridge05$rust_vec${}$stride() noexcept;", |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1298 | instance, |
| 1299 | ); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1300 | writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 1303 | fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) { |
| 1304 | let inner = ident.to_fully_qualified(); |
| 1305 | let instance = ident.to_symbol(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1306 | |
| 1307 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 1308 | writeln!(out, "void Box<{}>::uninit() noexcept {{", inner); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1309 | writeln!(out, " cxxbridge05$box${}$uninit(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1310 | writeln!(out, "}}"); |
| 1311 | |
| 1312 | writeln!(out, "template <>"); |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 1313 | writeln!(out, "void Box<{}>::drop() noexcept {{", inner); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1314 | writeln!(out, " cxxbridge05$box${}$drop(this);", instance); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1315 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1316 | } |
| 1317 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1318 | fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) { |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 1319 | let element = Type::Ident(element.clone()); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1320 | let inner = to_typename(&element, out.types); |
| 1321 | let instance = to_mangled(&element, out.types); |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 1322 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1323 | writeln!(out, "template <>"); |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1324 | writeln!(out, "Vec<{}>::Vec() noexcept {{", inner); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1325 | writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance); |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 1326 | writeln!(out, "}}"); |
| 1327 | |
| 1328 | writeln!(out, "template <>"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1329 | writeln!(out, "void Vec<{}>::drop() noexcept {{", inner); |
| 1330 | writeln!( |
| 1331 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1332 | " return cxxbridge05$rust_vec${}$drop(this);", |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 1333 | instance, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1334 | ); |
| 1335 | writeln!(out, "}}"); |
| 1336 | |
| 1337 | writeln!(out, "template <>"); |
| 1338 | writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1339 | writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1340 | writeln!(out, "}}"); |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1341 | |
| 1342 | writeln!(out, "template <>"); |
| 1343 | writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner); |
| 1344 | writeln!( |
| 1345 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1346 | " return cxxbridge05$rust_vec${}$data(this);", |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 1347 | instance, |
| 1348 | ); |
| 1349 | writeln!(out, "}}"); |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1350 | |
| 1351 | writeln!(out, "template <>"); |
| 1352 | writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1353 | writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance); |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 1354 | writeln!(out, "}}"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1357 | fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) { |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1358 | let ty = Type::Ident(ident.clone()); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1359 | let instance = to_mangled(&ty, out.types); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1360 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1361 | writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance); |
| 1362 | writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1363 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1364 | write_unique_ptr_common(out, &ty); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1365 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1366 | writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | // Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>. |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1370 | fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) { |
| David Tolnay | 0ecd05a | 2020-07-29 16:32:03 -0700 | [diff] [blame] | 1371 | out.include.new = true; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1372 | out.include.utility = true; |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1373 | let inner = to_typename(ty, out.types); |
| 1374 | let instance = to_mangled(ty, out.types); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1375 | |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1376 | let can_construct_from_value = match ty { |
| David Tolnay | ca0f9da | 2020-10-16 13:16:17 -0700 | [diff] [blame] | 1377 | // Some aliases are to opaque types; some are to trivial types. We can't |
| 1378 | // know at code generation time, so we generate both C++ and Rust side |
| 1379 | // bindings for a "new" method anyway. But the Rust code can't be called |
| 1380 | // for Opaque types because the 'new' method is not implemented. |
| 1381 | Type::Ident(ident) => { |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1382 | out.types.structs.contains_key(&ident.rust) |
| 1383 | || out.types.aliases.contains_key(&ident.rust) |
| David Tolnay | ca0f9da | 2020-10-16 13:16:17 -0700 | [diff] [blame] | 1384 | } |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1385 | _ => false, |
| 1386 | }; |
| 1387 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1388 | writeln!( |
| 1389 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1390 | "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1391 | inner, |
| 1392 | ); |
| 1393 | writeln!( |
| 1394 | out, |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1395 | "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1396 | inner, |
| 1397 | ); |
| 1398 | writeln!( |
| 1399 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1400 | "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1401 | instance, inner, |
| 1402 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1403 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1404 | writeln!(out, "}}"); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1405 | if can_construct_from_value { |
| 1406 | writeln!( |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1407 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1408 | "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{", |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1409 | instance, inner, inner, |
| 1410 | ); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1411 | writeln!( |
| 1412 | out, |
| 1413 | " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));", |
| 1414 | inner, inner, |
| 1415 | ); |
| 1416 | writeln!(out, "}}"); |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 1417 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1418 | writeln!( |
| 1419 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1420 | "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1421 | instance, inner, inner, |
| 1422 | ); |
| David Tolnay | 7e219b8 | 2020-03-01 13:14:51 -0800 | [diff] [blame] | 1423 | writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1424 | writeln!(out, "}}"); |
| 1425 | writeln!( |
| 1426 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1427 | "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1428 | inner, instance, inner, |
| 1429 | ); |
| 1430 | writeln!(out, " return ptr.get();"); |
| 1431 | writeln!(out, "}}"); |
| 1432 | writeln!( |
| 1433 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1434 | "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1435 | inner, instance, inner, |
| 1436 | ); |
| 1437 | writeln!(out, " return ptr.release();"); |
| 1438 | writeln!(out, "}}"); |
| 1439 | writeln!( |
| 1440 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1441 | "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{", |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1442 | instance, inner, |
| 1443 | ); |
| 1444 | writeln!(out, " ptr->~unique_ptr();"); |
| 1445 | writeln!(out, "}}"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1446 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1447 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1448 | fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) { |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1449 | let element = Type::Ident(element.clone()); |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1450 | let inner = to_typename(&element, out.types); |
| 1451 | let instance = to_mangled(&element, out.types); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1452 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1453 | writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance); |
| 1454 | writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1455 | writeln!( |
| 1456 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1457 | "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1458 | instance, inner, |
| 1459 | ); |
| 1460 | writeln!(out, " return s.size();"); |
| 1461 | writeln!(out, "}}"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1462 | writeln!( |
| 1463 | out, |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1464 | "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{", |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1465 | inner, instance, inner, |
| 1466 | ); |
| David Tolnay | b3fcf7b | 2020-04-30 22:58:28 -0700 | [diff] [blame] | 1467 | writeln!(out, " return &s[pos];"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1468 | writeln!(out, "}}"); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1469 | |
| David Tolnay | a7c2ea1 | 2020-10-30 21:32:53 -0700 | [diff] [blame] | 1470 | write_unique_ptr_common(out, vector_ty); |
| David Tolnay | 63da4d3 | 2020-04-25 09:41:12 -0700 | [diff] [blame] | 1471 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 1472 | writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1473 | } |