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