blob: 8cc1b1b55add0cf866e5bd69228b1de49dcace62 [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;
Joel Galensonc03402a2020-04-23 17:31:09 -07006use crate::syntax::{mangle, Api, Enum, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -07008use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04009
David Tolnay33d30292020-03-18 18:02:02 -070010pub(super) fn gen(
David Tolnay2ec14632020-05-04 00:47:10 -070011 namespace: &Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070012 apis: &[Api],
13 types: &Types,
David Tolnaya5cca312020-08-29 23:40:04 -070014 opt: &Opt,
David Tolnay33d30292020-03-18 18:02:02 -070015 header: bool,
16) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040017 let mut out_file = OutFile::new(namespace.clone(), header);
18 let out = &mut out_file;
19
David Tolnay54702b92020-07-31 11:50:09 -070020 if header {
21 writeln!(out.front, "#pragma once");
22 }
23
David Tolnaya5cca312020-08-29 23:40:04 -070024 out.include.extend(opt.include.clone());
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070027 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070035 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040036 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070045 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040046 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
David Tolnaya593d6e2020-08-29 19:48:08 -070065 if !types.cxx.contains(&strct.ident) {
66 write_struct(out, strct);
67 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070068 }
Joel Galensonc03402a2020-04-23 17:31:09 -070069 Api::Enum(enm) => {
70 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070071 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070072 check_enum(out, enm);
73 } else {
74 write_enum(out, enm);
75 }
Joel Galensonc03402a2020-04-23 17:31:09 -070076 }
David Tolnayc1fe0052020-04-17 15:15:06 -070077 Api::RustType(ety) => {
78 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070079 out.next_section();
80 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070081 }
David Tolnayc1fe0052020-04-17 15:15:06 -070082 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070083 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040084 }
85 }
86
87 if !header {
88 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070089 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040090 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070091 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040092 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
93 Api::RustFunction(efn) => (efn, write_rust_function_decl),
94 _ => continue,
95 };
96 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -070097 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -040098 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080099 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400100 }
101
102 for api in apis {
103 if let Api::RustFunction(efn) = api {
104 out.next_section();
105 write_rust_function_shim(out, efn, types);
106 }
107 }
108
109 out.next_section();
110 for name in namespace.iter().rev() {
111 writeln!(out, "}} // namespace {}", name);
112 }
113
114 if !header {
115 out.next_section();
116 write_generic_instantiations(out, types);
117 }
118
David Tolnay54702b92020-07-31 11:50:09 -0700119 write!(out.front, "{}", out.include);
120
121 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400122}
123
124fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400125 for ty in types {
126 match ty {
127 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700128 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
129 | Some(I64) => out.include.cstdint = true,
130 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800131 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700132 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400133 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800134 Type::RustBox(_) => out.include.type_traits = true,
135 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700136 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700137 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700138 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400139 }
140 }
David Tolnay7db73692019-10-20 14:51:12 -0400141}
142
David Tolnayf51447e2020-03-06 14:14:27 -0800143fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay16ab1462020-09-02 15:10:09 -0700144 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700145 let mut needs_rust_string = false;
146 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700147 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400148 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700149 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700150 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700151 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700152 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400153 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700154 match ty {
155 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700156 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700157 out.include.type_traits = true;
158 needs_rust_box = true;
159 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700160 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700161 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700162 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700163 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700164 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700165 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700166 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700167 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700168 Type::Str(_) => {
169 out.include.cstdint = true;
170 out.include.string = true;
171 needs_rust_str = true;
172 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700173 Type::Fn(_) => {
174 needs_rust_fn = true;
175 }
David Tolnay4770b472020-04-14 16:32:59 -0700176 Type::Slice(_) | Type::SliceRefU8(_) => {
177 needs_rust_slice = true;
178 }
David Tolnay7c295462020-04-25 12:45:07 -0700179 ty if ty == Isize => {
180 out.include.base_tsd = true;
181 needs_rust_isize = true;
182 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700183 ty if ty == RustString => {
184 out.include.array = true;
185 out.include.cstdint = true;
186 out.include.string = true;
187 needs_rust_string = true;
188 }
189 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400190 }
191 }
192
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800194 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800195 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700196 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800197 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700198 match api {
199 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700200 if efn.throws {
201 needs_trycatch = true;
202 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700204 let bitcopy = match arg.ty {
205 Type::RustVec(_) => true,
206 _ => arg.ty == RustString,
207 };
208 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700209 needs_unsafe_bitcopy = true;
210 break;
211 }
David Tolnay09011c32020-03-06 14:40:28 -0800212 }
213 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 Api::RustFunction(efn) if !out.header => {
215 if efn.throws {
216 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700217 out.include.string = true;
218 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 needs_rust_error = true;
220 }
221 for arg in &efn.args {
222 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
223 needs_manually_drop = true;
224 break;
225 }
226 }
227 if let Some(ret) = &efn.ret {
228 if types.needs_indirect_abi(ret) {
229 needs_maybe_uninit = true;
230 }
David Tolnayf51447e2020-03-06 14:14:27 -0800231 }
232 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800234 }
235 }
236
David Tolnay750755e2020-03-01 13:04:08 -0800237 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700238 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800239
David Tolnay16ab1462020-09-02 15:10:09 -0700240 if needs_panic
241 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700243 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700244 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700245 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700246 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700247 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700248 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 || needs_unsafe_bitcopy
250 || needs_manually_drop
251 || needs_maybe_uninit
252 {
David Tolnay736cbca2020-03-11 16:49:18 -0700253 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800254 }
255
David Tolnay16ab1462020-09-02 15:10:09 -0700256 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
257
David Tolnay99a95e62020-09-02 15:05:53 -0700258 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700259 out.next_section();
260 writeln!(out, "struct unsafe_bitcopy_t;");
261 }
262
David Tolnay591dcb62020-09-01 23:00:38 -0700263 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
264 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
265 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
266 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700267 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700268 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
269 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
270 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
271 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800272
273 if needs_manually_drop {
274 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700275 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800276 writeln!(out, "template <typename T>");
277 writeln!(out, "union ManuallyDrop {{");
278 writeln!(out, " T value;");
279 writeln!(
280 out,
281 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
282 );
283 writeln!(out, " ~ManuallyDrop() {{}}");
284 writeln!(out, "}};");
285 }
286
David Tolnay09011c32020-03-06 14:40:28 -0800287 if needs_maybe_uninit {
288 out.next_section();
289 writeln!(out, "template <typename T>");
290 writeln!(out, "union MaybeUninit {{");
291 writeln!(out, " T value;");
292 writeln!(out, " MaybeUninit() {{}}");
293 writeln!(out, " ~MaybeUninit() {{}}");
294 writeln!(out, "}};");
295 }
296
David Tolnay591dcb62020-09-01 23:00:38 -0700297 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700298
David Tolnay5d121442020-03-17 22:14:40 -0700299 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700300 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700301 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700302 out.include.type_traits = true;
303 out.include.utility = true;
304 writeln!(out, "class missing {{}};");
305 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700306 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700307 writeln!(out, "template <typename Try, typename Fail>");
308 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700309 writeln!(
310 out,
David Tolnay04722332020-03-18 11:31:54 -0700311 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700312 );
David Tolnay04722332020-03-18 11:31:54 -0700313 writeln!(out, " missing>::value>::type");
314 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700315 writeln!(out, " func();");
316 writeln!(out, "}} catch (const ::std::exception &e) {{");
317 writeln!(out, " fail(e.what());");
318 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700319 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700320 }
321
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800322 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400323}
324
325fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700326 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700327 writeln!(out, "#ifndef {}", guard);
328 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400329 for line in strct.doc.to_string().lines() {
330 writeln!(out, "//{}", line);
331 }
332 writeln!(out, "struct {} final {{", strct.ident);
333 for field in &strct.fields {
334 write!(out, " ");
335 write_type_space(out, &field.ty);
336 writeln!(out, "{};", field.ident);
337 }
338 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700339 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400340}
341
342fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
343 writeln!(out, "struct {};", ident);
344}
345
David Tolnay8861bee2020-01-20 18:39:24 -0800346fn write_struct_using(out: &mut OutFile, ident: &Ident) {
347 writeln!(out, "using {} = {};", ident, ident);
348}
349
David Tolnayc1fe0052020-04-17 15:15:06 -0700350fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700351 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700352 writeln!(out, "#ifndef {}", guard);
353 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700354 for line in ety.doc.to_string().lines() {
355 writeln!(out, "//{}", line);
356 }
357 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700358 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700359 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700360 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700361 write!(out, " ");
362 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700363 let local_name = method.ident.to_string();
364 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700365 writeln!(out, ";");
366 }
367 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700368 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700369}
370
Joel Galensonc03402a2020-04-23 17:31:09 -0700371fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700372 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700373 writeln!(out, "#ifndef {}", guard);
374 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700375 for line in enm.doc.to_string().lines() {
376 writeln!(out, "//{}", line);
377 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700378 write!(out, "enum class {} : ", enm.ident);
379 write_atom(out, enm.repr);
380 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700381 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700382 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700383 }
384 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700385 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700386}
387
Joel Galenson905eb2e2020-05-04 14:58:14 -0700388fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700389 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
390 write_atom(out, enm.repr);
391 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700392 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700393 write!(out, "static_assert(static_cast<");
394 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700395 writeln!(
396 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700397 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700398 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700399 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700400 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700401}
402
David Tolnayebef4a22020-03-17 15:33:47 -0700403fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
404 let mut has_cxx_throws = false;
405 for api in apis {
406 if let Api::CxxFunction(efn) = api {
407 if efn.throws {
408 has_cxx_throws = true;
409 break;
410 }
411 }
412 }
413
414 if has_cxx_throws {
415 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700416 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700417 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700418 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700419 );
420 }
421}
422
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700423fn write_cxx_function_shim(
424 out: &mut OutFile,
425 efn: &ExternFn,
426 types: &Types,
427 impl_annotations: &Option<String>,
428) {
429 if !out.header {
430 if let Some(annotation) = impl_annotations {
431 write!(out, "{} ", annotation);
432 }
433 }
David Tolnayebef4a22020-03-17 15:33:47 -0700434 if efn.throws {
435 write!(out, "::rust::Str::Repr ");
436 } else {
David Tolnay99642622020-03-25 13:07:35 -0700437 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700438 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700439 let mangled = mangle::extern_fn(&out.namespace, efn);
440 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700441 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700442 if receiver.mutability.is_none() {
443 write!(out, "const ");
444 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700445 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700446 }
David Tolnay7db73692019-10-20 14:51:12 -0400447 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700448 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400449 write!(out, ", ");
450 }
David Tolnaya46a2372020-03-06 10:03:48 -0800451 if arg.ty == RustString {
452 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700453 } else if let Type::RustVec(_) = arg.ty {
454 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800455 }
David Tolnay7db73692019-10-20 14:51:12 -0400456 write_extern_arg(out, arg, types);
457 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700458 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400459 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700460 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400461 write!(out, ", ");
462 }
David Tolnay99642622020-03-25 13:07:35 -0700463 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400464 write!(out, "*return$");
465 }
466 writeln!(out, ") noexcept {{");
467 write!(out, " ");
468 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700469 match &efn.receiver {
470 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700471 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700472 }
David Tolnay7db73692019-10-20 14:51:12 -0400473 for (i, arg) in efn.args.iter().enumerate() {
474 if i > 0 {
475 write!(out, ", ");
476 }
477 write_type(out, &arg.ty);
478 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700479 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700480 if let Some(receiver) = &efn.receiver {
481 if receiver.mutability.is_none() {
482 write!(out, " const");
483 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700484 }
485 write!(out, " = ");
486 match &efn.receiver {
487 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700488 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700489 }
490 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400491 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700492 if efn.throws {
493 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700494 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700495 writeln!(out, " [&] {{");
496 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700497 }
David Tolnay7db73692019-10-20 14:51:12 -0400498 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700499 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400500 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700501 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400502 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700503 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400504 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700505 }
506 match &efn.ret {
507 Some(Type::Ref(_)) => write!(out, "&"),
508 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700509 Some(Type::SliceRefU8(_)) if !indirect_return => {
510 write!(out, "::rust::Slice<uint8_t>::Repr(")
511 }
David Tolnay99642622020-03-25 13:07:35 -0700512 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400513 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700514 match &efn.receiver {
515 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700516 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700517 }
David Tolnay7db73692019-10-20 14:51:12 -0400518 for (i, arg) in efn.args.iter().enumerate() {
519 if i > 0 {
520 write!(out, ", ");
521 }
522 if let Type::RustBox(_) = &arg.ty {
523 write_type(out, &arg.ty);
524 write!(out, "::from_raw({})", arg.ident);
525 } else if let Type::UniquePtr(_) = &arg.ty {
526 write_type(out, &arg.ty);
527 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800528 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800529 write!(
530 out,
531 "::rust::String(::rust::unsafe_bitcopy, *{})",
532 arg.ident,
533 );
David Tolnay313b10e2020-04-25 16:30:51 -0700534 } else if let Type::RustVec(_) = arg.ty {
535 write_type(out, &arg.ty);
536 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400537 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700538 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800539 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400540 } else {
541 write!(out, "{}", arg.ident);
542 }
543 }
544 write!(out, ")");
545 match &efn.ret {
546 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
547 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700548 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400549 _ => {}
550 }
551 if indirect_return {
552 write!(out, ")");
553 }
554 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700555 if efn.throws {
556 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700557 writeln!(out, " throw$.ptr = nullptr;");
558 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700559 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700560 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700561 writeln!(
562 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700563 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700564 );
David Tolnay5d121442020-03-17 22:14:40 -0700565 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700566 writeln!(out, " return throw$;");
567 }
David Tolnay7db73692019-10-20 14:51:12 -0400568 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700569 for arg in &efn.args {
570 if let Type::Fn(f) = &arg.ty {
571 let var = &arg.ident;
572 write_function_pointer_trampoline(out, efn, var, f, types);
573 }
574 }
575}
576
577fn write_function_pointer_trampoline(
578 out: &mut OutFile,
579 efn: &ExternFn,
580 var: &Ident,
581 f: &Signature,
582 types: &Types,
583) {
584 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700585 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 let indirect_call = true;
587 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
588
589 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700590 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700591 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400592}
593
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700594fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700595 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700596 let indirect_call = false;
597 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
598}
599
600fn write_rust_function_decl_impl(
601 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700602 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 sig: &Signature,
604 types: &Types,
605 indirect_call: bool,
606) {
607 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700608 write!(out, "::rust::Str::Repr ");
609 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700610 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700611 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 write!(out, "{}(", link_name);
613 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700614 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700615 if receiver.mutability.is_none() {
616 write!(out, "const ");
617 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700618 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700619 needs_comma = true;
620 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700621 for arg in &sig.args {
622 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400623 write!(out, ", ");
624 }
625 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700626 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400627 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 if indirect_return(sig, types) {
629 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400630 write!(out, ", ");
631 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700632 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400633 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700634 needs_comma = true;
635 }
636 if indirect_call {
637 if needs_comma {
638 write!(out, ", ");
639 }
640 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400641 }
642 writeln!(out, ") noexcept;");
643}
644
645fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400646 for line in efn.doc.to_string().lines() {
647 writeln!(out, "//{}", line);
648 }
David Tolnaya73853b2020-04-20 01:19:56 -0700649 let local_name = match &efn.sig.receiver {
650 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700651 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700652 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700653 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700654 let indirect_call = false;
655 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
656}
657
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700658fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700659 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700660 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700661 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700662 indirect_call: bool,
663) {
664 write_return_type(out, &sig.ret);
665 write!(out, "{}(", local_name);
666 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400667 if i > 0 {
668 write!(out, ", ");
669 }
670 write_type_space(out, &arg.ty);
671 write!(out, "{}", arg.ident);
672 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700673 if indirect_call {
674 if !sig.args.is_empty() {
675 write!(out, ", ");
676 }
677 write!(out, "void *extern$");
678 }
David Tolnay1e548172020-03-16 13:37:09 -0700679 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700680 if let Some(receiver) = &sig.receiver {
681 if receiver.mutability.is_none() {
682 write!(out, " const");
683 }
684 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700685 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700686 write!(out, " noexcept");
687 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700688}
689
690fn write_rust_function_shim_impl(
691 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700692 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700693 sig: &Signature,
694 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700695 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700696 indirect_call: bool,
697) {
698 if out.header && sig.receiver.is_some() {
699 // We've already defined this inside the struct.
700 return;
701 }
David Tolnaya73853b2020-04-20 01:19:56 -0700702 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400703 if out.header {
704 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700705 return;
David Tolnay7db73692019-10-20 14:51:12 -0400706 }
David Tolnay439cde22020-04-20 00:46:25 -0700707 writeln!(out, " {{");
708 for arg in &sig.args {
709 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
710 out.include.utility = true;
711 write!(out, " ::rust::ManuallyDrop<");
712 write_type(out, &arg.ty);
713 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
714 }
715 }
716 write!(out, " ");
717 let indirect_return = indirect_return(sig, types);
718 if indirect_return {
719 write!(out, "::rust::MaybeUninit<");
720 write_type(out, sig.ret.as_ref().unwrap());
721 writeln!(out, "> return$;");
722 write!(out, " ");
723 } else if let Some(ret) = &sig.ret {
724 write!(out, "return ");
725 match ret {
726 Type::RustBox(_) => {
727 write_type(out, ret);
728 write!(out, "::from_raw(");
729 }
730 Type::UniquePtr(_) => {
731 write_type(out, ret);
732 write!(out, "(");
733 }
734 Type::Ref(_) => write!(out, "*"),
735 _ => {}
736 }
737 }
738 if sig.throws {
739 write!(out, "::rust::Str::Repr error$ = ");
740 }
741 write!(out, "{}(", invoke);
742 if sig.receiver.is_some() {
743 write!(out, "*this");
744 }
745 for (i, arg) in sig.args.iter().enumerate() {
746 if i > 0 || sig.receiver.is_some() {
747 write!(out, ", ");
748 }
749 match &arg.ty {
750 Type::Str(_) => write!(out, "::rust::Str::Repr("),
751 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
752 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
753 _ => {}
754 }
755 write!(out, "{}", arg.ident);
756 match &arg.ty {
757 Type::RustBox(_) => write!(out, ".into_raw()"),
758 Type::UniquePtr(_) => write!(out, ".release()"),
759 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
760 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
761 _ => {}
762 }
763 }
764 if indirect_return {
765 if !sig.args.is_empty() {
766 write!(out, ", ");
767 }
768 write!(out, "&return$.value");
769 }
770 if indirect_call {
771 if !sig.args.is_empty() || indirect_return {
772 write!(out, ", ");
773 }
774 write!(out, "extern$");
775 }
776 write!(out, ")");
777 if let Some(ret) = &sig.ret {
778 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
779 write!(out, ")");
780 }
781 }
782 writeln!(out, ";");
783 if sig.throws {
784 writeln!(out, " if (error$.ptr) {{");
785 writeln!(out, " throw ::rust::Error(error$);");
786 writeln!(out, " }}");
787 }
788 if indirect_return {
789 out.include.utility = true;
790 writeln!(out, " return ::std::move(return$.value);");
791 }
792 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400793}
794
795fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
796 match ty {
797 None => write!(out, "void "),
798 Some(ty) => write_type_space(out, ty),
799 }
800}
801
David Tolnay75dca2e2020-03-25 20:17:52 -0700802fn indirect_return(sig: &Signature, types: &Types) -> bool {
803 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700804 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700805 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700806}
807
David Tolnay99642622020-03-25 13:07:35 -0700808fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
809 match ty {
810 Type::RustBox(ty) | Type::UniquePtr(ty) => {
811 write_type_space(out, &ty.inner);
812 write!(out, "*");
813 }
814 Type::Ref(ty) => {
815 if ty.mutability.is_none() {
816 write!(out, "const ");
817 }
818 write_type(out, &ty.inner);
819 write!(out, " *");
820 }
821 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700822 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700823 _ => write_type(out, ty),
824 }
825}
826
827fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
828 write_indirect_return_type(out, ty);
829 match ty {
830 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700831 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700832 _ => write_space_after_type(out, ty),
833 }
834}
835
836fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400837 match ty {
838 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
839 write_type_space(out, &ty.inner);
840 write!(out, "*");
841 }
David Tolnay4a441222020-01-25 16:24:27 -0800842 Some(Type::Ref(ty)) => {
843 if ty.mutability.is_none() {
844 write!(out, "const ");
845 }
846 write_type(out, &ty.inner);
847 write!(out, " *");
848 }
David Tolnay750755e2020-03-01 13:04:08 -0800849 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700850 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400851 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
852 _ => write_return_type(out, ty),
853 }
854}
855
856fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
857 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700858 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400859 write_type_space(out, &ty.inner);
860 write!(out, "*");
861 }
David Tolnay750755e2020-03-01 13:04:08 -0800862 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700863 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400864 _ => write_type_space(out, &arg.ty),
865 }
866 if types.needs_indirect_abi(&arg.ty) {
867 write!(out, "*");
868 }
869 write!(out, "{}", arg.ident);
870}
871
872fn write_type(out: &mut OutFile, ty: &Type) {
873 match ty {
874 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700875 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400876 None => write!(out, "{}", ident),
877 },
878 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800879 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400880 write_type(out, &ty.inner);
881 write!(out, ">");
882 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700883 Type::RustVec(ty) => {
884 write!(out, "::rust::Vec<");
885 write_type(out, &ty.inner);
886 write!(out, ">");
887 }
David Tolnay7db73692019-10-20 14:51:12 -0400888 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800889 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400890 write_type(out, &ptr.inner);
891 write!(out, ">");
892 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700893 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700894 write!(out, "::std::vector<");
895 write_type(out, &ty.inner);
896 write!(out, ">");
897 }
David Tolnay7db73692019-10-20 14:51:12 -0400898 Type::Ref(r) => {
899 if r.mutability.is_none() {
900 write!(out, "const ");
901 }
902 write_type(out, &r.inner);
903 write!(out, " &");
904 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700905 Type::Slice(_) => {
906 // For now, only U8 slices are supported, which are covered separately below
907 unreachable!()
908 }
David Tolnay7db73692019-10-20 14:51:12 -0400909 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800910 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400911 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700912 Type::SliceRefU8(_) => {
913 write!(out, "::rust::Slice<uint8_t>");
914 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700915 Type::Fn(f) => {
916 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
917 match &f.ret {
918 Some(ret) => write_type(out, ret),
919 None => write!(out, "void"),
920 }
921 write!(out, "(");
922 for (i, arg) in f.args.iter().enumerate() {
923 if i > 0 {
924 write!(out, ", ");
925 }
926 write_type(out, &arg.ty);
927 }
928 write!(out, ")>");
929 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700930 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400931 }
932}
933
David Tolnayf6a89f22020-05-10 23:39:27 -0700934fn write_atom(out: &mut OutFile, atom: Atom) {
935 match atom {
936 Bool => write!(out, "bool"),
937 U8 => write!(out, "uint8_t"),
938 U16 => write!(out, "uint16_t"),
939 U32 => write!(out, "uint32_t"),
940 U64 => write!(out, "uint64_t"),
941 Usize => write!(out, "size_t"),
942 I8 => write!(out, "int8_t"),
943 I16 => write!(out, "int16_t"),
944 I32 => write!(out, "int32_t"),
945 I64 => write!(out, "int64_t"),
946 Isize => write!(out, "::rust::isize"),
947 F32 => write!(out, "float"),
948 F64 => write!(out, "double"),
949 CxxString => write!(out, "::std::string"),
950 RustString => write!(out, "::rust::String"),
951 }
952}
953
David Tolnay7db73692019-10-20 14:51:12 -0400954fn write_type_space(out: &mut OutFile, ty: &Type) {
955 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700956 write_space_after_type(out, ty);
957}
958
959fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400960 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700961 Type::Ident(_)
962 | Type::RustBox(_)
963 | Type::UniquePtr(_)
964 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700965 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700966 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700967 | Type::SliceRefU8(_)
968 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400969 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700970 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400971 }
972}
973
David Tolnaycd08c442020-04-25 10:16:33 -0700974// Only called for legal referent types of unique_ptr and element types of
975// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700976fn to_typename(namespace: &Namespace, ty: &Type) -> String {
977 match ty {
978 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700979 let mut path = String::new();
980 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700981 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700982 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700983 }
David Tolnaycd08c442020-04-25 10:16:33 -0700984 path += &ident.to_string();
985 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700986 }
987 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700988 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700989 }
990}
991
David Tolnayacdf20a2020-04-25 12:40:53 -0700992// Only called for legal referent types of unique_ptr and element types of
993// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700994fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
995 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700996 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700997 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700998 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700999 }
1000}
1001
David Tolnay7db73692019-10-20 14:51:12 -04001002fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001003 out.begin_block("extern \"C\"");
1004 for ty in types {
1005 if let Type::RustBox(ty) = ty {
1006 if let Type::Ident(inner) = &ty.inner {
1007 out.next_section();
1008 write_rust_box_extern(out, inner);
1009 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001010 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001011 if let Type::Ident(inner) = &ty.inner {
1012 if Atom::from(inner).is_none() {
1013 out.next_section();
1014 write_rust_vec_extern(out, inner);
1015 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001016 }
David Tolnay7db73692019-10-20 14:51:12 -04001017 } else if let Type::UniquePtr(ptr) = ty {
1018 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001019 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001020 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001021 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001022 }
1023 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001024 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001025 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001026 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001027 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001028 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001029 }
1030 }
1031 }
1032 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001033 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001034
David Tolnay750755e2020-03-01 13:04:08 -08001035 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001036 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001037 for ty in types {
1038 if let Type::RustBox(ty) = ty {
1039 if let Type::Ident(inner) = &ty.inner {
1040 write_rust_box_impl(out, inner);
1041 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001042 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001043 if let Type::Ident(inner) = &ty.inner {
1044 if Atom::from(inner).is_none() {
1045 write_rust_vec_impl(out, inner);
1046 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001047 }
David Tolnay7db73692019-10-20 14:51:12 -04001048 }
1049 }
David Tolnay591dcb62020-09-01 23:00:38 -07001050 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001051 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001052}
1053
1054fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1055 let mut inner = String::new();
1056 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001057 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001058 inner += "::";
1059 }
1060 inner += &ident.to_string();
1061 let instance = inner.replace("::", "$");
1062
David Tolnay591dcb62020-09-01 23:00:38 -07001063 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1064 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001065 writeln!(
1066 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001067 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001068 instance, inner,
1069 );
1070 writeln!(
1071 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001072 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001073 instance, inner,
1074 );
David Tolnay591dcb62020-09-01 23:00:38 -07001075 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001076}
1077
David Tolnay6787be62020-04-25 11:01:02 -07001078fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1079 let element = Type::Ident(element.clone());
1080 let inner = to_typename(&out.namespace, &element);
1081 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001082
David Tolnay591dcb62020-09-01 23:00:38 -07001083 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1084 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001085 writeln!(
1086 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001087 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001088 instance, inner,
1089 );
1090 writeln!(
1091 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001092 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001093 instance, inner,
1094 );
1095 writeln!(
1096 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001097 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001098 instance, inner,
1099 );
David Tolnay219c0792020-04-24 20:31:37 -07001100 writeln!(
1101 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001102 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001103 inner, instance,
1104 );
David Tolnay503d0192020-04-24 22:18:56 -07001105 writeln!(
1106 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001107 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001108 instance,
1109 );
David Tolnay591dcb62020-09-01 23:00:38 -07001110 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001111}
1112
David Tolnay7db73692019-10-20 14:51:12 -04001113fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1114 let mut inner = String::new();
1115 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001116 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001117 inner += "::";
1118 }
1119 inner += &ident.to_string();
1120 let instance = inner.replace("::", "$");
1121
1122 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001123 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001124 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001125 writeln!(out, "}}");
1126
1127 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001128 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001129 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001130 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001131}
1132
David Tolnay6787be62020-04-25 11:01:02 -07001133fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1134 let element = Type::Ident(element.clone());
1135 let inner = to_typename(&out.namespace, &element);
1136 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001137
Myron Ahneba35cf2020-02-05 19:41:51 +07001138 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001139 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001140 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001141 writeln!(out, "}}");
1142
1143 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001144 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1145 writeln!(
1146 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001147 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001148 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001149 );
1150 writeln!(out, "}}");
1151
1152 writeln!(out, "template <>");
1153 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001154 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001155 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001156
1157 writeln!(out, "template <>");
1158 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1159 writeln!(
1160 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001161 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001162 instance,
1163 );
1164 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001165
1166 writeln!(out, "template <>");
1167 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001168 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001169 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001170}
1171
David Tolnay63da4d32020-04-25 09:41:12 -07001172fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1173 let ty = Type::Ident(ident.clone());
1174 let instance = to_mangled(&out.namespace, &ty);
1175
David Tolnay591dcb62020-09-01 23:00:38 -07001176 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1177 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001178
1179 write_unique_ptr_common(out, &ty, types);
1180
David Tolnay591dcb62020-09-01 23:00:38 -07001181 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001182}
1183
1184// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1185fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001186 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001187 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001188 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001189 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001190
David Tolnay63da4d32020-04-25 09:41:12 -07001191 let can_construct_from_value = match ty {
1192 Type::Ident(ident) => types.structs.contains_key(ident),
1193 _ => false,
1194 };
1195
David Tolnay7db73692019-10-20 14:51:12 -04001196 writeln!(
1197 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001198 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001199 inner,
1200 );
1201 writeln!(
1202 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001203 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001204 inner,
1205 );
1206 writeln!(
1207 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001208 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001209 instance, inner,
1210 );
David Tolnay7e219b82020-03-01 13:14:51 -08001211 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001212 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001213 if can_construct_from_value {
1214 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001215 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001216 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001217 instance, inner, inner,
1218 );
David Tolnay63da4d32020-04-25 09:41:12 -07001219 writeln!(
1220 out,
1221 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1222 inner, inner,
1223 );
1224 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001225 }
David Tolnay7db73692019-10-20 14:51:12 -04001226 writeln!(
1227 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001228 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001229 instance, inner, inner,
1230 );
David Tolnay7e219b82020-03-01 13:14:51 -08001231 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001232 writeln!(out, "}}");
1233 writeln!(
1234 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001235 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001236 inner, instance, inner,
1237 );
1238 writeln!(out, " return ptr.get();");
1239 writeln!(out, "}}");
1240 writeln!(
1241 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001242 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001243 inner, instance, inner,
1244 );
1245 writeln!(out, " return ptr.release();");
1246 writeln!(out, "}}");
1247 writeln!(
1248 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001249 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001250 instance, inner,
1251 );
1252 writeln!(out, " ptr->~unique_ptr();");
1253 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001254}
Myron Ahneba35cf2020-02-05 19:41:51 +07001255
David Tolnay92105da2020-04-25 11:15:31 -07001256fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001257 let element = Type::Ident(element.clone());
1258 let inner = to_typename(&out.namespace, &element);
1259 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001260
David Tolnay591dcb62020-09-01 23:00:38 -07001261 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1262 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001263 writeln!(
1264 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001265 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001266 instance, inner,
1267 );
1268 writeln!(out, " return s.size();");
1269 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001270 writeln!(
1271 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001272 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001273 inner, instance, inner,
1274 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001275 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001276 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001277
1278 write_unique_ptr_common(out, vector_ty, types);
1279
David Tolnay591dcb62020-09-01 23:00:38 -07001280 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001281}