blob: 4bc633fe4c5d805722d6b5d90881aa62cb9f12e7 [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;
217 needs_rust_error = true;
218 }
219 for arg in &efn.args {
220 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
221 needs_manually_drop = true;
222 break;
223 }
224 }
225 if let Some(ret) = &efn.ret {
226 if types.needs_indirect_abi(ret) {
227 needs_maybe_uninit = true;
228 }
David Tolnayf51447e2020-03-06 14:14:27 -0800229 }
230 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800232 }
233 }
234
David Tolnay750755e2020-03-01 13:04:08 -0800235 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700236 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800237
David Tolnay16ab1462020-09-02 15:10:09 -0700238 if needs_panic
239 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700240 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700241 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700243 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700244 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700246 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700247 || needs_unsafe_bitcopy
248 || needs_manually_drop
249 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700250 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700251 {
David Tolnay736cbca2020-03-11 16:49:18 -0700252 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800253 }
254
David Tolnay16ab1462020-09-02 15:10:09 -0700255 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
256
David Tolnay99a95e62020-09-02 15:05:53 -0700257 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700258 out.next_section();
259 writeln!(out, "struct unsafe_bitcopy_t;");
260 }
261
David Tolnay591dcb62020-09-01 23:00:38 -0700262 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
263 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
264 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
265 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700266 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700267 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
268 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
269 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
270 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800271
272 if needs_manually_drop {
273 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700274 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800275 writeln!(out, "template <typename T>");
276 writeln!(out, "union ManuallyDrop {{");
277 writeln!(out, " T value;");
278 writeln!(
279 out,
280 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
281 );
282 writeln!(out, " ~ManuallyDrop() {{}}");
283 writeln!(out, "}};");
284 }
285
David Tolnay09011c32020-03-06 14:40:28 -0800286 if needs_maybe_uninit {
287 out.next_section();
288 writeln!(out, "template <typename T>");
289 writeln!(out, "union MaybeUninit {{");
290 writeln!(out, " T value;");
291 writeln!(out, " MaybeUninit() {{}}");
292 writeln!(out, " ~MaybeUninit() {{}}");
293 writeln!(out, "}};");
294 }
295
David Tolnay591dcb62020-09-01 23:00:38 -0700296 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700297
David Tolnay5d121442020-03-17 22:14:40 -0700298 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700299 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700300 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700301 out.include.type_traits = true;
302 out.include.utility = true;
303 writeln!(out, "class missing {{}};");
304 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700305 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700306 writeln!(out, "template <typename Try, typename Fail>");
307 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700308 writeln!(
309 out,
David Tolnay04722332020-03-18 11:31:54 -0700310 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700311 );
David Tolnay04722332020-03-18 11:31:54 -0700312 writeln!(out, " missing>::value>::type");
313 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700314 writeln!(out, " func();");
315 writeln!(out, "}} catch (const ::std::exception &e) {{");
316 writeln!(out, " fail(e.what());");
317 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700318 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700319 }
320
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800321 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400322}
323
324fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700325 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700326 writeln!(out, "#ifndef {}", guard);
327 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400328 for line in strct.doc.to_string().lines() {
329 writeln!(out, "//{}", line);
330 }
331 writeln!(out, "struct {} final {{", strct.ident);
332 for field in &strct.fields {
333 write!(out, " ");
334 write_type_space(out, &field.ty);
335 writeln!(out, "{};", field.ident);
336 }
337 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700338 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400339}
340
341fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
342 writeln!(out, "struct {};", ident);
343}
344
David Tolnay8861bee2020-01-20 18:39:24 -0800345fn write_struct_using(out: &mut OutFile, ident: &Ident) {
346 writeln!(out, "using {} = {};", ident, ident);
347}
348
David Tolnayc1fe0052020-04-17 15:15:06 -0700349fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700350 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700351 writeln!(out, "#ifndef {}", guard);
352 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700353 for line in ety.doc.to_string().lines() {
354 writeln!(out, "//{}", line);
355 }
356 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700357 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700358 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700359 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700360 write!(out, " ");
361 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700362 let local_name = method.ident.to_string();
363 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700364 writeln!(out, ";");
365 }
366 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700367 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700368}
369
Joel Galensonc03402a2020-04-23 17:31:09 -0700370fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700371 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700372 writeln!(out, "#ifndef {}", guard);
373 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700374 for line in enm.doc.to_string().lines() {
375 writeln!(out, "//{}", line);
376 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700377 write!(out, "enum class {} : ", enm.ident);
378 write_atom(out, enm.repr);
379 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700380 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700381 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700382 }
383 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700384 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700385}
386
Joel Galenson905eb2e2020-05-04 14:58:14 -0700387fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700388 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
389 write_atom(out, enm.repr);
390 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700391 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700392 write!(out, "static_assert(static_cast<");
393 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700394 writeln!(
395 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700396 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700397 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700398 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700399 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700400}
401
David Tolnayebef4a22020-03-17 15:33:47 -0700402fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
403 let mut has_cxx_throws = false;
404 for api in apis {
405 if let Api::CxxFunction(efn) = api {
406 if efn.throws {
407 has_cxx_throws = true;
408 break;
409 }
410 }
411 }
412
413 if has_cxx_throws {
414 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700415 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700416 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700417 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700418 );
419 }
420}
421
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700422fn write_cxx_function_shim(
423 out: &mut OutFile,
424 efn: &ExternFn,
425 types: &Types,
426 impl_annotations: &Option<String>,
427) {
428 if !out.header {
429 if let Some(annotation) = impl_annotations {
430 write!(out, "{} ", annotation);
431 }
432 }
David Tolnayebef4a22020-03-17 15:33:47 -0700433 if efn.throws {
434 write!(out, "::rust::Str::Repr ");
435 } else {
David Tolnay99642622020-03-25 13:07:35 -0700436 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700437 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700438 let mangled = mangle::extern_fn(&out.namespace, efn);
439 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700440 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700441 if receiver.mutability.is_none() {
442 write!(out, "const ");
443 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700444 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700445 }
David Tolnay7db73692019-10-20 14:51:12 -0400446 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700447 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400448 write!(out, ", ");
449 }
David Tolnaya46a2372020-03-06 10:03:48 -0800450 if arg.ty == RustString {
451 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700452 } else if let Type::RustVec(_) = arg.ty {
453 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800454 }
David Tolnay7db73692019-10-20 14:51:12 -0400455 write_extern_arg(out, arg, types);
456 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700457 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400458 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700459 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400460 write!(out, ", ");
461 }
David Tolnay99642622020-03-25 13:07:35 -0700462 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400463 write!(out, "*return$");
464 }
465 writeln!(out, ") noexcept {{");
466 write!(out, " ");
467 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700468 match &efn.receiver {
469 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700470 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700471 }
David Tolnay7db73692019-10-20 14:51:12 -0400472 for (i, arg) in efn.args.iter().enumerate() {
473 if i > 0 {
474 write!(out, ", ");
475 }
476 write_type(out, &arg.ty);
477 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700478 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700479 if let Some(receiver) = &efn.receiver {
480 if receiver.mutability.is_none() {
481 write!(out, " const");
482 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700483 }
484 write!(out, " = ");
485 match &efn.receiver {
486 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700487 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700488 }
489 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400490 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700491 if efn.throws {
492 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700493 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700494 writeln!(out, " [&] {{");
495 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700496 }
David Tolnay7db73692019-10-20 14:51:12 -0400497 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700498 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400499 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700500 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400501 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700502 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400503 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700504 }
505 match &efn.ret {
506 Some(Type::Ref(_)) => write!(out, "&"),
507 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700508 Some(Type::SliceRefU8(_)) if !indirect_return => {
509 write!(out, "::rust::Slice<uint8_t>::Repr(")
510 }
David Tolnay99642622020-03-25 13:07:35 -0700511 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400512 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700513 match &efn.receiver {
514 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700515 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700516 }
David Tolnay7db73692019-10-20 14:51:12 -0400517 for (i, arg) in efn.args.iter().enumerate() {
518 if i > 0 {
519 write!(out, ", ");
520 }
521 if let Type::RustBox(_) = &arg.ty {
522 write_type(out, &arg.ty);
523 write!(out, "::from_raw({})", arg.ident);
524 } else if let Type::UniquePtr(_) = &arg.ty {
525 write_type(out, &arg.ty);
526 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800527 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800528 write!(
529 out,
530 "::rust::String(::rust::unsafe_bitcopy, *{})",
531 arg.ident,
532 );
David Tolnay313b10e2020-04-25 16:30:51 -0700533 } else if let Type::RustVec(_) = arg.ty {
534 write_type(out, &arg.ty);
535 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400536 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700537 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800538 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400539 } else {
540 write!(out, "{}", arg.ident);
541 }
542 }
543 write!(out, ")");
544 match &efn.ret {
545 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
546 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700547 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400548 _ => {}
549 }
550 if indirect_return {
551 write!(out, ")");
552 }
553 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700554 if efn.throws {
555 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700556 writeln!(out, " throw$.ptr = nullptr;");
557 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700558 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700559 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700560 writeln!(
561 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700562 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700563 );
David Tolnay5d121442020-03-17 22:14:40 -0700564 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700565 writeln!(out, " return throw$;");
566 }
David Tolnay7db73692019-10-20 14:51:12 -0400567 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700568 for arg in &efn.args {
569 if let Type::Fn(f) = &arg.ty {
570 let var = &arg.ident;
571 write_function_pointer_trampoline(out, efn, var, f, types);
572 }
573 }
574}
575
576fn write_function_pointer_trampoline(
577 out: &mut OutFile,
578 efn: &ExternFn,
579 var: &Ident,
580 f: &Signature,
581 types: &Types,
582) {
583 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700584 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700585 let indirect_call = true;
586 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
587
588 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700589 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700590 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400591}
592
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700593fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700594 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700595 let indirect_call = false;
596 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
597}
598
599fn write_rust_function_decl_impl(
600 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700601 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700602 sig: &Signature,
603 types: &Types,
604 indirect_call: bool,
605) {
606 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700607 write!(out, "::rust::Str::Repr ");
608 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700609 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700610 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700611 write!(out, "{}(", link_name);
612 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700613 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700614 if receiver.mutability.is_none() {
615 write!(out, "const ");
616 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700617 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700618 needs_comma = true;
619 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700620 for arg in &sig.args {
621 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400622 write!(out, ", ");
623 }
624 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700625 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400626 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 if indirect_return(sig, types) {
628 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400629 write!(out, ", ");
630 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700631 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400632 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700633 needs_comma = true;
634 }
635 if indirect_call {
636 if needs_comma {
637 write!(out, ", ");
638 }
639 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400640 }
641 writeln!(out, ") noexcept;");
642}
643
644fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400645 for line in efn.doc.to_string().lines() {
646 writeln!(out, "//{}", line);
647 }
David Tolnaya73853b2020-04-20 01:19:56 -0700648 let local_name = match &efn.sig.receiver {
649 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700650 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700651 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700652 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 let indirect_call = false;
654 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
655}
656
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700657fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700658 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700659 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700660 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700661 indirect_call: bool,
662) {
663 write_return_type(out, &sig.ret);
664 write!(out, "{}(", local_name);
665 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400666 if i > 0 {
667 write!(out, ", ");
668 }
669 write_type_space(out, &arg.ty);
670 write!(out, "{}", arg.ident);
671 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700672 if indirect_call {
673 if !sig.args.is_empty() {
674 write!(out, ", ");
675 }
676 write!(out, "void *extern$");
677 }
David Tolnay1e548172020-03-16 13:37:09 -0700678 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700679 if let Some(receiver) = &sig.receiver {
680 if receiver.mutability.is_none() {
681 write!(out, " const");
682 }
683 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700684 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700685 write!(out, " noexcept");
686 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700687}
688
689fn write_rust_function_shim_impl(
690 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700691 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700692 sig: &Signature,
693 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700694 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700695 indirect_call: bool,
696) {
697 if out.header && sig.receiver.is_some() {
698 // We've already defined this inside the struct.
699 return;
700 }
David Tolnaya73853b2020-04-20 01:19:56 -0700701 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400702 if out.header {
703 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700704 return;
David Tolnay7db73692019-10-20 14:51:12 -0400705 }
David Tolnay439cde22020-04-20 00:46:25 -0700706 writeln!(out, " {{");
707 for arg in &sig.args {
708 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
709 out.include.utility = true;
710 write!(out, " ::rust::ManuallyDrop<");
711 write_type(out, &arg.ty);
712 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
713 }
714 }
715 write!(out, " ");
716 let indirect_return = indirect_return(sig, types);
717 if indirect_return {
718 write!(out, "::rust::MaybeUninit<");
719 write_type(out, sig.ret.as_ref().unwrap());
720 writeln!(out, "> return$;");
721 write!(out, " ");
722 } else if let Some(ret) = &sig.ret {
723 write!(out, "return ");
724 match ret {
725 Type::RustBox(_) => {
726 write_type(out, ret);
727 write!(out, "::from_raw(");
728 }
729 Type::UniquePtr(_) => {
730 write_type(out, ret);
731 write!(out, "(");
732 }
733 Type::Ref(_) => write!(out, "*"),
734 _ => {}
735 }
736 }
737 if sig.throws {
738 write!(out, "::rust::Str::Repr error$ = ");
739 }
740 write!(out, "{}(", invoke);
741 if sig.receiver.is_some() {
742 write!(out, "*this");
743 }
744 for (i, arg) in sig.args.iter().enumerate() {
745 if i > 0 || sig.receiver.is_some() {
746 write!(out, ", ");
747 }
748 match &arg.ty {
749 Type::Str(_) => write!(out, "::rust::Str::Repr("),
750 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
751 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
752 _ => {}
753 }
754 write!(out, "{}", arg.ident);
755 match &arg.ty {
756 Type::RustBox(_) => write!(out, ".into_raw()"),
757 Type::UniquePtr(_) => write!(out, ".release()"),
758 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
759 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
760 _ => {}
761 }
762 }
763 if indirect_return {
764 if !sig.args.is_empty() {
765 write!(out, ", ");
766 }
767 write!(out, "&return$.value");
768 }
769 if indirect_call {
770 if !sig.args.is_empty() || indirect_return {
771 write!(out, ", ");
772 }
773 write!(out, "extern$");
774 }
775 write!(out, ")");
776 if let Some(ret) = &sig.ret {
777 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
778 write!(out, ")");
779 }
780 }
781 writeln!(out, ";");
782 if sig.throws {
783 writeln!(out, " if (error$.ptr) {{");
784 writeln!(out, " throw ::rust::Error(error$);");
785 writeln!(out, " }}");
786 }
787 if indirect_return {
788 out.include.utility = true;
789 writeln!(out, " return ::std::move(return$.value);");
790 }
791 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400792}
793
794fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
795 match ty {
796 None => write!(out, "void "),
797 Some(ty) => write_type_space(out, ty),
798 }
799}
800
David Tolnay75dca2e2020-03-25 20:17:52 -0700801fn indirect_return(sig: &Signature, types: &Types) -> bool {
802 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700803 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700804 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700805}
806
David Tolnay99642622020-03-25 13:07:35 -0700807fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
808 match ty {
809 Type::RustBox(ty) | Type::UniquePtr(ty) => {
810 write_type_space(out, &ty.inner);
811 write!(out, "*");
812 }
813 Type::Ref(ty) => {
814 if ty.mutability.is_none() {
815 write!(out, "const ");
816 }
817 write_type(out, &ty.inner);
818 write!(out, " *");
819 }
820 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700821 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700822 _ => write_type(out, ty),
823 }
824}
825
826fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
827 write_indirect_return_type(out, ty);
828 match ty {
829 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700830 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700831 _ => write_space_after_type(out, ty),
832 }
833}
834
835fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400836 match ty {
837 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
838 write_type_space(out, &ty.inner);
839 write!(out, "*");
840 }
David Tolnay4a441222020-01-25 16:24:27 -0800841 Some(Type::Ref(ty)) => {
842 if ty.mutability.is_none() {
843 write!(out, "const ");
844 }
845 write_type(out, &ty.inner);
846 write!(out, " *");
847 }
David Tolnay750755e2020-03-01 13:04:08 -0800848 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700849 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400850 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
851 _ => write_return_type(out, ty),
852 }
853}
854
855fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
856 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700857 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400858 write_type_space(out, &ty.inner);
859 write!(out, "*");
860 }
David Tolnay750755e2020-03-01 13:04:08 -0800861 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700862 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400863 _ => write_type_space(out, &arg.ty),
864 }
865 if types.needs_indirect_abi(&arg.ty) {
866 write!(out, "*");
867 }
868 write!(out, "{}", arg.ident);
869}
870
871fn write_type(out: &mut OutFile, ty: &Type) {
872 match ty {
873 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700874 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400875 None => write!(out, "{}", ident),
876 },
877 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800878 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400879 write_type(out, &ty.inner);
880 write!(out, ">");
881 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700882 Type::RustVec(ty) => {
883 write!(out, "::rust::Vec<");
884 write_type(out, &ty.inner);
885 write!(out, ">");
886 }
David Tolnay7db73692019-10-20 14:51:12 -0400887 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800888 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400889 write_type(out, &ptr.inner);
890 write!(out, ">");
891 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700892 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700893 write!(out, "::std::vector<");
894 write_type(out, &ty.inner);
895 write!(out, ">");
896 }
David Tolnay7db73692019-10-20 14:51:12 -0400897 Type::Ref(r) => {
898 if r.mutability.is_none() {
899 write!(out, "const ");
900 }
901 write_type(out, &r.inner);
902 write!(out, " &");
903 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700904 Type::Slice(_) => {
905 // For now, only U8 slices are supported, which are covered separately below
906 unreachable!()
907 }
David Tolnay7db73692019-10-20 14:51:12 -0400908 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800909 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400910 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700911 Type::SliceRefU8(_) => {
912 write!(out, "::rust::Slice<uint8_t>");
913 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700914 Type::Fn(f) => {
915 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
916 match &f.ret {
917 Some(ret) => write_type(out, ret),
918 None => write!(out, "void"),
919 }
920 write!(out, "(");
921 for (i, arg) in f.args.iter().enumerate() {
922 if i > 0 {
923 write!(out, ", ");
924 }
925 write_type(out, &arg.ty);
926 }
927 write!(out, ")>");
928 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700929 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400930 }
931}
932
David Tolnayf6a89f22020-05-10 23:39:27 -0700933fn write_atom(out: &mut OutFile, atom: Atom) {
934 match atom {
935 Bool => write!(out, "bool"),
936 U8 => write!(out, "uint8_t"),
937 U16 => write!(out, "uint16_t"),
938 U32 => write!(out, "uint32_t"),
939 U64 => write!(out, "uint64_t"),
940 Usize => write!(out, "size_t"),
941 I8 => write!(out, "int8_t"),
942 I16 => write!(out, "int16_t"),
943 I32 => write!(out, "int32_t"),
944 I64 => write!(out, "int64_t"),
945 Isize => write!(out, "::rust::isize"),
946 F32 => write!(out, "float"),
947 F64 => write!(out, "double"),
948 CxxString => write!(out, "::std::string"),
949 RustString => write!(out, "::rust::String"),
950 }
951}
952
David Tolnay7db73692019-10-20 14:51:12 -0400953fn write_type_space(out: &mut OutFile, ty: &Type) {
954 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700955 write_space_after_type(out, ty);
956}
957
958fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400959 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700960 Type::Ident(_)
961 | Type::RustBox(_)
962 | Type::UniquePtr(_)
963 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700964 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700965 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700966 | Type::SliceRefU8(_)
967 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400968 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700969 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400970 }
971}
972
David Tolnaycd08c442020-04-25 10:16:33 -0700973// Only called for legal referent types of unique_ptr and element types of
974// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700975fn to_typename(namespace: &Namespace, ty: &Type) -> String {
976 match ty {
977 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700978 let mut path = String::new();
979 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700980 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700981 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700982 }
David Tolnaycd08c442020-04-25 10:16:33 -0700983 path += &ident.to_string();
984 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700985 }
986 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700987 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700988 }
989}
990
David Tolnayacdf20a2020-04-25 12:40:53 -0700991// Only called for legal referent types of unique_ptr and element types of
992// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700993fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
994 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700995 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700996 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700997 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700998 }
999}
1000
David Tolnay7db73692019-10-20 14:51:12 -04001001fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001002 out.begin_block("extern \"C\"");
1003 for ty in types {
1004 if let Type::RustBox(ty) = ty {
1005 if let Type::Ident(inner) = &ty.inner {
1006 out.next_section();
1007 write_rust_box_extern(out, inner);
1008 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001009 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001010 if let Type::Ident(inner) = &ty.inner {
1011 if Atom::from(inner).is_none() {
1012 out.next_section();
1013 write_rust_vec_extern(out, inner);
1014 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001015 }
David Tolnay7db73692019-10-20 14:51:12 -04001016 } else if let Type::UniquePtr(ptr) = ty {
1017 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001018 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001019 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001020 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001021 }
1022 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001023 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001024 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001025 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001026 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001027 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001028 }
1029 }
1030 }
1031 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001032 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001033
David Tolnay750755e2020-03-01 13:04:08 -08001034 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001035 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001036 for ty in types {
1037 if let Type::RustBox(ty) = ty {
1038 if let Type::Ident(inner) = &ty.inner {
1039 write_rust_box_impl(out, inner);
1040 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001041 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001042 if let Type::Ident(inner) = &ty.inner {
1043 if Atom::from(inner).is_none() {
1044 write_rust_vec_impl(out, inner);
1045 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001046 }
David Tolnay7db73692019-10-20 14:51:12 -04001047 }
1048 }
David Tolnay591dcb62020-09-01 23:00:38 -07001049 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001050 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001051}
1052
1053fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1054 let mut inner = String::new();
1055 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001056 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001057 inner += "::";
1058 }
1059 inner += &ident.to_string();
1060 let instance = inner.replace("::", "$");
1061
David Tolnay591dcb62020-09-01 23:00:38 -07001062 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1063 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001064 writeln!(
1065 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001066 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001067 instance, inner,
1068 );
1069 writeln!(
1070 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001071 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001072 instance, inner,
1073 );
David Tolnay591dcb62020-09-01 23:00:38 -07001074 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001075}
1076
David Tolnay6787be62020-04-25 11:01:02 -07001077fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1078 let element = Type::Ident(element.clone());
1079 let inner = to_typename(&out.namespace, &element);
1080 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001081
David Tolnay591dcb62020-09-01 23:00:38 -07001082 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1083 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001084 writeln!(
1085 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001086 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001087 instance, inner,
1088 );
1089 writeln!(
1090 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001091 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001092 instance, inner,
1093 );
1094 writeln!(
1095 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001096 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001097 instance, inner,
1098 );
David Tolnay219c0792020-04-24 20:31:37 -07001099 writeln!(
1100 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001101 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001102 inner, instance,
1103 );
David Tolnay503d0192020-04-24 22:18:56 -07001104 writeln!(
1105 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001106 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001107 instance,
1108 );
David Tolnay591dcb62020-09-01 23:00:38 -07001109 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001110}
1111
David Tolnay7db73692019-10-20 14:51:12 -04001112fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1113 let mut inner = String::new();
1114 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001115 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001116 inner += "::";
1117 }
1118 inner += &ident.to_string();
1119 let instance = inner.replace("::", "$");
1120
1121 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001122 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001123 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001124 writeln!(out, "}}");
1125
1126 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001127 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001128 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001129 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001130}
1131
David Tolnay6787be62020-04-25 11:01:02 -07001132fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1133 let element = Type::Ident(element.clone());
1134 let inner = to_typename(&out.namespace, &element);
1135 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001136
Myron Ahneba35cf2020-02-05 19:41:51 +07001137 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001138 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001139 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001140 writeln!(out, "}}");
1141
1142 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001143 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1144 writeln!(
1145 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001146 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001147 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001148 );
1149 writeln!(out, "}}");
1150
1151 writeln!(out, "template <>");
1152 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001153 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001154 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001155
1156 writeln!(out, "template <>");
1157 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1158 writeln!(
1159 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001160 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001161 instance,
1162 );
1163 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001164
1165 writeln!(out, "template <>");
1166 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001167 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001168 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001169}
1170
David Tolnay63da4d32020-04-25 09:41:12 -07001171fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1172 let ty = Type::Ident(ident.clone());
1173 let instance = to_mangled(&out.namespace, &ty);
1174
David Tolnay591dcb62020-09-01 23:00:38 -07001175 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1176 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001177
1178 write_unique_ptr_common(out, &ty, types);
1179
David Tolnay591dcb62020-09-01 23:00:38 -07001180 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001181}
1182
1183// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1184fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001185 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001186 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001187 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001188 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001189
David Tolnay63da4d32020-04-25 09:41:12 -07001190 let can_construct_from_value = match ty {
1191 Type::Ident(ident) => types.structs.contains_key(ident),
1192 _ => false,
1193 };
1194
David Tolnay7db73692019-10-20 14:51:12 -04001195 writeln!(
1196 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001197 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001198 inner,
1199 );
1200 writeln!(
1201 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001202 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001203 inner,
1204 );
1205 writeln!(
1206 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001207 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001208 instance, inner,
1209 );
David Tolnay7e219b82020-03-01 13:14:51 -08001210 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001211 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001212 if can_construct_from_value {
1213 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001214 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001215 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001216 instance, inner, inner,
1217 );
David Tolnay63da4d32020-04-25 09:41:12 -07001218 writeln!(
1219 out,
1220 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1221 inner, inner,
1222 );
1223 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001224 }
David Tolnay7db73692019-10-20 14:51:12 -04001225 writeln!(
1226 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001227 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001228 instance, inner, inner,
1229 );
David Tolnay7e219b82020-03-01 13:14:51 -08001230 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001231 writeln!(out, "}}");
1232 writeln!(
1233 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001234 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001235 inner, instance, inner,
1236 );
1237 writeln!(out, " return ptr.get();");
1238 writeln!(out, "}}");
1239 writeln!(
1240 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001241 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001242 inner, instance, inner,
1243 );
1244 writeln!(out, " return ptr.release();");
1245 writeln!(out, "}}");
1246 writeln!(
1247 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001248 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001249 instance, inner,
1250 );
1251 writeln!(out, " ptr->~unique_ptr();");
1252 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001253}
Myron Ahneba35cf2020-02-05 19:41:51 +07001254
David Tolnay92105da2020-04-25 11:15:31 -07001255fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001256 let element = Type::Ident(element.clone());
1257 let inner = to_typename(&out.namespace, &element);
1258 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001259
David Tolnay591dcb62020-09-01 23:00:38 -07001260 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1261 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001262 writeln!(
1263 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001264 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001265 instance, inner,
1266 );
1267 writeln!(out, " return s.size();");
1268 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001269 writeln!(
1270 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001271 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001272 inner, instance, inner,
1273 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001274 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001275 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001276
1277 write_unique_ptr_common(out, vector_ty, types);
1278
David Tolnay591dcb62020-09-01 23:00:38 -07001279 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001280}