blob: a354ae468286d2ff903715af5acca2c6aed3f1c1 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
David Tolnayb0cd3272020-10-27 21:32:54 -07006use crate::syntax::{
7 mangle, Api, Enum, ExternFn, ExternType, IncludeKind, Signature, Struct, Type, Types, Var,
8};
David Tolnay7db73692019-10-20 14:51:12 -04009use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -070010use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040011
David Tolnay33d30292020-03-18 18:02:02 -070012pub(super) fn gen(
David Tolnay2ec14632020-05-04 00:47:10 -070013 namespace: &Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070014 apis: &[Api],
15 types: &Types,
David Tolnaya5cca312020-08-29 23:40:04 -070016 opt: &Opt,
David Tolnay33d30292020-03-18 18:02:02 -070017 header: bool,
18) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040019 let mut out_file = OutFile::new(namespace.clone(), header);
20 let out = &mut out_file;
21
David Tolnay54702b92020-07-31 11:50:09 -070022 if header {
23 writeln!(out.front, "#pragma once");
24 }
25
David Tolnaya5cca312020-08-29 23:40:04 -070026 out.include.extend(opt.include.clone());
David Tolnay7db73692019-10-20 14:51:12 -040027 for api in apis {
28 if let Api::Include(include) = api {
David Tolnayb0cd3272020-10-27 21:32:54 -070029 match include.kind {
30 IncludeKind::Quoted => out.include.insert(&include.path),
31 IncludeKind::Bracketed => out.include.insert(format!("<{}>", include.path)),
32 }
David Tolnay7db73692019-10-20 14:51:12 -040033 }
34 }
35
36 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080037 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040038
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070040 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040041 writeln!(out, "namespace {} {{", name);
42 }
43
David Tolnay7db73692019-10-20 14:51:12 -040044 out.next_section();
45 for api in apis {
46 match api {
47 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080048 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
49 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070050 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040051 }
52 }
53
David Tolnayf94bef12020-04-17 14:46:42 -070054 let mut methods_for_type = HashMap::new();
55 for api in apis {
56 if let Api::RustFunction(efn) = api {
57 if let Some(receiver) = &efn.sig.receiver {
58 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070059 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070060 .or_insert_with(Vec::new)
61 .push(efn);
62 }
63 }
64 }
Joel Galenson968738f2020-04-15 14:19:33 -070065
David Tolnay7db73692019-10-20 14:51:12 -040066 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070067 match api {
68 Api::Struct(strct) => {
69 out.next_section();
David Tolnaya593d6e2020-08-29 19:48:08 -070070 if !types.cxx.contains(&strct.ident) {
71 write_struct(out, strct);
72 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070073 }
Joel Galensonc03402a2020-04-23 17:31:09 -070074 Api::Enum(enm) => {
75 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070076 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070077 check_enum(out, enm);
78 } else {
79 write_enum(out, enm);
80 }
Joel Galensonc03402a2020-04-23 17:31:09 -070081 }
David Tolnayc1fe0052020-04-17 15:15:06 -070082 Api::RustType(ety) => {
83 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070084 out.next_section();
85 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070086 }
David Tolnayc1fe0052020-04-17 15:15:06 -070087 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070088 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040089 }
90 }
91
David Tolnayfabca772020-10-03 21:25:41 -070092 out.next_section();
93 for api in apis {
94 if let Api::TypeAlias(ety) = api {
95 if types.required_trivial.contains_key(&ety.ident) {
96 check_trivial_extern_type(out, &ety.ident)
97 }
98 }
99 }
100
David Tolnay7db73692019-10-20 14:51:12 -0400101 if !header {
102 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700103 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400104 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700105 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400106 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
107 Api::RustFunction(efn) => (efn, write_rust_function_decl),
108 _ => continue,
109 };
110 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700111 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400112 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800113 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400114 }
115
116 for api in apis {
117 if let Api::RustFunction(efn) = api {
118 out.next_section();
119 write_rust_function_shim(out, efn, types);
120 }
121 }
122
123 out.next_section();
124 for name in namespace.iter().rev() {
125 writeln!(out, "}} // namespace {}", name);
126 }
127
128 if !header {
129 out.next_section();
130 write_generic_instantiations(out, types);
131 }
132
David Tolnay54702b92020-07-31 11:50:09 -0700133 write!(out.front, "{}", out.include);
134
135 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400136}
137
138fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400139 for ty in types {
140 match ty {
David Tolnay89e386d2020-10-03 19:02:19 -0700141 Type::Ident(ident) => match Atom::from(ident) {
142 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
143 | Some(I64) => out.include.cstdint = true,
144 Some(Usize) => out.include.cstddef = true,
145 Some(CxxString) => out.include.string = true,
David Tolnayf57f7562020-10-04 19:56:26 -0700146 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700147 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800148 Type::RustBox(_) => out.include.type_traits = true,
149 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700150 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700151 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700152 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400153 }
154 }
David Tolnay7db73692019-10-20 14:51:12 -0400155}
156
David Tolnayf51447e2020-03-06 14:14:27 -0800157fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay16ab1462020-09-02 15:10:09 -0700158 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700159 let mut needs_rust_string = false;
160 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700161 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400162 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700163 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700164 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700165 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700166 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400167 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700168 match ty {
169 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700170 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700171 out.include.type_traits = true;
172 needs_rust_box = true;
173 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700174 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700175 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700176 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700177 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700178 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700179 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700180 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700181 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700182 Type::Str(_) => {
183 out.include.cstdint = true;
184 out.include.string = true;
185 needs_rust_str = true;
186 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700187 Type::Fn(_) => {
188 needs_rust_fn = true;
189 }
David Tolnay4770b472020-04-14 16:32:59 -0700190 Type::Slice(_) | Type::SliceRefU8(_) => {
191 needs_rust_slice = true;
192 }
David Tolnay7c295462020-04-25 12:45:07 -0700193 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400194 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700195 needs_rust_isize = true;
196 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700197 ty if ty == RustString => {
198 out.include.array = true;
199 out.include.cstdint = true;
200 out.include.string = true;
201 needs_rust_string = true;
202 }
203 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400204 }
205 }
206
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800208 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800209 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700210 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800211 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700212 match api {
213 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700214 if efn.throws {
215 needs_trycatch = true;
216 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700217 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700218 let bitcopy = match arg.ty {
219 Type::RustVec(_) => true,
220 _ => arg.ty == RustString,
221 };
222 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700223 needs_unsafe_bitcopy = true;
224 break;
225 }
David Tolnay09011c32020-03-06 14:40:28 -0800226 }
227 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 Api::RustFunction(efn) if !out.header => {
229 if efn.throws {
230 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700231 out.include.string = true;
232 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200234 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 }
236 for arg in &efn.args {
237 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
238 needs_manually_drop = true;
239 break;
240 }
241 }
242 if let Some(ret) = &efn.ret {
243 if types.needs_indirect_abi(ret) {
244 needs_maybe_uninit = true;
245 }
David Tolnayf51447e2020-03-06 14:14:27 -0800246 }
247 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700248 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800249 }
250 }
251
David Tolnay750755e2020-03-01 13:04:08 -0800252 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700253 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800254
David Tolnay16ab1462020-09-02 15:10:09 -0700255 if needs_panic
256 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700257 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700258 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700259 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700260 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700261 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700262 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700263 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700264 || needs_unsafe_bitcopy
265 || needs_manually_drop
266 || needs_maybe_uninit
267 {
David Tolnay736cbca2020-03-11 16:49:18 -0700268 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800269 }
270
David Tolnay8f16ae72020-10-08 18:21:13 -0700271 include::write(out, needs_panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700272
David Tolnay99a95e62020-09-02 15:05:53 -0700273 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700274 out.next_section();
275 writeln!(out, "struct unsafe_bitcopy_t;");
276 }
277
David Tolnay8f16ae72020-10-08 18:21:13 -0700278 include::write(out, needs_rust_string, "CXXBRIDGE05_RUST_STRING");
279 include::write(out, needs_rust_str, "CXXBRIDGE05_RUST_STR");
280 include::write(out, needs_rust_slice, "CXXBRIDGE05_RUST_SLICE");
281 include::write(out, needs_rust_box, "CXXBRIDGE05_RUST_BOX");
282 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
283 include::write(out, needs_rust_vec, "CXXBRIDGE05_RUST_VEC");
284 include::write(out, needs_rust_fn, "CXXBRIDGE05_RUST_FN");
285 include::write(out, needs_rust_error, "CXXBRIDGE05_RUST_ERROR");
286 include::write(out, needs_rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800287
288 if needs_manually_drop {
289 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700290 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800291 writeln!(out, "template <typename T>");
292 writeln!(out, "union ManuallyDrop {{");
293 writeln!(out, " T value;");
294 writeln!(
295 out,
296 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
297 );
298 writeln!(out, " ~ManuallyDrop() {{}}");
299 writeln!(out, "}};");
300 }
301
David Tolnay09011c32020-03-06 14:40:28 -0800302 if needs_maybe_uninit {
303 out.next_section();
304 writeln!(out, "template <typename T>");
305 writeln!(out, "union MaybeUninit {{");
306 writeln!(out, " T value;");
307 writeln!(out, " MaybeUninit() {{}}");
308 writeln!(out, " ~MaybeUninit() {{}}");
309 writeln!(out, "}};");
310 }
311
David Tolnay8f16ae72020-10-08 18:21:13 -0700312 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700313
David Tolnay5d121442020-03-17 22:14:40 -0700314 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700315 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700316 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700317 out.include.type_traits = true;
318 out.include.utility = true;
319 writeln!(out, "class missing {{}};");
320 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700321 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700322 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700323 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700324 writeln!(
325 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700326 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700327 );
David Tolnay04722332020-03-18 11:31:54 -0700328 writeln!(out, " missing>::value>::type");
329 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700330 writeln!(out, " func();");
331 writeln!(out, "}} catch (const ::std::exception &e) {{");
332 writeln!(out, " fail(e.what());");
333 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700334 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700335 }
336
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800337 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400338}
339
340fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay8f16ae72020-10-08 18:21:13 -0700341 let guard = format!("CXXBRIDGE05_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700342 writeln!(out, "#ifndef {}", guard);
343 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400344 for line in strct.doc.to_string().lines() {
345 writeln!(out, "//{}", line);
346 }
347 writeln!(out, "struct {} final {{", strct.ident);
348 for field in &strct.fields {
349 write!(out, " ");
350 write_type_space(out, &field.ty);
351 writeln!(out, "{};", field.ident);
352 }
353 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700354 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400355}
356
357fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
358 writeln!(out, "struct {};", ident);
359}
360
David Tolnay8861bee2020-01-20 18:39:24 -0800361fn write_struct_using(out: &mut OutFile, ident: &Ident) {
362 writeln!(out, "using {} = {};", ident, ident);
363}
364
David Tolnayc1fe0052020-04-17 15:15:06 -0700365fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay8f16ae72020-10-08 18:21:13 -0700366 let guard = format!("CXXBRIDGE05_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700367 writeln!(out, "#ifndef {}", guard);
368 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700369 for line in ety.doc.to_string().lines() {
370 writeln!(out, "//{}", line);
371 }
372 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700373 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700374 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700375 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700376 write!(out, " ");
377 let sig = &method.sig;
David Tolnaya4641c72020-09-08 14:05:53 -0700378 let local_name = method.ident.cxx.to_string();
David Tolnaya73853b2020-04-20 01:19:56 -0700379 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700380 writeln!(out, ";");
381 }
382 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700383 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700384}
385
Joel Galensonc03402a2020-04-23 17:31:09 -0700386fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay8f16ae72020-10-08 18:21:13 -0700387 let guard = format!("CXXBRIDGE05_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700388 writeln!(out, "#ifndef {}", guard);
389 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700390 for line in enm.doc.to_string().lines() {
391 writeln!(out, "//{}", line);
392 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700393 write!(out, "enum class {} : ", enm.ident);
394 write_atom(out, enm.repr);
395 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700396 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700397 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700398 }
399 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700400 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700401}
402
Joel Galenson905eb2e2020-05-04 14:58:14 -0700403fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700404 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
405 write_atom(out, enm.repr);
406 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700407 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700408 write!(out, "static_assert(static_cast<");
409 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700410 writeln!(
411 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700412 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700413 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700414 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700415 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700416}
417
Adrian Taylor405e8742020-09-25 14:01:25 -0700418fn check_trivial_extern_type(out: &mut OutFile, id: &Ident) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700419 // NOTE: The following two static assertions are just nice-to-have and not
420 // necessary for soundness. That's because triviality is always declared by
421 // the user in the form of an unsafe impl of cxx::ExternType:
422 //
423 // unsafe impl ExternType for MyType {
424 // type Id = cxx::type_id!("...");
425 // type Kind = cxx::kind::Trivial;
426 // }
427 //
428 // Since the user went on the record with their unsafe impl to unsafely
429 // claim they KNOW that the type is trivial, it's fine for that to be on
430 // them if that were wrong.
431 //
432 // There may be a legitimate reason we'll want to remove these assertions
433 // for support of types that the programmer knows are Rust-movable despite
434 // not being recognized as such by the C++ type system due to a move
435 // constructor or destructor.
436
David Tolnayf57f7562020-10-04 19:56:26 -0700437 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700438 writeln!(out, "static_assert(");
439 writeln!(
440 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700441 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700442 id,
443 );
444 writeln!(
445 out,
446 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
447 id,
448 );
449 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700450 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700451 writeln!(
452 out,
453 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
454 id,
455 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700456}
457
David Tolnayebef4a22020-03-17 15:33:47 -0700458fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
459 let mut has_cxx_throws = false;
460 for api in apis {
461 if let Api::CxxFunction(efn) = api {
462 if efn.throws {
463 has_cxx_throws = true;
464 break;
465 }
466 }
467 }
468
469 if has_cxx_throws {
470 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700471 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700472 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700473 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700474 );
475 }
476}
477
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700478fn write_cxx_function_shim(
479 out: &mut OutFile,
480 efn: &ExternFn,
481 types: &Types,
482 impl_annotations: &Option<String>,
483) {
484 if !out.header {
485 if let Some(annotation) = impl_annotations {
486 write!(out, "{} ", annotation);
487 }
488 }
David Tolnayebef4a22020-03-17 15:33:47 -0700489 if efn.throws {
490 write!(out, "::rust::Str::Repr ");
491 } else {
David Tolnay99642622020-03-25 13:07:35 -0700492 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700493 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700494 let mangled = mangle::extern_fn(&out.namespace, efn);
495 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700496 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700497 if receiver.mutability.is_none() {
498 write!(out, "const ");
499 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700500 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700501 }
David Tolnay7db73692019-10-20 14:51:12 -0400502 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700503 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400504 write!(out, ", ");
505 }
David Tolnaya46a2372020-03-06 10:03:48 -0800506 if arg.ty == RustString {
507 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700508 } else if let Type::RustVec(_) = arg.ty {
509 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800510 }
David Tolnay7db73692019-10-20 14:51:12 -0400511 write_extern_arg(out, arg, types);
512 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700513 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400514 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700515 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400516 write!(out, ", ");
517 }
David Tolnay99642622020-03-25 13:07:35 -0700518 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400519 write!(out, "*return$");
520 }
521 writeln!(out, ") noexcept {{");
522 write!(out, " ");
523 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700524 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700525 None => write!(out, "(*{}$)(", efn.ident.rust),
526 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700527 }
David Tolnay7db73692019-10-20 14:51:12 -0400528 for (i, arg) in efn.args.iter().enumerate() {
529 if i > 0 {
530 write!(out, ", ");
531 }
532 write_type(out, &arg.ty);
533 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700534 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700535 if let Some(receiver) = &efn.receiver {
536 if receiver.mutability.is_none() {
537 write!(out, " const");
538 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700539 }
540 write!(out, " = ");
541 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700542 None => write!(out, "{}", efn.ident.cxx),
543 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident.cxx),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700544 }
545 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400546 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700547 if efn.throws {
548 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700549 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700550 writeln!(out, " [&] {{");
551 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700552 }
David Tolnay7db73692019-10-20 14:51:12 -0400553 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700554 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400555 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700556 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400557 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700558 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400559 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700560 }
561 match &efn.ret {
562 Some(Type::Ref(_)) => write!(out, "&"),
563 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700564 Some(Type::SliceRefU8(_)) if !indirect_return => {
565 write!(out, "::rust::Slice<uint8_t>::Repr(")
566 }
David Tolnay99642622020-03-25 13:07:35 -0700567 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400568 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700569 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700570 None => write!(out, "{}$(", efn.ident.rust),
571 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700572 }
David Tolnay7db73692019-10-20 14:51:12 -0400573 for (i, arg) in efn.args.iter().enumerate() {
574 if i > 0 {
575 write!(out, ", ");
576 }
577 if let Type::RustBox(_) = &arg.ty {
578 write_type(out, &arg.ty);
579 write!(out, "::from_raw({})", arg.ident);
580 } else if let Type::UniquePtr(_) = &arg.ty {
581 write_type(out, &arg.ty);
582 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800583 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800584 write!(
585 out,
586 "::rust::String(::rust::unsafe_bitcopy, *{})",
587 arg.ident,
588 );
David Tolnay313b10e2020-04-25 16:30:51 -0700589 } else if let Type::RustVec(_) = arg.ty {
590 write_type(out, &arg.ty);
591 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400592 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700593 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800594 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400595 } else {
596 write!(out, "{}", arg.ident);
597 }
598 }
599 write!(out, ")");
600 match &efn.ret {
601 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
602 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700603 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400604 _ => {}
605 }
606 if indirect_return {
607 write!(out, ")");
608 }
609 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700610 if efn.throws {
611 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700612 writeln!(out, " throw$.ptr = nullptr;");
613 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700614 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700615 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700616 writeln!(
617 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700618 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700619 );
David Tolnay5d121442020-03-17 22:14:40 -0700620 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700621 writeln!(out, " return throw$;");
622 }
David Tolnay7db73692019-10-20 14:51:12 -0400623 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700624 for arg in &efn.args {
625 if let Type::Fn(f) = &arg.ty {
626 let var = &arg.ident;
627 write_function_pointer_trampoline(out, efn, var, f, types);
628 }
629 }
630}
631
632fn write_function_pointer_trampoline(
633 out: &mut OutFile,
634 efn: &ExternFn,
635 var: &Ident,
636 f: &Signature,
637 types: &Types,
638) {
639 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700640 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700641 let indirect_call = true;
642 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
643
644 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700645 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700646 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400647}
648
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700649fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700650 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700651 let indirect_call = false;
652 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
653}
654
655fn write_rust_function_decl_impl(
656 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700657 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700658 sig: &Signature,
659 types: &Types,
660 indirect_call: bool,
661) {
662 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700663 write!(out, "::rust::Str::Repr ");
664 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700665 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700666 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700667 write!(out, "{}(", link_name);
668 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700669 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700670 if receiver.mutability.is_none() {
671 write!(out, "const ");
672 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700673 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700674 needs_comma = true;
675 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700676 for arg in &sig.args {
677 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400678 write!(out, ", ");
679 }
680 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700681 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400682 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700683 if indirect_return(sig, types) {
684 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400685 write!(out, ", ");
686 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700687 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400688 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700689 needs_comma = true;
690 }
691 if indirect_call {
692 if needs_comma {
693 write!(out, ", ");
694 }
695 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400696 }
697 writeln!(out, ") noexcept;");
698}
699
700fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400701 for line in efn.doc.to_string().lines() {
702 writeln!(out, "//{}", line);
703 }
David Tolnaya73853b2020-04-20 01:19:56 -0700704 let local_name = match &efn.sig.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700705 None => efn.ident.cxx.to_string(),
706 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident.cxx),
David Tolnaya73853b2020-04-20 01:19:56 -0700707 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700708 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700709 let indirect_call = false;
710 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
711}
712
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700713fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700714 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700715 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700716 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700717 indirect_call: bool,
718) {
719 write_return_type(out, &sig.ret);
720 write!(out, "{}(", local_name);
721 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400722 if i > 0 {
723 write!(out, ", ");
724 }
725 write_type_space(out, &arg.ty);
726 write!(out, "{}", arg.ident);
727 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700728 if indirect_call {
729 if !sig.args.is_empty() {
730 write!(out, ", ");
731 }
732 write!(out, "void *extern$");
733 }
David Tolnay1e548172020-03-16 13:37:09 -0700734 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700735 if let Some(receiver) = &sig.receiver {
736 if receiver.mutability.is_none() {
737 write!(out, " const");
738 }
739 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700740 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700741 write!(out, " noexcept");
742 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700743}
744
745fn write_rust_function_shim_impl(
746 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700747 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700748 sig: &Signature,
749 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700750 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700751 indirect_call: bool,
752) {
753 if out.header && sig.receiver.is_some() {
754 // We've already defined this inside the struct.
755 return;
756 }
David Tolnaya73853b2020-04-20 01:19:56 -0700757 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400758 if out.header {
759 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700760 return;
David Tolnay7db73692019-10-20 14:51:12 -0400761 }
David Tolnay439cde22020-04-20 00:46:25 -0700762 writeln!(out, " {{");
763 for arg in &sig.args {
764 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
765 out.include.utility = true;
766 write!(out, " ::rust::ManuallyDrop<");
767 write_type(out, &arg.ty);
768 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
769 }
770 }
771 write!(out, " ");
772 let indirect_return = indirect_return(sig, types);
773 if indirect_return {
774 write!(out, "::rust::MaybeUninit<");
775 write_type(out, sig.ret.as_ref().unwrap());
776 writeln!(out, "> return$;");
777 write!(out, " ");
778 } else if let Some(ret) = &sig.ret {
779 write!(out, "return ");
780 match ret {
781 Type::RustBox(_) => {
782 write_type(out, ret);
783 write!(out, "::from_raw(");
784 }
785 Type::UniquePtr(_) => {
786 write_type(out, ret);
787 write!(out, "(");
788 }
789 Type::Ref(_) => write!(out, "*"),
790 _ => {}
791 }
792 }
793 if sig.throws {
794 write!(out, "::rust::Str::Repr error$ = ");
795 }
796 write!(out, "{}(", invoke);
797 if sig.receiver.is_some() {
798 write!(out, "*this");
799 }
800 for (i, arg) in sig.args.iter().enumerate() {
801 if i > 0 || sig.receiver.is_some() {
802 write!(out, ", ");
803 }
804 match &arg.ty {
805 Type::Str(_) => write!(out, "::rust::Str::Repr("),
806 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
807 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
808 _ => {}
809 }
810 write!(out, "{}", arg.ident);
811 match &arg.ty {
812 Type::RustBox(_) => write!(out, ".into_raw()"),
813 Type::UniquePtr(_) => write!(out, ".release()"),
814 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
815 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
816 _ => {}
817 }
818 }
819 if indirect_return {
820 if !sig.args.is_empty() {
821 write!(out, ", ");
822 }
823 write!(out, "&return$.value");
824 }
825 if indirect_call {
826 if !sig.args.is_empty() || indirect_return {
827 write!(out, ", ");
828 }
829 write!(out, "extern$");
830 }
831 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400832 if !indirect_return {
833 if let Some(ret) = &sig.ret {
834 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
835 write!(out, ")");
836 }
David Tolnay439cde22020-04-20 00:46:25 -0700837 }
838 }
839 writeln!(out, ";");
840 if sig.throws {
841 writeln!(out, " if (error$.ptr) {{");
842 writeln!(out, " throw ::rust::Error(error$);");
843 writeln!(out, " }}");
844 }
845 if indirect_return {
846 out.include.utility = true;
847 writeln!(out, " return ::std::move(return$.value);");
848 }
849 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400850}
851
852fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
853 match ty {
854 None => write!(out, "void "),
855 Some(ty) => write_type_space(out, ty),
856 }
857}
858
David Tolnay75dca2e2020-03-25 20:17:52 -0700859fn indirect_return(sig: &Signature, types: &Types) -> bool {
860 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700861 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700862 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700863}
864
David Tolnay99642622020-03-25 13:07:35 -0700865fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
866 match ty {
867 Type::RustBox(ty) | Type::UniquePtr(ty) => {
868 write_type_space(out, &ty.inner);
869 write!(out, "*");
870 }
871 Type::Ref(ty) => {
872 if ty.mutability.is_none() {
873 write!(out, "const ");
874 }
875 write_type(out, &ty.inner);
876 write!(out, " *");
877 }
878 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700879 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700880 _ => write_type(out, ty),
881 }
882}
883
884fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
885 write_indirect_return_type(out, ty);
886 match ty {
887 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700888 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700889 _ => write_space_after_type(out, ty),
890 }
891}
892
893fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400894 match ty {
895 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
896 write_type_space(out, &ty.inner);
897 write!(out, "*");
898 }
David Tolnay4a441222020-01-25 16:24:27 -0800899 Some(Type::Ref(ty)) => {
900 if ty.mutability.is_none() {
901 write!(out, "const ");
902 }
903 write_type(out, &ty.inner);
904 write!(out, " *");
905 }
David Tolnay750755e2020-03-01 13:04:08 -0800906 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700907 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400908 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
909 _ => write_return_type(out, ty),
910 }
911}
912
913fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
914 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700915 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400916 write_type_space(out, &ty.inner);
917 write!(out, "*");
918 }
David Tolnay750755e2020-03-01 13:04:08 -0800919 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700920 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400921 _ => write_type_space(out, &arg.ty),
922 }
923 if types.needs_indirect_abi(&arg.ty) {
924 write!(out, "*");
925 }
926 write!(out, "{}", arg.ident);
927}
928
929fn write_type(out: &mut OutFile, ty: &Type) {
930 match ty {
931 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700932 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400933 None => write!(out, "{}", ident),
934 },
935 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800936 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400937 write_type(out, &ty.inner);
938 write!(out, ">");
939 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700940 Type::RustVec(ty) => {
941 write!(out, "::rust::Vec<");
942 write_type(out, &ty.inner);
943 write!(out, ">");
944 }
David Tolnay7db73692019-10-20 14:51:12 -0400945 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800946 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400947 write_type(out, &ptr.inner);
948 write!(out, ">");
949 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700950 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700951 write!(out, "::std::vector<");
952 write_type(out, &ty.inner);
953 write!(out, ">");
954 }
David Tolnay7db73692019-10-20 14:51:12 -0400955 Type::Ref(r) => {
956 if r.mutability.is_none() {
957 write!(out, "const ");
958 }
959 write_type(out, &r.inner);
960 write!(out, " &");
961 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700962 Type::Slice(_) => {
963 // For now, only U8 slices are supported, which are covered separately below
964 unreachable!()
965 }
David Tolnay7db73692019-10-20 14:51:12 -0400966 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800967 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400968 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700969 Type::SliceRefU8(_) => {
970 write!(out, "::rust::Slice<uint8_t>");
971 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700972 Type::Fn(f) => {
973 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
974 match &f.ret {
975 Some(ret) => write_type(out, ret),
976 None => write!(out, "void"),
977 }
978 write!(out, "(");
979 for (i, arg) in f.args.iter().enumerate() {
980 if i > 0 {
981 write!(out, ", ");
982 }
983 write_type(out, &arg.ty);
984 }
985 write!(out, ")>");
986 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700987 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400988 }
989}
990
David Tolnayf6a89f22020-05-10 23:39:27 -0700991fn write_atom(out: &mut OutFile, atom: Atom) {
992 match atom {
993 Bool => write!(out, "bool"),
994 U8 => write!(out, "uint8_t"),
995 U16 => write!(out, "uint16_t"),
996 U32 => write!(out, "uint32_t"),
997 U64 => write!(out, "uint64_t"),
998 Usize => write!(out, "size_t"),
999 I8 => write!(out, "int8_t"),
1000 I16 => write!(out, "int16_t"),
1001 I32 => write!(out, "int32_t"),
1002 I64 => write!(out, "int64_t"),
1003 Isize => write!(out, "::rust::isize"),
1004 F32 => write!(out, "float"),
1005 F64 => write!(out, "double"),
1006 CxxString => write!(out, "::std::string"),
1007 RustString => write!(out, "::rust::String"),
1008 }
1009}
1010
David Tolnay7db73692019-10-20 14:51:12 -04001011fn write_type_space(out: &mut OutFile, ty: &Type) {
1012 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001013 write_space_after_type(out, ty);
1014}
1015
1016fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001017 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001018 Type::Ident(_)
1019 | Type::RustBox(_)
1020 | Type::UniquePtr(_)
1021 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001022 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001023 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001024 | Type::SliceRefU8(_)
1025 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001026 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001027 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001028 }
1029}
1030
David Tolnaycd08c442020-04-25 10:16:33 -07001031// Only called for legal referent types of unique_ptr and element types of
1032// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -07001033fn to_typename(namespace: &Namespace, ty: &Type) -> String {
1034 match ty {
1035 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -07001036 let mut path = String::new();
1037 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001038 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -07001039 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001040 }
David Tolnaycd08c442020-04-25 10:16:33 -07001041 path += &ident.to_string();
1042 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001043 }
1044 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001045 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001046 }
1047}
1048
David Tolnayacdf20a2020-04-25 12:40:53 -07001049// Only called for legal referent types of unique_ptr and element types of
1050// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001051fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1052 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001053 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001054 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001055 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001056 }
1057}
1058
David Tolnay7db73692019-10-20 14:51:12 -04001059fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001060 out.begin_block("extern \"C\"");
1061 for ty in types {
1062 if let Type::RustBox(ty) = ty {
1063 if let Type::Ident(inner) = &ty.inner {
1064 out.next_section();
1065 write_rust_box_extern(out, inner);
1066 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001067 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001068 if let Type::Ident(inner) = &ty.inner {
1069 if Atom::from(inner).is_none() {
1070 out.next_section();
1071 write_rust_vec_extern(out, inner);
1072 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001073 }
David Tolnay7db73692019-10-20 14:51:12 -04001074 } else if let Type::UniquePtr(ptr) = ty {
1075 if let Type::Ident(inner) = &ptr.inner {
David Tolnay7e69f892020-10-03 22:20:22 -07001076 if Atom::from(inner).is_none()
1077 && (!types.aliases.contains_key(inner) || types.explicit_impls.contains(ty))
1078 {
David Tolnay7db73692019-10-20 14:51:12 -04001079 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001080 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001081 }
1082 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001083 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001084 if let Type::Ident(inner) = &ptr.inner {
David Tolnay7e69f892020-10-03 22:20:22 -07001085 if Atom::from(inner).is_none()
1086 && (!types.aliases.contains_key(inner) || types.explicit_impls.contains(ty))
1087 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001088 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001089 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001090 }
1091 }
1092 }
1093 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001094 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001095
David Tolnay750755e2020-03-01 13:04:08 -08001096 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001097 out.begin_block("inline namespace cxxbridge05");
David Tolnay7db73692019-10-20 14:51:12 -04001098 for ty in types {
1099 if let Type::RustBox(ty) = ty {
1100 if let Type::Ident(inner) = &ty.inner {
1101 write_rust_box_impl(out, inner);
1102 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001103 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001104 if let Type::Ident(inner) = &ty.inner {
1105 if Atom::from(inner).is_none() {
1106 write_rust_vec_impl(out, inner);
1107 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001108 }
David Tolnay7db73692019-10-20 14:51:12 -04001109 }
1110 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001111 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001112 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001113}
1114
1115fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1116 let mut inner = String::new();
1117 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001118 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001119 inner += "::";
1120 }
1121 inner += &ident.to_string();
1122 let instance = inner.replace("::", "$");
1123
David Tolnay8f16ae72020-10-08 18:21:13 -07001124 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1125 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001126 writeln!(
1127 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001128 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001129 instance, inner,
1130 );
1131 writeln!(
1132 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001133 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001134 instance, inner,
1135 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001136 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001137}
1138
David Tolnay6787be62020-04-25 11:01:02 -07001139fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1140 let element = Type::Ident(element.clone());
1141 let inner = to_typename(&out.namespace, &element);
1142 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001143
David Tolnay8f16ae72020-10-08 18:21:13 -07001144 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1145 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001146 writeln!(
1147 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001148 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001149 instance, inner,
1150 );
1151 writeln!(
1152 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001153 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001154 instance, inner,
1155 );
1156 writeln!(
1157 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001158 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001159 instance, inner,
1160 );
David Tolnay219c0792020-04-24 20:31:37 -07001161 writeln!(
1162 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001163 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001164 inner, instance,
1165 );
David Tolnay503d0192020-04-24 22:18:56 -07001166 writeln!(
1167 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001168 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001169 instance,
1170 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001171 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001172}
1173
David Tolnay7db73692019-10-20 14:51:12 -04001174fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1175 let mut inner = String::new();
1176 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001177 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001178 inner += "::";
1179 }
1180 inner += &ident.to_string();
1181 let instance = inner.replace("::", "$");
1182
1183 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001184 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001185 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001186 writeln!(out, "}}");
1187
1188 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001189 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001190 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001191 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001192}
1193
David Tolnay6787be62020-04-25 11:01:02 -07001194fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1195 let element = Type::Ident(element.clone());
1196 let inner = to_typename(&out.namespace, &element);
1197 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001198
Myron Ahneba35cf2020-02-05 19:41:51 +07001199 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001200 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001201 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001202 writeln!(out, "}}");
1203
1204 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001205 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1206 writeln!(
1207 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001208 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001209 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001210 );
1211 writeln!(out, "}}");
1212
1213 writeln!(out, "template <>");
1214 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001215 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001216 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001217
1218 writeln!(out, "template <>");
1219 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1220 writeln!(
1221 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001222 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001223 instance,
1224 );
1225 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001226
1227 writeln!(out, "template <>");
1228 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001229 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001230 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001231}
1232
David Tolnay63da4d32020-04-25 09:41:12 -07001233fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1234 let ty = Type::Ident(ident.clone());
1235 let instance = to_mangled(&out.namespace, &ty);
1236
David Tolnay8f16ae72020-10-08 18:21:13 -07001237 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1238 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001239
1240 write_unique_ptr_common(out, &ty, types);
1241
David Tolnay8f16ae72020-10-08 18:21:13 -07001242 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001243}
1244
1245// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1246fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001247 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001248 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001249 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001250 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001251
David Tolnay63da4d32020-04-25 09:41:12 -07001252 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001253 // Some aliases are to opaque types; some are to trivial types. We can't
1254 // know at code generation time, so we generate both C++ and Rust side
1255 // bindings for a "new" method anyway. But the Rust code can't be called
1256 // for Opaque types because the 'new' method is not implemented.
1257 Type::Ident(ident) => {
1258 types.structs.contains_key(ident) || types.aliases.contains_key(ident)
1259 }
David Tolnay63da4d32020-04-25 09:41:12 -07001260 _ => false,
1261 };
1262
David Tolnay7db73692019-10-20 14:51:12 -04001263 writeln!(
1264 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001265 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001266 inner,
1267 );
1268 writeln!(
1269 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001270 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001271 inner,
1272 );
1273 writeln!(
1274 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001275 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001276 instance, inner,
1277 );
David Tolnay7e219b82020-03-01 13:14:51 -08001278 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001279 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001280 if can_construct_from_value {
1281 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001282 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001283 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001284 instance, inner, inner,
1285 );
David Tolnay63da4d32020-04-25 09:41:12 -07001286 writeln!(
1287 out,
1288 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1289 inner, inner,
1290 );
1291 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001292 }
David Tolnay7db73692019-10-20 14:51:12 -04001293 writeln!(
1294 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001295 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001296 instance, inner, inner,
1297 );
David Tolnay7e219b82020-03-01 13:14:51 -08001298 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001299 writeln!(out, "}}");
1300 writeln!(
1301 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001302 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001303 inner, instance, inner,
1304 );
1305 writeln!(out, " return ptr.get();");
1306 writeln!(out, "}}");
1307 writeln!(
1308 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001309 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001310 inner, instance, inner,
1311 );
1312 writeln!(out, " return ptr.release();");
1313 writeln!(out, "}}");
1314 writeln!(
1315 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001316 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001317 instance, inner,
1318 );
1319 writeln!(out, " ptr->~unique_ptr();");
1320 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001321}
Myron Ahneba35cf2020-02-05 19:41:51 +07001322
David Tolnay92105da2020-04-25 11:15:31 -07001323fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001324 let element = Type::Ident(element.clone());
1325 let inner = to_typename(&out.namespace, &element);
1326 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001327
David Tolnay8f16ae72020-10-08 18:21:13 -07001328 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1329 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001330 writeln!(
1331 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001332 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001333 instance, inner,
1334 );
1335 writeln!(out, " return s.size();");
1336 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001337 writeln!(
1338 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001339 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001340 inner, instance, inner,
1341 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001342 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001343 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001344
1345 write_unique_ptr_common(out, vector_ty, types);
1346
David Tolnay8f16ae72020-10-08 18:21:13 -07001347 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001348}