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