blob: 2e2b81f8c6c3e4298d5164e8e6819b24dca9026b [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, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400778 if !indirect_return {
779 if let Some(ret) = &sig.ret {
780 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
781 write!(out, ")");
782 }
David Tolnay439cde22020-04-20 00:46:25 -0700783 }
784 }
785 writeln!(out, ";");
786 if sig.throws {
787 writeln!(out, " if (error$.ptr) {{");
788 writeln!(out, " throw ::rust::Error(error$);");
789 writeln!(out, " }}");
790 }
791 if indirect_return {
792 out.include.utility = true;
793 writeln!(out, " return ::std::move(return$.value);");
794 }
795 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400796}
797
798fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
799 match ty {
800 None => write!(out, "void "),
801 Some(ty) => write_type_space(out, ty),
802 }
803}
804
David Tolnay75dca2e2020-03-25 20:17:52 -0700805fn indirect_return(sig: &Signature, types: &Types) -> bool {
806 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700807 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700808 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700809}
810
David Tolnay99642622020-03-25 13:07:35 -0700811fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
812 match ty {
813 Type::RustBox(ty) | Type::UniquePtr(ty) => {
814 write_type_space(out, &ty.inner);
815 write!(out, "*");
816 }
817 Type::Ref(ty) => {
818 if ty.mutability.is_none() {
819 write!(out, "const ");
820 }
821 write_type(out, &ty.inner);
822 write!(out, " *");
823 }
824 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700825 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700826 _ => write_type(out, ty),
827 }
828}
829
830fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
831 write_indirect_return_type(out, ty);
832 match ty {
833 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700834 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700835 _ => write_space_after_type(out, ty),
836 }
837}
838
839fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400840 match ty {
841 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
842 write_type_space(out, &ty.inner);
843 write!(out, "*");
844 }
David Tolnay4a441222020-01-25 16:24:27 -0800845 Some(Type::Ref(ty)) => {
846 if ty.mutability.is_none() {
847 write!(out, "const ");
848 }
849 write_type(out, &ty.inner);
850 write!(out, " *");
851 }
David Tolnay750755e2020-03-01 13:04:08 -0800852 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700853 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400854 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
855 _ => write_return_type(out, ty),
856 }
857}
858
859fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
860 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700861 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400862 write_type_space(out, &ty.inner);
863 write!(out, "*");
864 }
David Tolnay750755e2020-03-01 13:04:08 -0800865 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700866 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400867 _ => write_type_space(out, &arg.ty),
868 }
869 if types.needs_indirect_abi(&arg.ty) {
870 write!(out, "*");
871 }
872 write!(out, "{}", arg.ident);
873}
874
875fn write_type(out: &mut OutFile, ty: &Type) {
876 match ty {
877 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700878 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400879 None => write!(out, "{}", ident),
880 },
881 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800882 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400883 write_type(out, &ty.inner);
884 write!(out, ">");
885 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700886 Type::RustVec(ty) => {
887 write!(out, "::rust::Vec<");
888 write_type(out, &ty.inner);
889 write!(out, ">");
890 }
David Tolnay7db73692019-10-20 14:51:12 -0400891 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800892 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400893 write_type(out, &ptr.inner);
894 write!(out, ">");
895 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700896 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700897 write!(out, "::std::vector<");
898 write_type(out, &ty.inner);
899 write!(out, ">");
900 }
David Tolnay7db73692019-10-20 14:51:12 -0400901 Type::Ref(r) => {
902 if r.mutability.is_none() {
903 write!(out, "const ");
904 }
905 write_type(out, &r.inner);
906 write!(out, " &");
907 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700908 Type::Slice(_) => {
909 // For now, only U8 slices are supported, which are covered separately below
910 unreachable!()
911 }
David Tolnay7db73692019-10-20 14:51:12 -0400912 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800913 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400914 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700915 Type::SliceRefU8(_) => {
916 write!(out, "::rust::Slice<uint8_t>");
917 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700918 Type::Fn(f) => {
919 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
920 match &f.ret {
921 Some(ret) => write_type(out, ret),
922 None => write!(out, "void"),
923 }
924 write!(out, "(");
925 for (i, arg) in f.args.iter().enumerate() {
926 if i > 0 {
927 write!(out, ", ");
928 }
929 write_type(out, &arg.ty);
930 }
931 write!(out, ")>");
932 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700933 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400934 }
935}
936
David Tolnayf6a89f22020-05-10 23:39:27 -0700937fn write_atom(out: &mut OutFile, atom: Atom) {
938 match atom {
939 Bool => write!(out, "bool"),
940 U8 => write!(out, "uint8_t"),
941 U16 => write!(out, "uint16_t"),
942 U32 => write!(out, "uint32_t"),
943 U64 => write!(out, "uint64_t"),
944 Usize => write!(out, "size_t"),
945 I8 => write!(out, "int8_t"),
946 I16 => write!(out, "int16_t"),
947 I32 => write!(out, "int32_t"),
948 I64 => write!(out, "int64_t"),
949 Isize => write!(out, "::rust::isize"),
950 F32 => write!(out, "float"),
951 F64 => write!(out, "double"),
952 CxxString => write!(out, "::std::string"),
953 RustString => write!(out, "::rust::String"),
954 }
955}
956
David Tolnay7db73692019-10-20 14:51:12 -0400957fn write_type_space(out: &mut OutFile, ty: &Type) {
958 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700959 write_space_after_type(out, ty);
960}
961
962fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400963 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700964 Type::Ident(_)
965 | Type::RustBox(_)
966 | Type::UniquePtr(_)
967 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700968 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700969 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700970 | Type::SliceRefU8(_)
971 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400972 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700973 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400974 }
975}
976
David Tolnaycd08c442020-04-25 10:16:33 -0700977// Only called for legal referent types of unique_ptr and element types of
978// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700979fn to_typename(namespace: &Namespace, ty: &Type) -> String {
980 match ty {
981 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700982 let mut path = String::new();
983 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700984 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700985 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700986 }
David Tolnaycd08c442020-04-25 10:16:33 -0700987 path += &ident.to_string();
988 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700989 }
990 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700991 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700992 }
993}
994
David Tolnayacdf20a2020-04-25 12:40:53 -0700995// Only called for legal referent types of unique_ptr and element types of
996// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700997fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
998 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700999 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001000 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001001 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001002 }
1003}
1004
David Tolnay7db73692019-10-20 14:51:12 -04001005fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001006 out.begin_block("extern \"C\"");
1007 for ty in types {
1008 if let Type::RustBox(ty) = ty {
1009 if let Type::Ident(inner) = &ty.inner {
1010 out.next_section();
1011 write_rust_box_extern(out, inner);
1012 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001013 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001014 if let Type::Ident(inner) = &ty.inner {
1015 if Atom::from(inner).is_none() {
1016 out.next_section();
1017 write_rust_vec_extern(out, inner);
1018 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001019 }
David Tolnay7db73692019-10-20 14:51:12 -04001020 } else if let Type::UniquePtr(ptr) = ty {
1021 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001022 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001023 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001024 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001025 }
1026 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001027 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001028 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001029 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001030 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001031 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001032 }
1033 }
1034 }
1035 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001036 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001037
David Tolnay750755e2020-03-01 13:04:08 -08001038 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001039 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001040 for ty in types {
1041 if let Type::RustBox(ty) = ty {
1042 if let Type::Ident(inner) = &ty.inner {
1043 write_rust_box_impl(out, inner);
1044 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001045 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001046 if let Type::Ident(inner) = &ty.inner {
1047 if Atom::from(inner).is_none() {
1048 write_rust_vec_impl(out, inner);
1049 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001050 }
David Tolnay7db73692019-10-20 14:51:12 -04001051 }
1052 }
David Tolnay591dcb62020-09-01 23:00:38 -07001053 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001054 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001055}
1056
1057fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1058 let mut inner = String::new();
1059 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001060 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001061 inner += "::";
1062 }
1063 inner += &ident.to_string();
1064 let instance = inner.replace("::", "$");
1065
David Tolnay591dcb62020-09-01 23:00:38 -07001066 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1067 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001068 writeln!(
1069 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001070 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001071 instance, inner,
1072 );
1073 writeln!(
1074 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001075 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001076 instance, inner,
1077 );
David Tolnay591dcb62020-09-01 23:00:38 -07001078 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001079}
1080
David Tolnay6787be62020-04-25 11:01:02 -07001081fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1082 let element = Type::Ident(element.clone());
1083 let inner = to_typename(&out.namespace, &element);
1084 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001085
David Tolnay591dcb62020-09-01 23:00:38 -07001086 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1087 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001088 writeln!(
1089 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001090 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001091 instance, inner,
1092 );
1093 writeln!(
1094 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001095 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001096 instance, inner,
1097 );
1098 writeln!(
1099 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001100 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001101 instance, inner,
1102 );
David Tolnay219c0792020-04-24 20:31:37 -07001103 writeln!(
1104 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001105 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001106 inner, instance,
1107 );
David Tolnay503d0192020-04-24 22:18:56 -07001108 writeln!(
1109 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001110 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001111 instance,
1112 );
David Tolnay591dcb62020-09-01 23:00:38 -07001113 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001114}
1115
David Tolnay7db73692019-10-20 14:51:12 -04001116fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1117 let mut inner = String::new();
1118 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001119 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001120 inner += "::";
1121 }
1122 inner += &ident.to_string();
1123 let instance = inner.replace("::", "$");
1124
1125 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001126 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001127 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001128 writeln!(out, "}}");
1129
1130 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001131 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001132 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001133 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001134}
1135
David Tolnay6787be62020-04-25 11:01:02 -07001136fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1137 let element = Type::Ident(element.clone());
1138 let inner = to_typename(&out.namespace, &element);
1139 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001140
Myron Ahneba35cf2020-02-05 19:41:51 +07001141 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001142 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001143 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001144 writeln!(out, "}}");
1145
1146 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001147 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1148 writeln!(
1149 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001150 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001151 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001152 );
1153 writeln!(out, "}}");
1154
1155 writeln!(out, "template <>");
1156 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001157 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001158 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001159
1160 writeln!(out, "template <>");
1161 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1162 writeln!(
1163 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001164 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001165 instance,
1166 );
1167 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001168
1169 writeln!(out, "template <>");
1170 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001171 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001172 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001173}
1174
David Tolnay63da4d32020-04-25 09:41:12 -07001175fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1176 let ty = Type::Ident(ident.clone());
1177 let instance = to_mangled(&out.namespace, &ty);
1178
David Tolnay591dcb62020-09-01 23:00:38 -07001179 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1180 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001181
1182 write_unique_ptr_common(out, &ty, types);
1183
David Tolnay591dcb62020-09-01 23:00:38 -07001184 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001185}
1186
1187// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1188fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001189 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001190 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001191 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001192 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001193
David Tolnay63da4d32020-04-25 09:41:12 -07001194 let can_construct_from_value = match ty {
1195 Type::Ident(ident) => types.structs.contains_key(ident),
1196 _ => false,
1197 };
1198
David Tolnay7db73692019-10-20 14:51:12 -04001199 writeln!(
1200 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001201 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001202 inner,
1203 );
1204 writeln!(
1205 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001206 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001207 inner,
1208 );
1209 writeln!(
1210 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001211 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001212 instance, inner,
1213 );
David Tolnay7e219b82020-03-01 13:14:51 -08001214 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001215 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001216 if can_construct_from_value {
1217 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001218 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001219 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001220 instance, inner, inner,
1221 );
David Tolnay63da4d32020-04-25 09:41:12 -07001222 writeln!(
1223 out,
1224 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1225 inner, inner,
1226 );
1227 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001228 }
David Tolnay7db73692019-10-20 14:51:12 -04001229 writeln!(
1230 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001231 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001232 instance, inner, inner,
1233 );
David Tolnay7e219b82020-03-01 13:14:51 -08001234 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001235 writeln!(out, "}}");
1236 writeln!(
1237 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001238 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001239 inner, instance, inner,
1240 );
1241 writeln!(out, " return ptr.get();");
1242 writeln!(out, "}}");
1243 writeln!(
1244 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001245 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001246 inner, instance, inner,
1247 );
1248 writeln!(out, " return ptr.release();");
1249 writeln!(out, "}}");
1250 writeln!(
1251 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001252 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001253 instance, inner,
1254 );
1255 writeln!(out, " ptr->~unique_ptr();");
1256 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001257}
Myron Ahneba35cf2020-02-05 19:41:51 +07001258
David Tolnay92105da2020-04-25 11:15:31 -07001259fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001260 let element = Type::Ident(element.clone());
1261 let inner = to_typename(&out.namespace, &element);
1262 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001263
David Tolnay591dcb62020-09-01 23:00:38 -07001264 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1265 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001266 writeln!(
1267 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001268 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001269 instance, inner,
1270 );
1271 writeln!(out, " return s.size();");
1272 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001273 writeln!(
1274 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001275 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001276 inner, instance, inner,
1277 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001278 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001279 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001280
1281 write_unique_ptr_common(out, vector_ty, types);
1282
David Tolnay591dcb62020-09-01 23:00:38 -07001283 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001284}