blob: 862be814ff108bb68d22967796ee8a2ad3217ea8 [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 => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400180 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700181 needs_rust_isize = true;
182 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700183 ty if ty == RustString => {
184 out.include.array = true;
185 out.include.cstdint = true;
186 out.include.string = true;
187 needs_rust_string = true;
188 }
189 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400190 }
191 }
192
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800194 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800195 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700196 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800197 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700198 match api {
199 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700200 if efn.throws {
201 needs_trycatch = true;
202 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700204 let bitcopy = match arg.ty {
205 Type::RustVec(_) => true,
206 _ => arg.ty == RustString,
207 };
208 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700209 needs_unsafe_bitcopy = true;
210 break;
211 }
David Tolnay09011c32020-03-06 14:40:28 -0800212 }
213 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 Api::RustFunction(efn) if !out.header => {
215 if efn.throws {
216 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700217 out.include.string = true;
218 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200220 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 }
222 for arg in &efn.args {
223 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
224 needs_manually_drop = true;
225 break;
226 }
227 }
228 if let Some(ret) = &efn.ret {
229 if types.needs_indirect_abi(ret) {
230 needs_maybe_uninit = true;
231 }
David Tolnayf51447e2020-03-06 14:14:27 -0800232 }
233 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700234 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800235 }
236 }
237
David Tolnay750755e2020-03-01 13:04:08 -0800238 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700239 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800240
David Tolnay16ab1462020-09-02 15:10:09 -0700241 if needs_panic
242 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700244 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700246 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700247 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700248 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700249 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700250 || needs_unsafe_bitcopy
251 || needs_manually_drop
252 || needs_maybe_uninit
253 {
David Tolnay736cbca2020-03-11 16:49:18 -0700254 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800255 }
256
David Tolnay16ab1462020-09-02 15:10:09 -0700257 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
258
David Tolnay99a95e62020-09-02 15:05:53 -0700259 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700260 out.next_section();
261 writeln!(out, "struct unsafe_bitcopy_t;");
262 }
263
David Tolnay591dcb62020-09-01 23:00:38 -0700264 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
265 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
266 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
267 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700268 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700269 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
270 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
271 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
272 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800273
274 if needs_manually_drop {
275 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700276 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800277 writeln!(out, "template <typename T>");
278 writeln!(out, "union ManuallyDrop {{");
279 writeln!(out, " T value;");
280 writeln!(
281 out,
282 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
283 );
284 writeln!(out, " ~ManuallyDrop() {{}}");
285 writeln!(out, "}};");
286 }
287
David Tolnay09011c32020-03-06 14:40:28 -0800288 if needs_maybe_uninit {
289 out.next_section();
290 writeln!(out, "template <typename T>");
291 writeln!(out, "union MaybeUninit {{");
292 writeln!(out, " T value;");
293 writeln!(out, " MaybeUninit() {{}}");
294 writeln!(out, " ~MaybeUninit() {{}}");
295 writeln!(out, "}};");
296 }
297
David Tolnay591dcb62020-09-01 23:00:38 -0700298 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700299
David Tolnay5d121442020-03-17 22:14:40 -0700300 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700301 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700302 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700303 out.include.type_traits = true;
304 out.include.utility = true;
305 writeln!(out, "class missing {{}};");
306 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700307 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700308 writeln!(out, "template <typename Try, typename Fail>");
309 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700310 writeln!(
311 out,
David Tolnay04722332020-03-18 11:31:54 -0700312 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700313 );
David Tolnay04722332020-03-18 11:31:54 -0700314 writeln!(out, " missing>::value>::type");
315 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700316 writeln!(out, " func();");
317 writeln!(out, "}} catch (const ::std::exception &e) {{");
318 writeln!(out, " fail(e.what());");
319 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700320 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700321 }
322
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800323 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400324}
325
326fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700327 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700328 writeln!(out, "#ifndef {}", guard);
329 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400330 for line in strct.doc.to_string().lines() {
331 writeln!(out, "//{}", line);
332 }
333 writeln!(out, "struct {} final {{", strct.ident);
334 for field in &strct.fields {
335 write!(out, " ");
336 write_type_space(out, &field.ty);
337 writeln!(out, "{};", field.ident);
338 }
339 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700340 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400341}
342
343fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
344 writeln!(out, "struct {};", ident);
345}
346
David Tolnay8861bee2020-01-20 18:39:24 -0800347fn write_struct_using(out: &mut OutFile, ident: &Ident) {
348 writeln!(out, "using {} = {};", ident, ident);
349}
350
David Tolnayc1fe0052020-04-17 15:15:06 -0700351fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700352 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700353 writeln!(out, "#ifndef {}", guard);
354 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700355 for line in ety.doc.to_string().lines() {
356 writeln!(out, "//{}", line);
357 }
358 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700359 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700360 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700361 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700362 write!(out, " ");
363 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700364 let local_name = method.ident.to_string();
365 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700366 writeln!(out, ";");
367 }
368 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700369 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700370}
371
Joel Galensonc03402a2020-04-23 17:31:09 -0700372fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700373 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700374 writeln!(out, "#ifndef {}", guard);
375 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700376 for line in enm.doc.to_string().lines() {
377 writeln!(out, "//{}", line);
378 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700379 write!(out, "enum class {} : ", enm.ident);
380 write_atom(out, enm.repr);
381 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700382 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700383 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700384 }
385 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700386 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700387}
388
Joel Galenson905eb2e2020-05-04 14:58:14 -0700389fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700390 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
391 write_atom(out, enm.repr);
392 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700393 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700394 write!(out, "static_assert(static_cast<");
395 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700396 writeln!(
397 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700398 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700399 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700400 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700401 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700402}
403
David Tolnayebef4a22020-03-17 15:33:47 -0700404fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
405 let mut has_cxx_throws = false;
406 for api in apis {
407 if let Api::CxxFunction(efn) = api {
408 if efn.throws {
409 has_cxx_throws = true;
410 break;
411 }
412 }
413 }
414
415 if has_cxx_throws {
416 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700417 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700418 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700419 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700420 );
421 }
422}
423
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700424fn write_cxx_function_shim(
425 out: &mut OutFile,
426 efn: &ExternFn,
427 types: &Types,
428 impl_annotations: &Option<String>,
429) {
430 if !out.header {
431 if let Some(annotation) = impl_annotations {
432 write!(out, "{} ", annotation);
433 }
434 }
David Tolnayebef4a22020-03-17 15:33:47 -0700435 if efn.throws {
436 write!(out, "::rust::Str::Repr ");
437 } else {
David Tolnay99642622020-03-25 13:07:35 -0700438 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700439 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700440 let mangled = mangle::extern_fn(&out.namespace, efn);
441 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700442 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700443 if receiver.mutability.is_none() {
444 write!(out, "const ");
445 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700446 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700447 }
David Tolnay7db73692019-10-20 14:51:12 -0400448 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700449 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400450 write!(out, ", ");
451 }
David Tolnaya46a2372020-03-06 10:03:48 -0800452 if arg.ty == RustString {
453 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700454 } else if let Type::RustVec(_) = arg.ty {
455 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800456 }
David Tolnay7db73692019-10-20 14:51:12 -0400457 write_extern_arg(out, arg, types);
458 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700459 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400460 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700461 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400462 write!(out, ", ");
463 }
David Tolnay99642622020-03-25 13:07:35 -0700464 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400465 write!(out, "*return$");
466 }
467 writeln!(out, ") noexcept {{");
468 write!(out, " ");
469 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700470 match &efn.receiver {
471 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700472 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700473 }
David Tolnay7db73692019-10-20 14:51:12 -0400474 for (i, arg) in efn.args.iter().enumerate() {
475 if i > 0 {
476 write!(out, ", ");
477 }
478 write_type(out, &arg.ty);
479 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700480 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700481 if let Some(receiver) = &efn.receiver {
482 if receiver.mutability.is_none() {
483 write!(out, " const");
484 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700485 }
486 write!(out, " = ");
487 match &efn.receiver {
488 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700489 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700490 }
491 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400492 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700493 if efn.throws {
494 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700495 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700496 writeln!(out, " [&] {{");
497 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700498 }
David Tolnay7db73692019-10-20 14:51:12 -0400499 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700500 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400501 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700502 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400503 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700504 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400505 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700506 }
507 match &efn.ret {
508 Some(Type::Ref(_)) => write!(out, "&"),
509 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700510 Some(Type::SliceRefU8(_)) if !indirect_return => {
511 write!(out, "::rust::Slice<uint8_t>::Repr(")
512 }
David Tolnay99642622020-03-25 13:07:35 -0700513 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400514 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700515 match &efn.receiver {
516 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700517 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700518 }
David Tolnay7db73692019-10-20 14:51:12 -0400519 for (i, arg) in efn.args.iter().enumerate() {
520 if i > 0 {
521 write!(out, ", ");
522 }
523 if let Type::RustBox(_) = &arg.ty {
524 write_type(out, &arg.ty);
525 write!(out, "::from_raw({})", arg.ident);
526 } else if let Type::UniquePtr(_) = &arg.ty {
527 write_type(out, &arg.ty);
528 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800529 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800530 write!(
531 out,
532 "::rust::String(::rust::unsafe_bitcopy, *{})",
533 arg.ident,
534 );
David Tolnay313b10e2020-04-25 16:30:51 -0700535 } else if let Type::RustVec(_) = arg.ty {
536 write_type(out, &arg.ty);
537 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400538 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700539 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800540 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400541 } else {
542 write!(out, "{}", arg.ident);
543 }
544 }
545 write!(out, ")");
546 match &efn.ret {
547 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
548 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700549 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400550 _ => {}
551 }
552 if indirect_return {
553 write!(out, ")");
554 }
555 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700556 if efn.throws {
557 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700558 writeln!(out, " throw$.ptr = nullptr;");
559 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700560 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700561 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700562 writeln!(
563 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700564 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700565 );
David Tolnay5d121442020-03-17 22:14:40 -0700566 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700567 writeln!(out, " return throw$;");
568 }
David Tolnay7db73692019-10-20 14:51:12 -0400569 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700570 for arg in &efn.args {
571 if let Type::Fn(f) = &arg.ty {
572 let var = &arg.ident;
573 write_function_pointer_trampoline(out, efn, var, f, types);
574 }
575 }
576}
577
578fn write_function_pointer_trampoline(
579 out: &mut OutFile,
580 efn: &ExternFn,
581 var: &Ident,
582 f: &Signature,
583 types: &Types,
584) {
585 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700586 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700587 let indirect_call = true;
588 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
589
590 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700591 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400593}
594
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700595fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700596 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700597 let indirect_call = false;
598 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
599}
600
601fn write_rust_function_decl_impl(
602 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700603 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700604 sig: &Signature,
605 types: &Types,
606 indirect_call: bool,
607) {
608 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700609 write!(out, "::rust::Str::Repr ");
610 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700611 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700612 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700613 write!(out, "{}(", link_name);
614 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700615 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700616 if receiver.mutability.is_none() {
617 write!(out, "const ");
618 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700619 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700620 needs_comma = true;
621 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700622 for arg in &sig.args {
623 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400624 write!(out, ", ");
625 }
626 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400628 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700629 if indirect_return(sig, types) {
630 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400631 write!(out, ", ");
632 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700633 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400634 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700635 needs_comma = true;
636 }
637 if indirect_call {
638 if needs_comma {
639 write!(out, ", ");
640 }
641 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400642 }
643 writeln!(out, ") noexcept;");
644}
645
646fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400647 for line in efn.doc.to_string().lines() {
648 writeln!(out, "//{}", line);
649 }
David Tolnaya73853b2020-04-20 01:19:56 -0700650 let local_name = match &efn.sig.receiver {
651 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700652 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700653 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700654 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700655 let indirect_call = false;
656 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
657}
658
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700659fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700660 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700661 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700662 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700663 indirect_call: bool,
664) {
665 write_return_type(out, &sig.ret);
666 write!(out, "{}(", local_name);
667 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400668 if i > 0 {
669 write!(out, ", ");
670 }
671 write_type_space(out, &arg.ty);
672 write!(out, "{}", arg.ident);
673 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700674 if indirect_call {
675 if !sig.args.is_empty() {
676 write!(out, ", ");
677 }
678 write!(out, "void *extern$");
679 }
David Tolnay1e548172020-03-16 13:37:09 -0700680 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700681 if let Some(receiver) = &sig.receiver {
682 if receiver.mutability.is_none() {
683 write!(out, " const");
684 }
685 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700686 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700687 write!(out, " noexcept");
688 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700689}
690
691fn write_rust_function_shim_impl(
692 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700693 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700694 sig: &Signature,
695 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700696 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700697 indirect_call: bool,
698) {
699 if out.header && sig.receiver.is_some() {
700 // We've already defined this inside the struct.
701 return;
702 }
David Tolnaya73853b2020-04-20 01:19:56 -0700703 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400704 if out.header {
705 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700706 return;
David Tolnay7db73692019-10-20 14:51:12 -0400707 }
David Tolnay439cde22020-04-20 00:46:25 -0700708 writeln!(out, " {{");
709 for arg in &sig.args {
710 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
711 out.include.utility = true;
712 write!(out, " ::rust::ManuallyDrop<");
713 write_type(out, &arg.ty);
714 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
715 }
716 }
717 write!(out, " ");
718 let indirect_return = indirect_return(sig, types);
719 if indirect_return {
720 write!(out, "::rust::MaybeUninit<");
721 write_type(out, sig.ret.as_ref().unwrap());
722 writeln!(out, "> return$;");
723 write!(out, " ");
724 } else if let Some(ret) = &sig.ret {
725 write!(out, "return ");
726 match ret {
727 Type::RustBox(_) => {
728 write_type(out, ret);
729 write!(out, "::from_raw(");
730 }
731 Type::UniquePtr(_) => {
732 write_type(out, ret);
733 write!(out, "(");
734 }
735 Type::Ref(_) => write!(out, "*"),
736 _ => {}
737 }
738 }
739 if sig.throws {
740 write!(out, "::rust::Str::Repr error$ = ");
741 }
742 write!(out, "{}(", invoke);
743 if sig.receiver.is_some() {
744 write!(out, "*this");
745 }
746 for (i, arg) in sig.args.iter().enumerate() {
747 if i > 0 || sig.receiver.is_some() {
748 write!(out, ", ");
749 }
750 match &arg.ty {
751 Type::Str(_) => write!(out, "::rust::Str::Repr("),
752 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
753 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
754 _ => {}
755 }
756 write!(out, "{}", arg.ident);
757 match &arg.ty {
758 Type::RustBox(_) => write!(out, ".into_raw()"),
759 Type::UniquePtr(_) => write!(out, ".release()"),
760 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
761 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
762 _ => {}
763 }
764 }
765 if indirect_return {
766 if !sig.args.is_empty() {
767 write!(out, ", ");
768 }
769 write!(out, "&return$.value");
770 }
771 if indirect_call {
772 if !sig.args.is_empty() || indirect_return {
773 write!(out, ", ");
774 }
775 write!(out, "extern$");
776 }
777 write!(out, ")");
778 if let Some(ret) = &sig.ret {
779 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
780 write!(out, ")");
781 }
782 }
783 writeln!(out, ";");
784 if sig.throws {
785 writeln!(out, " if (error$.ptr) {{");
786 writeln!(out, " throw ::rust::Error(error$);");
787 writeln!(out, " }}");
788 }
789 if indirect_return {
790 out.include.utility = true;
791 writeln!(out, " return ::std::move(return$.value);");
792 }
793 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400794}
795
796fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
797 match ty {
798 None => write!(out, "void "),
799 Some(ty) => write_type_space(out, ty),
800 }
801}
802
David Tolnay75dca2e2020-03-25 20:17:52 -0700803fn indirect_return(sig: &Signature, types: &Types) -> bool {
804 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700805 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700806 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700807}
808
David Tolnay99642622020-03-25 13:07:35 -0700809fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
810 match ty {
811 Type::RustBox(ty) | Type::UniquePtr(ty) => {
812 write_type_space(out, &ty.inner);
813 write!(out, "*");
814 }
815 Type::Ref(ty) => {
816 if ty.mutability.is_none() {
817 write!(out, "const ");
818 }
819 write_type(out, &ty.inner);
820 write!(out, " *");
821 }
822 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700823 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700824 _ => write_type(out, ty),
825 }
826}
827
828fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
829 write_indirect_return_type(out, ty);
830 match ty {
831 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700832 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700833 _ => write_space_after_type(out, ty),
834 }
835}
836
837fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400838 match ty {
839 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
840 write_type_space(out, &ty.inner);
841 write!(out, "*");
842 }
David Tolnay4a441222020-01-25 16:24:27 -0800843 Some(Type::Ref(ty)) => {
844 if ty.mutability.is_none() {
845 write!(out, "const ");
846 }
847 write_type(out, &ty.inner);
848 write!(out, " *");
849 }
David Tolnay750755e2020-03-01 13:04:08 -0800850 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700851 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400852 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
853 _ => write_return_type(out, ty),
854 }
855}
856
857fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
858 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700859 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400860 write_type_space(out, &ty.inner);
861 write!(out, "*");
862 }
David Tolnay750755e2020-03-01 13:04:08 -0800863 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700864 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400865 _ => write_type_space(out, &arg.ty),
866 }
867 if types.needs_indirect_abi(&arg.ty) {
868 write!(out, "*");
869 }
870 write!(out, "{}", arg.ident);
871}
872
873fn write_type(out: &mut OutFile, ty: &Type) {
874 match ty {
875 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700876 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400877 None => write!(out, "{}", ident),
878 },
879 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800880 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400881 write_type(out, &ty.inner);
882 write!(out, ">");
883 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700884 Type::RustVec(ty) => {
885 write!(out, "::rust::Vec<");
886 write_type(out, &ty.inner);
887 write!(out, ">");
888 }
David Tolnay7db73692019-10-20 14:51:12 -0400889 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800890 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400891 write_type(out, &ptr.inner);
892 write!(out, ">");
893 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700894 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700895 write!(out, "::std::vector<");
896 write_type(out, &ty.inner);
897 write!(out, ">");
898 }
David Tolnay7db73692019-10-20 14:51:12 -0400899 Type::Ref(r) => {
900 if r.mutability.is_none() {
901 write!(out, "const ");
902 }
903 write_type(out, &r.inner);
904 write!(out, " &");
905 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700906 Type::Slice(_) => {
907 // For now, only U8 slices are supported, which are covered separately below
908 unreachable!()
909 }
David Tolnay7db73692019-10-20 14:51:12 -0400910 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800911 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400912 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700913 Type::SliceRefU8(_) => {
914 write!(out, "::rust::Slice<uint8_t>");
915 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700916 Type::Fn(f) => {
917 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
918 match &f.ret {
919 Some(ret) => write_type(out, ret),
920 None => write!(out, "void"),
921 }
922 write!(out, "(");
923 for (i, arg) in f.args.iter().enumerate() {
924 if i > 0 {
925 write!(out, ", ");
926 }
927 write_type(out, &arg.ty);
928 }
929 write!(out, ")>");
930 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700931 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400932 }
933}
934
David Tolnayf6a89f22020-05-10 23:39:27 -0700935fn write_atom(out: &mut OutFile, atom: Atom) {
936 match atom {
937 Bool => write!(out, "bool"),
938 U8 => write!(out, "uint8_t"),
939 U16 => write!(out, "uint16_t"),
940 U32 => write!(out, "uint32_t"),
941 U64 => write!(out, "uint64_t"),
942 Usize => write!(out, "size_t"),
943 I8 => write!(out, "int8_t"),
944 I16 => write!(out, "int16_t"),
945 I32 => write!(out, "int32_t"),
946 I64 => write!(out, "int64_t"),
947 Isize => write!(out, "::rust::isize"),
948 F32 => write!(out, "float"),
949 F64 => write!(out, "double"),
950 CxxString => write!(out, "::std::string"),
951 RustString => write!(out, "::rust::String"),
952 }
953}
954
David Tolnay7db73692019-10-20 14:51:12 -0400955fn write_type_space(out: &mut OutFile, ty: &Type) {
956 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700957 write_space_after_type(out, ty);
958}
959
960fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400961 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700962 Type::Ident(_)
963 | Type::RustBox(_)
964 | Type::UniquePtr(_)
965 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700966 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700967 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700968 | Type::SliceRefU8(_)
969 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400970 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700971 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400972 }
973}
974
David Tolnaycd08c442020-04-25 10:16:33 -0700975// Only called for legal referent types of unique_ptr and element types of
976// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700977fn to_typename(namespace: &Namespace, ty: &Type) -> String {
978 match ty {
979 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700980 let mut path = String::new();
981 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700982 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700983 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700984 }
David Tolnaycd08c442020-04-25 10:16:33 -0700985 path += &ident.to_string();
986 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700987 }
988 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700989 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700990 }
991}
992
David Tolnayacdf20a2020-04-25 12:40:53 -0700993// Only called for legal referent types of unique_ptr and element types of
994// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700995fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
996 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700997 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700998 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700999 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001000 }
1001}
1002
David Tolnay7db73692019-10-20 14:51:12 -04001003fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001004 out.begin_block("extern \"C\"");
1005 for ty in types {
1006 if let Type::RustBox(ty) = ty {
1007 if let Type::Ident(inner) = &ty.inner {
1008 out.next_section();
1009 write_rust_box_extern(out, inner);
1010 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001011 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001012 if let Type::Ident(inner) = &ty.inner {
1013 if Atom::from(inner).is_none() {
1014 out.next_section();
1015 write_rust_vec_extern(out, inner);
1016 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001017 }
David Tolnay7db73692019-10-20 14:51:12 -04001018 } else if let Type::UniquePtr(ptr) = ty {
1019 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001020 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001021 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001022 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001023 }
1024 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001025 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001026 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001027 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001028 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001029 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001030 }
1031 }
1032 }
1033 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001034 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001035
David Tolnay750755e2020-03-01 13:04:08 -08001036 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001037 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001038 for ty in types {
1039 if let Type::RustBox(ty) = ty {
1040 if let Type::Ident(inner) = &ty.inner {
1041 write_rust_box_impl(out, inner);
1042 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001043 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001044 if let Type::Ident(inner) = &ty.inner {
1045 if Atom::from(inner).is_none() {
1046 write_rust_vec_impl(out, inner);
1047 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001048 }
David Tolnay7db73692019-10-20 14:51:12 -04001049 }
1050 }
David Tolnay591dcb62020-09-01 23:00:38 -07001051 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001052 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001053}
1054
1055fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1056 let mut inner = String::new();
1057 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001058 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001059 inner += "::";
1060 }
1061 inner += &ident.to_string();
1062 let instance = inner.replace("::", "$");
1063
David Tolnay591dcb62020-09-01 23:00:38 -07001064 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1065 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001066 writeln!(
1067 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001068 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001069 instance, inner,
1070 );
1071 writeln!(
1072 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001073 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001074 instance, inner,
1075 );
David Tolnay591dcb62020-09-01 23:00:38 -07001076 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001077}
1078
David Tolnay6787be62020-04-25 11:01:02 -07001079fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1080 let element = Type::Ident(element.clone());
1081 let inner = to_typename(&out.namespace, &element);
1082 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001083
David Tolnay591dcb62020-09-01 23:00:38 -07001084 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1085 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001086 writeln!(
1087 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001088 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001089 instance, inner,
1090 );
1091 writeln!(
1092 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001093 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001094 instance, inner,
1095 );
1096 writeln!(
1097 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001098 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001099 instance, inner,
1100 );
David Tolnay219c0792020-04-24 20:31:37 -07001101 writeln!(
1102 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001103 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001104 inner, instance,
1105 );
David Tolnay503d0192020-04-24 22:18:56 -07001106 writeln!(
1107 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001108 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001109 instance,
1110 );
David Tolnay591dcb62020-09-01 23:00:38 -07001111 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001112}
1113
David Tolnay7db73692019-10-20 14:51:12 -04001114fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1115 let mut inner = String::new();
1116 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001117 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001118 inner += "::";
1119 }
1120 inner += &ident.to_string();
1121 let instance = inner.replace("::", "$");
1122
1123 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001124 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001125 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001126 writeln!(out, "}}");
1127
1128 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001129 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001130 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001131 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001132}
1133
David Tolnay6787be62020-04-25 11:01:02 -07001134fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1135 let element = Type::Ident(element.clone());
1136 let inner = to_typename(&out.namespace, &element);
1137 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001138
Myron Ahneba35cf2020-02-05 19:41:51 +07001139 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001140 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001141 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001142 writeln!(out, "}}");
1143
1144 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001145 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1146 writeln!(
1147 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001148 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001149 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001150 );
1151 writeln!(out, "}}");
1152
1153 writeln!(out, "template <>");
1154 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001155 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001156 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001157
1158 writeln!(out, "template <>");
1159 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1160 writeln!(
1161 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001162 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001163 instance,
1164 );
1165 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001166
1167 writeln!(out, "template <>");
1168 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001169 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001170 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001171}
1172
David Tolnay63da4d32020-04-25 09:41:12 -07001173fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1174 let ty = Type::Ident(ident.clone());
1175 let instance = to_mangled(&out.namespace, &ty);
1176
David Tolnay591dcb62020-09-01 23:00:38 -07001177 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1178 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001179
1180 write_unique_ptr_common(out, &ty, types);
1181
David Tolnay591dcb62020-09-01 23:00:38 -07001182 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001183}
1184
1185// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1186fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001187 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001188 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001189 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001190 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001191
David Tolnay63da4d32020-04-25 09:41:12 -07001192 let can_construct_from_value = match ty {
1193 Type::Ident(ident) => types.structs.contains_key(ident),
1194 _ => false,
1195 };
1196
David Tolnay7db73692019-10-20 14:51:12 -04001197 writeln!(
1198 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001199 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001200 inner,
1201 );
1202 writeln!(
1203 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001204 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001205 inner,
1206 );
1207 writeln!(
1208 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001209 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001210 instance, inner,
1211 );
David Tolnay7e219b82020-03-01 13:14:51 -08001212 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001213 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001214 if can_construct_from_value {
1215 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001216 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001217 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001218 instance, inner, inner,
1219 );
David Tolnay63da4d32020-04-25 09:41:12 -07001220 writeln!(
1221 out,
1222 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1223 inner, inner,
1224 );
1225 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001226 }
David Tolnay7db73692019-10-20 14:51:12 -04001227 writeln!(
1228 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001229 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001230 instance, inner, inner,
1231 );
David Tolnay7e219b82020-03-01 13:14:51 -08001232 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001233 writeln!(out, "}}");
1234 writeln!(
1235 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001236 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001237 inner, instance, inner,
1238 );
1239 writeln!(out, " return ptr.get();");
1240 writeln!(out, "}}");
1241 writeln!(
1242 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001243 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001244 inner, instance, inner,
1245 );
1246 writeln!(out, " return ptr.release();");
1247 writeln!(out, "}}");
1248 writeln!(
1249 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001250 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001251 instance, inner,
1252 );
1253 writeln!(out, " ptr->~unique_ptr();");
1254 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001255}
Myron Ahneba35cf2020-02-05 19:41:51 +07001256
David Tolnay92105da2020-04-25 11:15:31 -07001257fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001258 let element = Type::Ident(element.clone());
1259 let inner = to_typename(&out.namespace, &element);
1260 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001261
David Tolnay591dcb62020-09-01 23:00:38 -07001262 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1263 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001264 writeln!(
1265 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001266 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001267 instance, inner,
1268 );
1269 writeln!(out, " return s.size();");
1270 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001271 writeln!(
1272 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001273 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001274 inner, instance, inner,
1275 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001276 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001277 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001278
1279 write_unique_ptr_common(out, vector_ty, types);
1280
David Tolnay591dcb62020-09-01 23:00:38 -07001281 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001282}