blob: 68d87753ff8f4d94ec1f08ced28e4f48a1751f1b [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
250 {
David Tolnay736cbca2020-03-11 16:49:18 -0700251 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800252 }
253
David Tolnay16ab1462020-09-02 15:10:09 -0700254 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
255
David Tolnay99a95e62020-09-02 15:05:53 -0700256 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700257 out.next_section();
258 writeln!(out, "struct unsafe_bitcopy_t;");
259 }
260
David Tolnay591dcb62020-09-01 23:00:38 -0700261 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
262 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
263 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
264 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700265 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700266 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
267 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
268 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
269 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800270
271 if needs_manually_drop {
272 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700273 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800274 writeln!(out, "template <typename T>");
275 writeln!(out, "union ManuallyDrop {{");
276 writeln!(out, " T value;");
277 writeln!(
278 out,
279 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
280 );
281 writeln!(out, " ~ManuallyDrop() {{}}");
282 writeln!(out, "}};");
283 }
284
David Tolnay09011c32020-03-06 14:40:28 -0800285 if needs_maybe_uninit {
286 out.next_section();
287 writeln!(out, "template <typename T>");
288 writeln!(out, "union MaybeUninit {{");
289 writeln!(out, " T value;");
290 writeln!(out, " MaybeUninit() {{}}");
291 writeln!(out, " ~MaybeUninit() {{}}");
292 writeln!(out, "}};");
293 }
294
David Tolnay591dcb62020-09-01 23:00:38 -0700295 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700296
David Tolnay5d121442020-03-17 22:14:40 -0700297 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700298 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700299 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700300 out.include.type_traits = true;
301 out.include.utility = true;
302 writeln!(out, "class missing {{}};");
303 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700304 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700305 writeln!(out, "template <typename Try, typename Fail>");
306 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700307 writeln!(
308 out,
David Tolnay04722332020-03-18 11:31:54 -0700309 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700310 );
David Tolnay04722332020-03-18 11:31:54 -0700311 writeln!(out, " missing>::value>::type");
312 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700313 writeln!(out, " func();");
314 writeln!(out, "}} catch (const ::std::exception &e) {{");
315 writeln!(out, " fail(e.what());");
316 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700317 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700318 }
319
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800320 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400321}
322
323fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700324 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700325 writeln!(out, "#ifndef {}", guard);
326 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400327 for line in strct.doc.to_string().lines() {
328 writeln!(out, "//{}", line);
329 }
330 writeln!(out, "struct {} final {{", strct.ident);
331 for field in &strct.fields {
332 write!(out, " ");
333 write_type_space(out, &field.ty);
334 writeln!(out, "{};", field.ident);
335 }
336 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700337 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400338}
339
340fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
341 writeln!(out, "struct {};", ident);
342}
343
David Tolnay8861bee2020-01-20 18:39:24 -0800344fn write_struct_using(out: &mut OutFile, ident: &Ident) {
345 writeln!(out, "using {} = {};", ident, ident);
346}
347
David Tolnayc1fe0052020-04-17 15:15:06 -0700348fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700349 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700350 writeln!(out, "#ifndef {}", guard);
351 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700352 for line in ety.doc.to_string().lines() {
353 writeln!(out, "//{}", line);
354 }
355 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700356 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700357 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700358 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700359 write!(out, " ");
360 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700361 let local_name = method.ident.to_string();
362 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700363 writeln!(out, ";");
364 }
365 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700366 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700367}
368
Joel Galensonc03402a2020-04-23 17:31:09 -0700369fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700370 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700371 writeln!(out, "#ifndef {}", guard);
372 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700373 for line in enm.doc.to_string().lines() {
374 writeln!(out, "//{}", line);
375 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700376 write!(out, "enum class {} : ", enm.ident);
377 write_atom(out, enm.repr);
378 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700379 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700380 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700381 }
382 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700383 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700384}
385
Joel Galenson905eb2e2020-05-04 14:58:14 -0700386fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700387 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
388 write_atom(out, enm.repr);
389 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700390 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700391 write!(out, "static_assert(static_cast<");
392 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700393 writeln!(
394 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700395 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700396 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700397 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700398 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700399}
400
David Tolnayebef4a22020-03-17 15:33:47 -0700401fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
402 let mut has_cxx_throws = false;
403 for api in apis {
404 if let Api::CxxFunction(efn) = api {
405 if efn.throws {
406 has_cxx_throws = true;
407 break;
408 }
409 }
410 }
411
412 if has_cxx_throws {
413 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700414 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700415 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700416 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700417 );
418 }
419}
420
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700421fn write_cxx_function_shim(
422 out: &mut OutFile,
423 efn: &ExternFn,
424 types: &Types,
425 impl_annotations: &Option<String>,
426) {
427 if !out.header {
428 if let Some(annotation) = impl_annotations {
429 write!(out, "{} ", annotation);
430 }
431 }
David Tolnayebef4a22020-03-17 15:33:47 -0700432 if efn.throws {
433 write!(out, "::rust::Str::Repr ");
434 } else {
David Tolnay99642622020-03-25 13:07:35 -0700435 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700436 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700437 let mangled = mangle::extern_fn(&out.namespace, efn);
438 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700439 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700440 if receiver.mutability.is_none() {
441 write!(out, "const ");
442 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700443 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700444 }
David Tolnay7db73692019-10-20 14:51:12 -0400445 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700446 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400447 write!(out, ", ");
448 }
David Tolnaya46a2372020-03-06 10:03:48 -0800449 if arg.ty == RustString {
450 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700451 } else if let Type::RustVec(_) = arg.ty {
452 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800453 }
David Tolnay7db73692019-10-20 14:51:12 -0400454 write_extern_arg(out, arg, types);
455 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700456 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400457 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700458 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400459 write!(out, ", ");
460 }
David Tolnay99642622020-03-25 13:07:35 -0700461 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400462 write!(out, "*return$");
463 }
464 writeln!(out, ") noexcept {{");
465 write!(out, " ");
466 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700467 match &efn.receiver {
468 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700469 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700470 }
David Tolnay7db73692019-10-20 14:51:12 -0400471 for (i, arg) in efn.args.iter().enumerate() {
472 if i > 0 {
473 write!(out, ", ");
474 }
475 write_type(out, &arg.ty);
476 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700477 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700478 if let Some(receiver) = &efn.receiver {
479 if receiver.mutability.is_none() {
480 write!(out, " const");
481 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700482 }
483 write!(out, " = ");
484 match &efn.receiver {
485 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700486 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700487 }
488 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400489 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700490 if efn.throws {
491 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700492 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700493 writeln!(out, " [&] {{");
494 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700495 }
David Tolnay7db73692019-10-20 14:51:12 -0400496 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700497 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400498 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700499 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400500 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700501 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400502 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700503 }
504 match &efn.ret {
505 Some(Type::Ref(_)) => write!(out, "&"),
506 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700507 Some(Type::SliceRefU8(_)) if !indirect_return => {
508 write!(out, "::rust::Slice<uint8_t>::Repr(")
509 }
David Tolnay99642622020-03-25 13:07:35 -0700510 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400511 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700512 match &efn.receiver {
513 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700514 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700515 }
David Tolnay7db73692019-10-20 14:51:12 -0400516 for (i, arg) in efn.args.iter().enumerate() {
517 if i > 0 {
518 write!(out, ", ");
519 }
520 if let Type::RustBox(_) = &arg.ty {
521 write_type(out, &arg.ty);
522 write!(out, "::from_raw({})", arg.ident);
523 } else if let Type::UniquePtr(_) = &arg.ty {
524 write_type(out, &arg.ty);
525 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800526 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800527 write!(
528 out,
529 "::rust::String(::rust::unsafe_bitcopy, *{})",
530 arg.ident,
531 );
David Tolnay313b10e2020-04-25 16:30:51 -0700532 } else if let Type::RustVec(_) = arg.ty {
533 write_type(out, &arg.ty);
534 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400535 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700536 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800537 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400538 } else {
539 write!(out, "{}", arg.ident);
540 }
541 }
542 write!(out, ")");
543 match &efn.ret {
544 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
545 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700546 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400547 _ => {}
548 }
549 if indirect_return {
550 write!(out, ")");
551 }
552 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700553 if efn.throws {
554 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700555 writeln!(out, " throw$.ptr = nullptr;");
556 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700557 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700558 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700559 writeln!(
560 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700561 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700562 );
David Tolnay5d121442020-03-17 22:14:40 -0700563 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700564 writeln!(out, " return throw$;");
565 }
David Tolnay7db73692019-10-20 14:51:12 -0400566 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700567 for arg in &efn.args {
568 if let Type::Fn(f) = &arg.ty {
569 let var = &arg.ident;
570 write_function_pointer_trampoline(out, efn, var, f, types);
571 }
572 }
573}
574
575fn write_function_pointer_trampoline(
576 out: &mut OutFile,
577 efn: &ExternFn,
578 var: &Ident,
579 f: &Signature,
580 types: &Types,
581) {
582 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700583 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700584 let indirect_call = true;
585 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
586
587 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700588 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700589 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400590}
591
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700592fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700593 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 let indirect_call = false;
595 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
596}
597
598fn write_rust_function_decl_impl(
599 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700600 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700601 sig: &Signature,
602 types: &Types,
603 indirect_call: bool,
604) {
605 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700606 write!(out, "::rust::Str::Repr ");
607 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700608 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700609 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700610 write!(out, "{}(", link_name);
611 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700612 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700613 if receiver.mutability.is_none() {
614 write!(out, "const ");
615 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700616 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700617 needs_comma = true;
618 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 for arg in &sig.args {
620 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400621 write!(out, ", ");
622 }
623 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700624 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400625 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700626 if indirect_return(sig, types) {
627 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400628 write!(out, ", ");
629 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700630 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400631 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700632 needs_comma = true;
633 }
634 if indirect_call {
635 if needs_comma {
636 write!(out, ", ");
637 }
638 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400639 }
640 writeln!(out, ") noexcept;");
641}
642
643fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400644 for line in efn.doc.to_string().lines() {
645 writeln!(out, "//{}", line);
646 }
David Tolnaya73853b2020-04-20 01:19:56 -0700647 let local_name = match &efn.sig.receiver {
648 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700649 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700650 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700651 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 let indirect_call = false;
653 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
654}
655
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700656fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700657 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700658 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700659 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700660 indirect_call: bool,
661) {
662 write_return_type(out, &sig.ret);
663 write!(out, "{}(", local_name);
664 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400665 if i > 0 {
666 write!(out, ", ");
667 }
668 write_type_space(out, &arg.ty);
669 write!(out, "{}", arg.ident);
670 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700671 if indirect_call {
672 if !sig.args.is_empty() {
673 write!(out, ", ");
674 }
675 write!(out, "void *extern$");
676 }
David Tolnay1e548172020-03-16 13:37:09 -0700677 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700678 if let Some(receiver) = &sig.receiver {
679 if receiver.mutability.is_none() {
680 write!(out, " const");
681 }
682 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700683 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700684 write!(out, " noexcept");
685 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700686}
687
688fn write_rust_function_shim_impl(
689 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700690 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700691 sig: &Signature,
692 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700693 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700694 indirect_call: bool,
695) {
696 if out.header && sig.receiver.is_some() {
697 // We've already defined this inside the struct.
698 return;
699 }
David Tolnaya73853b2020-04-20 01:19:56 -0700700 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400701 if out.header {
702 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700703 return;
David Tolnay7db73692019-10-20 14:51:12 -0400704 }
David Tolnay439cde22020-04-20 00:46:25 -0700705 writeln!(out, " {{");
706 for arg in &sig.args {
707 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
708 out.include.utility = true;
709 write!(out, " ::rust::ManuallyDrop<");
710 write_type(out, &arg.ty);
711 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
712 }
713 }
714 write!(out, " ");
715 let indirect_return = indirect_return(sig, types);
716 if indirect_return {
717 write!(out, "::rust::MaybeUninit<");
718 write_type(out, sig.ret.as_ref().unwrap());
719 writeln!(out, "> return$;");
720 write!(out, " ");
721 } else if let Some(ret) = &sig.ret {
722 write!(out, "return ");
723 match ret {
724 Type::RustBox(_) => {
725 write_type(out, ret);
726 write!(out, "::from_raw(");
727 }
728 Type::UniquePtr(_) => {
729 write_type(out, ret);
730 write!(out, "(");
731 }
732 Type::Ref(_) => write!(out, "*"),
733 _ => {}
734 }
735 }
736 if sig.throws {
737 write!(out, "::rust::Str::Repr error$ = ");
738 }
739 write!(out, "{}(", invoke);
740 if sig.receiver.is_some() {
741 write!(out, "*this");
742 }
743 for (i, arg) in sig.args.iter().enumerate() {
744 if i > 0 || sig.receiver.is_some() {
745 write!(out, ", ");
746 }
747 match &arg.ty {
748 Type::Str(_) => write!(out, "::rust::Str::Repr("),
749 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
750 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
751 _ => {}
752 }
753 write!(out, "{}", arg.ident);
754 match &arg.ty {
755 Type::RustBox(_) => write!(out, ".into_raw()"),
756 Type::UniquePtr(_) => write!(out, ".release()"),
757 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
758 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
759 _ => {}
760 }
761 }
762 if indirect_return {
763 if !sig.args.is_empty() {
764 write!(out, ", ");
765 }
766 write!(out, "&return$.value");
767 }
768 if indirect_call {
769 if !sig.args.is_empty() || indirect_return {
770 write!(out, ", ");
771 }
772 write!(out, "extern$");
773 }
774 write!(out, ")");
775 if let Some(ret) = &sig.ret {
776 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
777 write!(out, ")");
778 }
779 }
780 writeln!(out, ";");
781 if sig.throws {
782 writeln!(out, " if (error$.ptr) {{");
783 writeln!(out, " throw ::rust::Error(error$);");
784 writeln!(out, " }}");
785 }
786 if indirect_return {
787 out.include.utility = true;
788 writeln!(out, " return ::std::move(return$.value);");
789 }
790 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400791}
792
793fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
794 match ty {
795 None => write!(out, "void "),
796 Some(ty) => write_type_space(out, ty),
797 }
798}
799
David Tolnay75dca2e2020-03-25 20:17:52 -0700800fn indirect_return(sig: &Signature, types: &Types) -> bool {
801 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700802 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700803 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700804}
805
David Tolnay99642622020-03-25 13:07:35 -0700806fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
807 match ty {
808 Type::RustBox(ty) | Type::UniquePtr(ty) => {
809 write_type_space(out, &ty.inner);
810 write!(out, "*");
811 }
812 Type::Ref(ty) => {
813 if ty.mutability.is_none() {
814 write!(out, "const ");
815 }
816 write_type(out, &ty.inner);
817 write!(out, " *");
818 }
819 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700820 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700821 _ => write_type(out, ty),
822 }
823}
824
825fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
826 write_indirect_return_type(out, ty);
827 match ty {
828 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700829 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700830 _ => write_space_after_type(out, ty),
831 }
832}
833
834fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400835 match ty {
836 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
837 write_type_space(out, &ty.inner);
838 write!(out, "*");
839 }
David Tolnay4a441222020-01-25 16:24:27 -0800840 Some(Type::Ref(ty)) => {
841 if ty.mutability.is_none() {
842 write!(out, "const ");
843 }
844 write_type(out, &ty.inner);
845 write!(out, " *");
846 }
David Tolnay750755e2020-03-01 13:04:08 -0800847 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700848 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400849 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
850 _ => write_return_type(out, ty),
851 }
852}
853
854fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
855 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700856 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400857 write_type_space(out, &ty.inner);
858 write!(out, "*");
859 }
David Tolnay750755e2020-03-01 13:04:08 -0800860 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700861 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400862 _ => write_type_space(out, &arg.ty),
863 }
864 if types.needs_indirect_abi(&arg.ty) {
865 write!(out, "*");
866 }
867 write!(out, "{}", arg.ident);
868}
869
870fn write_type(out: &mut OutFile, ty: &Type) {
871 match ty {
872 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700873 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400874 None => write!(out, "{}", ident),
875 },
876 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800877 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400878 write_type(out, &ty.inner);
879 write!(out, ">");
880 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700881 Type::RustVec(ty) => {
882 write!(out, "::rust::Vec<");
883 write_type(out, &ty.inner);
884 write!(out, ">");
885 }
David Tolnay7db73692019-10-20 14:51:12 -0400886 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800887 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400888 write_type(out, &ptr.inner);
889 write!(out, ">");
890 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700891 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700892 write!(out, "::std::vector<");
893 write_type(out, &ty.inner);
894 write!(out, ">");
895 }
David Tolnay7db73692019-10-20 14:51:12 -0400896 Type::Ref(r) => {
897 if r.mutability.is_none() {
898 write!(out, "const ");
899 }
900 write_type(out, &r.inner);
901 write!(out, " &");
902 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700903 Type::Slice(_) => {
904 // For now, only U8 slices are supported, which are covered separately below
905 unreachable!()
906 }
David Tolnay7db73692019-10-20 14:51:12 -0400907 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800908 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400909 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700910 Type::SliceRefU8(_) => {
911 write!(out, "::rust::Slice<uint8_t>");
912 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700913 Type::Fn(f) => {
914 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
915 match &f.ret {
916 Some(ret) => write_type(out, ret),
917 None => write!(out, "void"),
918 }
919 write!(out, "(");
920 for (i, arg) in f.args.iter().enumerate() {
921 if i > 0 {
922 write!(out, ", ");
923 }
924 write_type(out, &arg.ty);
925 }
926 write!(out, ")>");
927 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700928 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400929 }
930}
931
David Tolnayf6a89f22020-05-10 23:39:27 -0700932fn write_atom(out: &mut OutFile, atom: Atom) {
933 match atom {
934 Bool => write!(out, "bool"),
935 U8 => write!(out, "uint8_t"),
936 U16 => write!(out, "uint16_t"),
937 U32 => write!(out, "uint32_t"),
938 U64 => write!(out, "uint64_t"),
939 Usize => write!(out, "size_t"),
940 I8 => write!(out, "int8_t"),
941 I16 => write!(out, "int16_t"),
942 I32 => write!(out, "int32_t"),
943 I64 => write!(out, "int64_t"),
944 Isize => write!(out, "::rust::isize"),
945 F32 => write!(out, "float"),
946 F64 => write!(out, "double"),
947 CxxString => write!(out, "::std::string"),
948 RustString => write!(out, "::rust::String"),
949 }
950}
951
David Tolnay7db73692019-10-20 14:51:12 -0400952fn write_type_space(out: &mut OutFile, ty: &Type) {
953 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700954 write_space_after_type(out, ty);
955}
956
957fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400958 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700959 Type::Ident(_)
960 | Type::RustBox(_)
961 | Type::UniquePtr(_)
962 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700963 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700964 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700965 | Type::SliceRefU8(_)
966 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400967 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700968 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400969 }
970}
971
David Tolnaycd08c442020-04-25 10:16:33 -0700972// Only called for legal referent types of unique_ptr and element types of
973// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700974fn to_typename(namespace: &Namespace, ty: &Type) -> String {
975 match ty {
976 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700977 let mut path = String::new();
978 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700979 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700980 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700981 }
David Tolnaycd08c442020-04-25 10:16:33 -0700982 path += &ident.to_string();
983 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700984 }
985 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700986 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700987 }
988}
989
David Tolnayacdf20a2020-04-25 12:40:53 -0700990// Only called for legal referent types of unique_ptr and element types of
991// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700992fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
993 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700994 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700995 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700996 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700997 }
998}
999
David Tolnay7db73692019-10-20 14:51:12 -04001000fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001001 out.begin_block("extern \"C\"");
1002 for ty in types {
1003 if let Type::RustBox(ty) = ty {
1004 if let Type::Ident(inner) = &ty.inner {
1005 out.next_section();
1006 write_rust_box_extern(out, inner);
1007 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001008 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001009 if let Type::Ident(inner) = &ty.inner {
1010 if Atom::from(inner).is_none() {
1011 out.next_section();
1012 write_rust_vec_extern(out, inner);
1013 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001014 }
David Tolnay7db73692019-10-20 14:51:12 -04001015 } else if let Type::UniquePtr(ptr) = ty {
1016 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001017 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001018 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001019 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001020 }
1021 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001022 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001023 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001024 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001025 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001026 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001027 }
1028 }
1029 }
1030 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001031 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001032
David Tolnay750755e2020-03-01 13:04:08 -08001033 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001034 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001035 for ty in types {
1036 if let Type::RustBox(ty) = ty {
1037 if let Type::Ident(inner) = &ty.inner {
1038 write_rust_box_impl(out, inner);
1039 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001040 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001041 if let Type::Ident(inner) = &ty.inner {
1042 if Atom::from(inner).is_none() {
1043 write_rust_vec_impl(out, inner);
1044 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001045 }
David Tolnay7db73692019-10-20 14:51:12 -04001046 }
1047 }
David Tolnay591dcb62020-09-01 23:00:38 -07001048 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001049 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001050}
1051
1052fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1053 let mut inner = String::new();
1054 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001055 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001056 inner += "::";
1057 }
1058 inner += &ident.to_string();
1059 let instance = inner.replace("::", "$");
1060
David Tolnay591dcb62020-09-01 23:00:38 -07001061 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1062 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001063 writeln!(
1064 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001065 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001066 instance, inner,
1067 );
1068 writeln!(
1069 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001070 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001071 instance, inner,
1072 );
David Tolnay591dcb62020-09-01 23:00:38 -07001073 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001074}
1075
David Tolnay6787be62020-04-25 11:01:02 -07001076fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1077 let element = Type::Ident(element.clone());
1078 let inner = to_typename(&out.namespace, &element);
1079 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001080
David Tolnay591dcb62020-09-01 23:00:38 -07001081 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1082 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001083 writeln!(
1084 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001085 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001086 instance, inner,
1087 );
1088 writeln!(
1089 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001090 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001091 instance, inner,
1092 );
1093 writeln!(
1094 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001095 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001096 instance, inner,
1097 );
David Tolnay219c0792020-04-24 20:31:37 -07001098 writeln!(
1099 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001100 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001101 inner, instance,
1102 );
David Tolnay503d0192020-04-24 22:18:56 -07001103 writeln!(
1104 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001105 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001106 instance,
1107 );
David Tolnay591dcb62020-09-01 23:00:38 -07001108 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001109}
1110
David Tolnay7db73692019-10-20 14:51:12 -04001111fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1112 let mut inner = String::new();
1113 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001114 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001115 inner += "::";
1116 }
1117 inner += &ident.to_string();
1118 let instance = inner.replace("::", "$");
1119
1120 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001121 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001122 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001123 writeln!(out, "}}");
1124
1125 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001126 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001127 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001128 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001129}
1130
David Tolnay6787be62020-04-25 11:01:02 -07001131fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1132 let element = Type::Ident(element.clone());
1133 let inner = to_typename(&out.namespace, &element);
1134 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001135
Myron Ahneba35cf2020-02-05 19:41:51 +07001136 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001137 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001138 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001139 writeln!(out, "}}");
1140
1141 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001142 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1143 writeln!(
1144 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001145 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001146 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001147 );
1148 writeln!(out, "}}");
1149
1150 writeln!(out, "template <>");
1151 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001152 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001153 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001154
1155 writeln!(out, "template <>");
1156 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1157 writeln!(
1158 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001159 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001160 instance,
1161 );
1162 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001163
1164 writeln!(out, "template <>");
1165 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001166 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001167 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001168}
1169
David Tolnay63da4d32020-04-25 09:41:12 -07001170fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1171 let ty = Type::Ident(ident.clone());
1172 let instance = to_mangled(&out.namespace, &ty);
1173
David Tolnay591dcb62020-09-01 23:00:38 -07001174 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1175 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001176
1177 write_unique_ptr_common(out, &ty, types);
1178
David Tolnay591dcb62020-09-01 23:00:38 -07001179 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001180}
1181
1182// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1183fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001184 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001185 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001186 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001187 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001188
David Tolnay63da4d32020-04-25 09:41:12 -07001189 let can_construct_from_value = match ty {
1190 Type::Ident(ident) => types.structs.contains_key(ident),
1191 _ => false,
1192 };
1193
David Tolnay7db73692019-10-20 14:51:12 -04001194 writeln!(
1195 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001196 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001197 inner,
1198 );
1199 writeln!(
1200 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001201 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001202 inner,
1203 );
1204 writeln!(
1205 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001206 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001207 instance, inner,
1208 );
David Tolnay7e219b82020-03-01 13:14:51 -08001209 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001210 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001211 if can_construct_from_value {
1212 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001213 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001214 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001215 instance, inner, inner,
1216 );
David Tolnay63da4d32020-04-25 09:41:12 -07001217 writeln!(
1218 out,
1219 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1220 inner, inner,
1221 );
1222 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001223 }
David Tolnay7db73692019-10-20 14:51:12 -04001224 writeln!(
1225 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001226 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001227 instance, inner, inner,
1228 );
David Tolnay7e219b82020-03-01 13:14:51 -08001229 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001230 writeln!(out, "}}");
1231 writeln!(
1232 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001233 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001234 inner, instance, inner,
1235 );
1236 writeln!(out, " return ptr.get();");
1237 writeln!(out, "}}");
1238 writeln!(
1239 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001240 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001241 inner, instance, inner,
1242 );
1243 writeln!(out, " return ptr.release();");
1244 writeln!(out, "}}");
1245 writeln!(
1246 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001247 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001248 instance, inner,
1249 );
1250 writeln!(out, " ptr->~unique_ptr();");
1251 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001252}
Myron Ahneba35cf2020-02-05 19:41:51 +07001253
David Tolnay92105da2020-04-25 11:15:31 -07001254fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001255 let element = Type::Ident(element.clone());
1256 let inner = to_typename(&out.namespace, &element);
1257 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001258
David Tolnay591dcb62020-09-01 23:00:38 -07001259 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1260 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001261 writeln!(
1262 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001263 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001264 instance, inner,
1265 );
1266 writeln!(out, " return s.size();");
1267 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001268 writeln!(
1269 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001270 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001271 inner, instance, inner,
1272 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001273 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001274 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001275
1276 write_unique_ptr_common(out, vector_ty, types);
1277
David Tolnay591dcb62020-09-01 23:00:38 -07001278 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001279}