blob: 098137e373e948d20c4e39791231bfef61953b7d [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,
14 opt: Opt,
15 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 Tolnay33d30292020-03-18 18:02:02 -070020 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040021 for api in apis {
22 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070023 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040024 }
25 }
26
27 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080028 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040029
David Tolnay7db73692019-10-20 14:51:12 -040030 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070031 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040032 writeln!(out, "namespace {} {{", name);
33 }
34
David Tolnay7db73692019-10-20 14:51:12 -040035 out.next_section();
36 for api in apis {
37 match api {
38 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080039 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
40 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070041 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040042 }
43 }
44
David Tolnayf94bef12020-04-17 14:46:42 -070045 let mut methods_for_type = HashMap::new();
46 for api in apis {
47 if let Api::RustFunction(efn) = api {
48 if let Some(receiver) = &efn.sig.receiver {
49 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070050 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070051 .or_insert_with(Vec::new)
52 .push(efn);
53 }
54 }
55 }
Joel Galenson968738f2020-04-15 14:19:33 -070056
David Tolnay7db73692019-10-20 14:51:12 -040057 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070058 match api {
59 Api::Struct(strct) => {
60 out.next_section();
Adrian Taylora1f890b2020-07-31 15:35:34 -070061 write_struct(out, strct, namespace);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 }
Joel Galensonc03402a2020-04-23 17:31:09 -070063 Api::Enum(enm) => {
64 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070065 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070066 check_enum(out, enm);
67 } else {
Adrian Taylora1f890b2020-07-31 15:35:34 -070068 write_enum(out, enm, namespace);
Joel Galenson905eb2e2020-05-04 14:58:14 -070069 }
Joel Galensonc03402a2020-04-23 17:31:09 -070070 }
David Tolnayc1fe0052020-04-17 15:15:06 -070071 Api::RustType(ety) => {
72 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070073 out.next_section();
Adrian Taylora1f890b2020-07-31 15:35:34 -070074 write_struct_with_methods(out, ety, methods, namespace);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070075 }
David Tolnayc1fe0052020-04-17 15:15:06 -070076 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070077 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040078 }
79 }
80
81 if !header {
82 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070083 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040084 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070085 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040086 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
87 Api::RustFunction(efn) => (efn, write_rust_function_decl),
88 _ => continue,
89 };
90 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -070091 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -040092 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080093 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040094 }
95
96 for api in apis {
97 if let Api::RustFunction(efn) = api {
98 out.next_section();
99 write_rust_function_shim(out, efn, types);
100 }
101 }
102
103 out.next_section();
104 for name in namespace.iter().rev() {
105 writeln!(out, "}} // namespace {}", name);
106 }
107
108 if !header {
109 out.next_section();
110 write_generic_instantiations(out, types);
111 }
112
David Tolnayf4632de2020-07-31 10:46:55 -0700113 // We collected necessary includes lazily while generating the above. Now
114 // put it all together.
115 let mut full_file = OutFile::new(namespace.clone(), header);
116 let full = &mut full_file;
117 if header {
118 writeln!(full, "#pragma once");
119 }
120 write!(full, "{}", out.include);
121 full.next_section();
122 full.extend(out);
123 full_file
David Tolnay7db73692019-10-20 14:51:12 -0400124}
125
126fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400127 for ty in types {
128 match ty {
129 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700130 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
131 | Some(I64) => out.include.cstdint = true,
132 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800133 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700134 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400135 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800136 Type::RustBox(_) => out.include.type_traits = true,
137 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700138 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700139 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700140 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400141 }
142 }
David Tolnay7db73692019-10-20 14:51:12 -0400143}
144
David Tolnayf51447e2020-03-06 14:14:27 -0800145fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700146 let mut needs_rust_string = false;
147 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700148 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400149 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700150 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700151 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700152 let mut needs_rust_isize = 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;
Myron Ahneba35cf2020-02-05 19:41:51 +0700164 needs_rust_vec = true;
165 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 Type::Str(_) => {
167 out.include.cstdint = true;
168 out.include.string = true;
169 needs_rust_str = true;
170 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700171 Type::Fn(_) => {
172 needs_rust_fn = true;
173 }
David Tolnay4770b472020-04-14 16:32:59 -0700174 Type::Slice(_) | Type::SliceRefU8(_) => {
175 needs_rust_slice = true;
176 }
David Tolnay7c295462020-04-25 12:45:07 -0700177 ty if ty == Isize => {
178 out.include.base_tsd = true;
179 needs_rust_isize = true;
180 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700181 ty if ty == RustString => {
182 out.include.array = true;
183 out.include.cstdint = true;
184 out.include.string = true;
185 needs_rust_string = true;
186 }
187 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400188 }
189 }
190
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 let mut needs_rust_error = false;
192 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800193 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800194 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700195 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800196 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700197 match api {
198 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700199 if efn.throws {
200 needs_trycatch = true;
201 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700202 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700203 let bitcopy = match arg.ty {
204 Type::RustVec(_) => true,
205 _ => arg.ty == RustString,
206 };
207 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700208 needs_unsafe_bitcopy = true;
209 break;
210 }
David Tolnay09011c32020-03-06 14:40:28 -0800211 }
212 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 Api::RustFunction(efn) if !out.header => {
214 if efn.throws {
215 out.include.exception = true;
216 needs_rust_error = true;
217 }
218 for arg in &efn.args {
219 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
220 needs_manually_drop = true;
221 break;
222 }
223 }
224 if let Some(ret) = &efn.ret {
225 if types.needs_indirect_abi(ret) {
226 needs_maybe_uninit = true;
227 }
David Tolnayf51447e2020-03-06 14:14:27 -0800228 }
229 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700230 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800231 }
232 }
233
David Tolnay750755e2020-03-01 13:04:08 -0800234 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700235 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800236
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 if needs_rust_string
238 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700239 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700240 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700241 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700242 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700244 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 || needs_unsafe_bitcopy
246 || needs_manually_drop
247 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700248 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 {
David Tolnay736cbca2020-03-11 16:49:18 -0700250 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800251 }
252
David Tolnay32ee9d32020-05-12 20:01:09 -0700253 if needs_rust_string || needs_rust_vec {
David Tolnayd1402742020-03-25 22:21:42 -0700254 out.next_section();
255 writeln!(out, "struct unsafe_bitcopy_t;");
256 }
257
David Tolnaye54f3382020-05-12 19:58:36 -0700258 include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
259 include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
260 include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
261 include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
262 include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
263 include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
264 include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
265 include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
266 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800267
268 if needs_manually_drop {
269 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700270 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800271 writeln!(out, "template <typename T>");
272 writeln!(out, "union ManuallyDrop {{");
273 writeln!(out, " T value;");
274 writeln!(
275 out,
276 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
277 );
278 writeln!(out, " ~ManuallyDrop() {{}}");
279 writeln!(out, "}};");
280 }
281
David Tolnay09011c32020-03-06 14:40:28 -0800282 if needs_maybe_uninit {
283 out.next_section();
284 writeln!(out, "template <typename T>");
285 writeln!(out, "union MaybeUninit {{");
286 writeln!(out, " T value;");
287 writeln!(out, " MaybeUninit() {{}}");
288 writeln!(out, " ~MaybeUninit() {{}}");
289 writeln!(out, "}};");
290 }
291
David Tolnay69601622020-04-29 18:48:36 -0700292 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700293
David Tolnay5d121442020-03-17 22:14:40 -0700294 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700295 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700296 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700297 out.include.type_traits = true;
298 out.include.utility = true;
299 writeln!(out, "class missing {{}};");
300 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700301 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700302 writeln!(out, "template <typename Try, typename Fail>");
303 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700304 writeln!(
305 out,
David Tolnay04722332020-03-18 11:31:54 -0700306 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700307 );
David Tolnay04722332020-03-18 11:31:54 -0700308 writeln!(out, " missing>::value>::type");
309 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700310 writeln!(out, " func();");
311 writeln!(out, "}} catch (const ::std::exception &e) {{");
312 writeln!(out, " fail(e.what());");
313 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700314 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700315 }
316
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800317 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400318}
319
Adrian Taylora1f890b2020-07-31 15:35:34 -0700320fn write_struct(out: &mut OutFile, strct: &Struct, namespace: &Namespace) {
321 write_include_guard_start(out, namespace, &strct.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400322 for line in strct.doc.to_string().lines() {
323 writeln!(out, "//{}", line);
324 }
325 writeln!(out, "struct {} final {{", strct.ident);
326 for field in &strct.fields {
327 write!(out, " ");
328 write_type_space(out, &field.ty);
329 writeln!(out, "{};", field.ident);
330 }
331 writeln!(out, "}};");
Adrian Taylora1f890b2020-07-31 15:35:34 -0700332 write_include_guard_end(out, namespace, &strct.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400333}
334
335fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
336 writeln!(out, "struct {};", ident);
337}
338
David Tolnay8861bee2020-01-20 18:39:24 -0800339fn write_struct_using(out: &mut OutFile, ident: &Ident) {
340 writeln!(out, "using {} = {};", ident, ident);
341}
342
Adrian Taylora1f890b2020-07-31 15:35:34 -0700343const INCLUDE_GUARD_PREFIX: &'static str = "CXXBRIDGE03_TYPE_";
344
345fn write_include_guard_start(out: &mut OutFile, namespace: &Namespace, ident: &Ident) {
346 writeln!(
347 out,
348 "#ifndef {}{}{}",
349 INCLUDE_GUARD_PREFIX, namespace, ident
350 );
351 writeln!(
352 out,
353 "#define {}{}{}",
354 INCLUDE_GUARD_PREFIX, namespace, ident
355 );
356}
357
358fn write_include_guard_end(out: &mut OutFile, namespace: &Namespace, ident: &Ident) {
359 writeln!(
360 out,
361 "#endif // {}{}{}",
362 INCLUDE_GUARD_PREFIX, namespace, ident
363 );
364}
365
366fn write_struct_with_methods(
367 out: &mut OutFile,
368 ety: &ExternType,
369 methods: &[&ExternFn],
370 namespace: &Namespace,
371) {
372 write_include_guard_start(out, namespace, &ety.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700373 for line in ety.doc.to_string().lines() {
374 writeln!(out, "//{}", line);
375 }
376 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700377 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700378 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700379 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700380 write!(out, " ");
381 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700382 let local_name = method.ident.to_string();
383 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700384 writeln!(out, ";");
385 }
386 writeln!(out, "}};");
Adrian Taylora1f890b2020-07-31 15:35:34 -0700387 write_include_guard_end(out, namespace, &ety.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700388}
389
Adrian Taylora1f890b2020-07-31 15:35:34 -0700390fn write_enum(out: &mut OutFile, enm: &Enum, namespace: &Namespace) {
391 write_include_guard_start(out, namespace, &enm.ident);
Joel Galensonc03402a2020-04-23 17:31:09 -0700392 for line in enm.doc.to_string().lines() {
393 writeln!(out, "//{}", line);
394 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700395 write!(out, "enum class {} : ", enm.ident);
396 write_atom(out, enm.repr);
397 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700398 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700399 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700400 }
401 writeln!(out, "}};");
Adrian Taylora1f890b2020-07-31 15:35:34 -0700402 write_include_guard_end(out, namespace, &enm.ident);
Joel Galensonc03402a2020-04-23 17:31:09 -0700403}
404
Joel Galenson905eb2e2020-05-04 14:58:14 -0700405fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700406 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
407 write_atom(out, enm.repr);
408 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700409 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700410 write!(out, "static_assert(static_cast<");
411 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700412 writeln!(
413 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700414 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700415 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700416 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700417 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700418}
419
David Tolnayebef4a22020-03-17 15:33:47 -0700420fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
421 let mut has_cxx_throws = false;
422 for api in apis {
423 if let Api::CxxFunction(efn) = api {
424 if efn.throws {
425 has_cxx_throws = true;
426 break;
427 }
428 }
429 }
430
431 if has_cxx_throws {
432 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700433 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700434 out,
David Tolnay69601622020-04-29 18:48:36 -0700435 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700436 );
437 }
438}
439
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700440fn write_cxx_function_shim(
441 out: &mut OutFile,
442 efn: &ExternFn,
443 types: &Types,
444 impl_annotations: &Option<String>,
445) {
446 if !out.header {
447 if let Some(annotation) = impl_annotations {
448 write!(out, "{} ", annotation);
449 }
450 }
David Tolnayebef4a22020-03-17 15:33:47 -0700451 if efn.throws {
452 write!(out, "::rust::Str::Repr ");
453 } else {
David Tolnay99642622020-03-25 13:07:35 -0700454 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700455 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700456 let mangled = mangle::extern_fn(&out.namespace, efn);
457 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700458 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700459 if receiver.mutability.is_none() {
460 write!(out, "const ");
461 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700462 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700463 }
David Tolnay7db73692019-10-20 14:51:12 -0400464 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700465 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400466 write!(out, ", ");
467 }
David Tolnaya46a2372020-03-06 10:03:48 -0800468 if arg.ty == RustString {
469 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700470 } else if let Type::RustVec(_) = arg.ty {
471 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800472 }
David Tolnay7db73692019-10-20 14:51:12 -0400473 write_extern_arg(out, arg, types);
474 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700475 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400476 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700477 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400478 write!(out, ", ");
479 }
David Tolnay99642622020-03-25 13:07:35 -0700480 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400481 write!(out, "*return$");
482 }
483 writeln!(out, ") noexcept {{");
484 write!(out, " ");
485 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700486 match &efn.receiver {
487 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700488 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700489 }
David Tolnay7db73692019-10-20 14:51:12 -0400490 for (i, arg) in efn.args.iter().enumerate() {
491 if i > 0 {
492 write!(out, ", ");
493 }
494 write_type(out, &arg.ty);
495 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700496 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700497 if let Some(receiver) = &efn.receiver {
498 if receiver.mutability.is_none() {
499 write!(out, " const");
500 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700501 }
502 write!(out, " = ");
503 match &efn.receiver {
504 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700505 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700506 }
507 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400508 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700509 if efn.throws {
510 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700511 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700512 writeln!(out, " [&] {{");
513 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700514 }
David Tolnay7db73692019-10-20 14:51:12 -0400515 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700516 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400517 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700518 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400519 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700520 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400521 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700522 }
523 match &efn.ret {
524 Some(Type::Ref(_)) => write!(out, "&"),
525 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700526 Some(Type::SliceRefU8(_)) if !indirect_return => {
527 write!(out, "::rust::Slice<uint8_t>::Repr(")
528 }
David Tolnay99642622020-03-25 13:07:35 -0700529 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400530 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700531 match &efn.receiver {
532 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700533 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700534 }
David Tolnay7db73692019-10-20 14:51:12 -0400535 for (i, arg) in efn.args.iter().enumerate() {
536 if i > 0 {
537 write!(out, ", ");
538 }
539 if let Type::RustBox(_) = &arg.ty {
540 write_type(out, &arg.ty);
541 write!(out, "::from_raw({})", arg.ident);
542 } else if let Type::UniquePtr(_) = &arg.ty {
543 write_type(out, &arg.ty);
544 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800545 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800546 write!(
547 out,
548 "::rust::String(::rust::unsafe_bitcopy, *{})",
549 arg.ident,
550 );
David Tolnay313b10e2020-04-25 16:30:51 -0700551 } else if let Type::RustVec(_) = arg.ty {
552 write_type(out, &arg.ty);
553 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400554 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700555 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800556 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400557 } else {
558 write!(out, "{}", arg.ident);
559 }
560 }
561 write!(out, ")");
562 match &efn.ret {
563 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
564 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700565 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400566 _ => {}
567 }
568 if indirect_return {
569 write!(out, ")");
570 }
571 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700572 if efn.throws {
573 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700574 writeln!(out, " throw$.ptr = nullptr;");
575 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700576 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700577 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700578 writeln!(
579 out,
David Tolnay69601622020-04-29 18:48:36 -0700580 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700581 );
David Tolnay5d121442020-03-17 22:14:40 -0700582 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700583 writeln!(out, " return throw$;");
584 }
David Tolnay7db73692019-10-20 14:51:12 -0400585 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 for arg in &efn.args {
587 if let Type::Fn(f) = &arg.ty {
588 let var = &arg.ident;
589 write_function_pointer_trampoline(out, efn, var, f, types);
590 }
591 }
592}
593
594fn write_function_pointer_trampoline(
595 out: &mut OutFile,
596 efn: &ExternFn,
597 var: &Ident,
598 f: &Signature,
599 types: &Types,
600) {
601 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700602 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 let indirect_call = true;
604 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
605
606 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700607 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700608 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400609}
610
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700611fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700612 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700613 let indirect_call = false;
614 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
615}
616
617fn write_rust_function_decl_impl(
618 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700619 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700620 sig: &Signature,
621 types: &Types,
622 indirect_call: bool,
623) {
624 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700625 write!(out, "::rust::Str::Repr ");
626 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700628 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700629 write!(out, "{}(", link_name);
630 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700631 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700632 if receiver.mutability.is_none() {
633 write!(out, "const ");
634 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700635 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700636 needs_comma = true;
637 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700638 for arg in &sig.args {
639 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400640 write!(out, ", ");
641 }
642 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700643 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400644 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700645 if indirect_return(sig, types) {
646 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400647 write!(out, ", ");
648 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700649 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400650 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700651 needs_comma = true;
652 }
653 if indirect_call {
654 if needs_comma {
655 write!(out, ", ");
656 }
657 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400658 }
659 writeln!(out, ") noexcept;");
660}
661
662fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400663 for line in efn.doc.to_string().lines() {
664 writeln!(out, "//{}", line);
665 }
David Tolnaya73853b2020-04-20 01:19:56 -0700666 let local_name = match &efn.sig.receiver {
667 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700668 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700669 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700670 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700671 let indirect_call = false;
672 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
673}
674
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700675fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700676 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700677 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700678 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700679 indirect_call: bool,
680) {
681 write_return_type(out, &sig.ret);
682 write!(out, "{}(", local_name);
683 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400684 if i > 0 {
685 write!(out, ", ");
686 }
687 write_type_space(out, &arg.ty);
688 write!(out, "{}", arg.ident);
689 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700690 if indirect_call {
691 if !sig.args.is_empty() {
692 write!(out, ", ");
693 }
694 write!(out, "void *extern$");
695 }
David Tolnay1e548172020-03-16 13:37:09 -0700696 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700697 if let Some(receiver) = &sig.receiver {
698 if receiver.mutability.is_none() {
699 write!(out, " const");
700 }
701 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700702 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700703 write!(out, " noexcept");
704 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700705}
706
707fn write_rust_function_shim_impl(
708 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700709 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700710 sig: &Signature,
711 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700712 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700713 indirect_call: bool,
714) {
715 if out.header && sig.receiver.is_some() {
716 // We've already defined this inside the struct.
717 return;
718 }
David Tolnaya73853b2020-04-20 01:19:56 -0700719 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400720 if out.header {
721 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700722 return;
David Tolnay7db73692019-10-20 14:51:12 -0400723 }
David Tolnay439cde22020-04-20 00:46:25 -0700724 writeln!(out, " {{");
725 for arg in &sig.args {
726 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
727 out.include.utility = true;
728 write!(out, " ::rust::ManuallyDrop<");
729 write_type(out, &arg.ty);
730 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
731 }
732 }
733 write!(out, " ");
734 let indirect_return = indirect_return(sig, types);
735 if indirect_return {
736 write!(out, "::rust::MaybeUninit<");
737 write_type(out, sig.ret.as_ref().unwrap());
738 writeln!(out, "> return$;");
739 write!(out, " ");
740 } else if let Some(ret) = &sig.ret {
741 write!(out, "return ");
742 match ret {
743 Type::RustBox(_) => {
744 write_type(out, ret);
745 write!(out, "::from_raw(");
746 }
747 Type::UniquePtr(_) => {
748 write_type(out, ret);
749 write!(out, "(");
750 }
751 Type::Ref(_) => write!(out, "*"),
752 _ => {}
753 }
754 }
755 if sig.throws {
756 write!(out, "::rust::Str::Repr error$ = ");
757 }
758 write!(out, "{}(", invoke);
759 if sig.receiver.is_some() {
760 write!(out, "*this");
761 }
762 for (i, arg) in sig.args.iter().enumerate() {
763 if i > 0 || sig.receiver.is_some() {
764 write!(out, ", ");
765 }
766 match &arg.ty {
767 Type::Str(_) => write!(out, "::rust::Str::Repr("),
768 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
769 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
770 _ => {}
771 }
772 write!(out, "{}", arg.ident);
773 match &arg.ty {
774 Type::RustBox(_) => write!(out, ".into_raw()"),
775 Type::UniquePtr(_) => write!(out, ".release()"),
776 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
777 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
778 _ => {}
779 }
780 }
781 if indirect_return {
782 if !sig.args.is_empty() {
783 write!(out, ", ");
784 }
785 write!(out, "&return$.value");
786 }
787 if indirect_call {
788 if !sig.args.is_empty() || indirect_return {
789 write!(out, ", ");
790 }
791 write!(out, "extern$");
792 }
793 write!(out, ")");
794 if let Some(ret) = &sig.ret {
795 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
796 write!(out, ")");
797 }
798 }
799 writeln!(out, ";");
800 if sig.throws {
801 writeln!(out, " if (error$.ptr) {{");
802 writeln!(out, " throw ::rust::Error(error$);");
803 writeln!(out, " }}");
804 }
805 if indirect_return {
806 out.include.utility = true;
807 writeln!(out, " return ::std::move(return$.value);");
808 }
809 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400810}
811
812fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
813 match ty {
814 None => write!(out, "void "),
815 Some(ty) => write_type_space(out, ty),
816 }
817}
818
David Tolnay75dca2e2020-03-25 20:17:52 -0700819fn indirect_return(sig: &Signature, types: &Types) -> bool {
820 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700821 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700822 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700823}
824
David Tolnay99642622020-03-25 13:07:35 -0700825fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
826 match ty {
827 Type::RustBox(ty) | Type::UniquePtr(ty) => {
828 write_type_space(out, &ty.inner);
829 write!(out, "*");
830 }
831 Type::Ref(ty) => {
832 if ty.mutability.is_none() {
833 write!(out, "const ");
834 }
835 write_type(out, &ty.inner);
836 write!(out, " *");
837 }
838 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700839 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700840 _ => write_type(out, ty),
841 }
842}
843
844fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
845 write_indirect_return_type(out, ty);
846 match ty {
847 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700848 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700849 _ => write_space_after_type(out, ty),
850 }
851}
852
853fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400854 match ty {
855 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
856 write_type_space(out, &ty.inner);
857 write!(out, "*");
858 }
David Tolnay4a441222020-01-25 16:24:27 -0800859 Some(Type::Ref(ty)) => {
860 if ty.mutability.is_none() {
861 write!(out, "const ");
862 }
863 write_type(out, &ty.inner);
864 write!(out, " *");
865 }
David Tolnay750755e2020-03-01 13:04:08 -0800866 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700867 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400868 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
869 _ => write_return_type(out, ty),
870 }
871}
872
873fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
874 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700875 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400876 write_type_space(out, &ty.inner);
877 write!(out, "*");
878 }
David Tolnay750755e2020-03-01 13:04:08 -0800879 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700880 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400881 _ => write_type_space(out, &arg.ty),
882 }
883 if types.needs_indirect_abi(&arg.ty) {
884 write!(out, "*");
885 }
886 write!(out, "{}", arg.ident);
887}
888
889fn write_type(out: &mut OutFile, ty: &Type) {
890 match ty {
891 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700892 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400893 None => write!(out, "{}", ident),
894 },
895 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800896 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400897 write_type(out, &ty.inner);
898 write!(out, ">");
899 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700900 Type::RustVec(ty) => {
901 write!(out, "::rust::Vec<");
902 write_type(out, &ty.inner);
903 write!(out, ">");
904 }
David Tolnay7db73692019-10-20 14:51:12 -0400905 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800906 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400907 write_type(out, &ptr.inner);
908 write!(out, ">");
909 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700910 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700911 write!(out, "::std::vector<");
912 write_type(out, &ty.inner);
913 write!(out, ">");
914 }
David Tolnay7db73692019-10-20 14:51:12 -0400915 Type::Ref(r) => {
916 if r.mutability.is_none() {
917 write!(out, "const ");
918 }
919 write_type(out, &r.inner);
920 write!(out, " &");
921 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700922 Type::Slice(_) => {
923 // For now, only U8 slices are supported, which are covered separately below
924 unreachable!()
925 }
David Tolnay7db73692019-10-20 14:51:12 -0400926 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800927 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400928 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700929 Type::SliceRefU8(_) => {
930 write!(out, "::rust::Slice<uint8_t>");
931 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700932 Type::Fn(f) => {
933 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
934 match &f.ret {
935 Some(ret) => write_type(out, ret),
936 None => write!(out, "void"),
937 }
938 write!(out, "(");
939 for (i, arg) in f.args.iter().enumerate() {
940 if i > 0 {
941 write!(out, ", ");
942 }
943 write_type(out, &arg.ty);
944 }
945 write!(out, ")>");
946 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700947 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400948 }
949}
950
David Tolnayf6a89f22020-05-10 23:39:27 -0700951fn write_atom(out: &mut OutFile, atom: Atom) {
952 match atom {
953 Bool => write!(out, "bool"),
954 U8 => write!(out, "uint8_t"),
955 U16 => write!(out, "uint16_t"),
956 U32 => write!(out, "uint32_t"),
957 U64 => write!(out, "uint64_t"),
958 Usize => write!(out, "size_t"),
959 I8 => write!(out, "int8_t"),
960 I16 => write!(out, "int16_t"),
961 I32 => write!(out, "int32_t"),
962 I64 => write!(out, "int64_t"),
963 Isize => write!(out, "::rust::isize"),
964 F32 => write!(out, "float"),
965 F64 => write!(out, "double"),
966 CxxString => write!(out, "::std::string"),
967 RustString => write!(out, "::rust::String"),
968 }
969}
970
David Tolnay7db73692019-10-20 14:51:12 -0400971fn write_type_space(out: &mut OutFile, ty: &Type) {
972 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700973 write_space_after_type(out, ty);
974}
975
976fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400977 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700978 Type::Ident(_)
979 | Type::RustBox(_)
980 | Type::UniquePtr(_)
981 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700982 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700983 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700984 | Type::SliceRefU8(_)
985 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400986 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700987 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400988 }
989}
990
David Tolnaycd08c442020-04-25 10:16:33 -0700991// Only called for legal referent types of unique_ptr and element types of
992// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700993fn to_typename(namespace: &Namespace, ty: &Type) -> String {
994 match ty {
995 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700996 let mut path = String::new();
997 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700998 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700999 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001000 }
David Tolnaycd08c442020-04-25 10:16:33 -07001001 path += &ident.to_string();
1002 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001003 }
1004 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001005 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001006 }
1007}
1008
David Tolnayacdf20a2020-04-25 12:40:53 -07001009// Only called for legal referent types of unique_ptr and element types of
1010// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001011fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1012 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001013 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001014 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001015 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001016 }
1017}
1018
David Tolnay7db73692019-10-20 14:51:12 -04001019fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001020 out.begin_block("extern \"C\"");
1021 for ty in types {
1022 if let Type::RustBox(ty) = ty {
1023 if let Type::Ident(inner) = &ty.inner {
1024 out.next_section();
1025 write_rust_box_extern(out, inner);
1026 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001027 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001028 if let Type::Ident(inner) = &ty.inner {
1029 if Atom::from(inner).is_none() {
1030 out.next_section();
1031 write_rust_vec_extern(out, inner);
1032 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001033 }
David Tolnay7db73692019-10-20 14:51:12 -04001034 } else if let Type::UniquePtr(ptr) = ty {
1035 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001036 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001037 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001038 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001039 }
1040 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001041 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001042 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001043 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001044 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001045 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001046 }
1047 }
1048 }
1049 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001050 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001051
David Tolnay750755e2020-03-01 13:04:08 -08001052 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001053 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001054 for ty in types {
1055 if let Type::RustBox(ty) = ty {
1056 if let Type::Ident(inner) = &ty.inner {
1057 write_rust_box_impl(out, inner);
1058 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001059 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001060 if let Type::Ident(inner) = &ty.inner {
1061 if Atom::from(inner).is_none() {
1062 write_rust_vec_impl(out, inner);
1063 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001064 }
David Tolnay7db73692019-10-20 14:51:12 -04001065 }
1066 }
David Tolnay69601622020-04-29 18:48:36 -07001067 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001068 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001069}
1070
1071fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1072 let mut inner = String::new();
1073 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001074 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001075 inner += "::";
1076 }
1077 inner += &ident.to_string();
1078 let instance = inner.replace("::", "$");
1079
David Tolnay69601622020-04-29 18:48:36 -07001080 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1081 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001082 writeln!(
1083 out,
David Tolnay69601622020-04-29 18:48:36 -07001084 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001085 instance, inner,
1086 );
1087 writeln!(
1088 out,
David Tolnay69601622020-04-29 18:48:36 -07001089 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001090 instance, inner,
1091 );
David Tolnay69601622020-04-29 18:48:36 -07001092 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001093}
1094
David Tolnay6787be62020-04-25 11:01:02 -07001095fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1096 let element = Type::Ident(element.clone());
1097 let inner = to_typename(&out.namespace, &element);
1098 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001099
David Tolnay69601622020-04-29 18:48:36 -07001100 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1101 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001102 writeln!(
1103 out,
David Tolnay69601622020-04-29 18:48:36 -07001104 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001105 instance, inner,
1106 );
1107 writeln!(
1108 out,
David Tolnay69601622020-04-29 18:48:36 -07001109 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001110 instance, inner,
1111 );
1112 writeln!(
1113 out,
David Tolnay69601622020-04-29 18:48:36 -07001114 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001115 instance, inner,
1116 );
David Tolnay219c0792020-04-24 20:31:37 -07001117 writeln!(
1118 out,
David Tolnay69601622020-04-29 18:48:36 -07001119 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001120 inner, instance,
1121 );
David Tolnay503d0192020-04-24 22:18:56 -07001122 writeln!(
1123 out,
David Tolnay69601622020-04-29 18:48:36 -07001124 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001125 instance,
1126 );
David Tolnay69601622020-04-29 18:48:36 -07001127 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001128}
1129
David Tolnay7db73692019-10-20 14:51:12 -04001130fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1131 let mut inner = String::new();
1132 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001133 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001134 inner += "::";
1135 }
1136 inner += &ident.to_string();
1137 let instance = inner.replace("::", "$");
1138
1139 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001140 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001141 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001142 writeln!(out, "}}");
1143
1144 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001145 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001146 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001147 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001148}
1149
David Tolnay6787be62020-04-25 11:01:02 -07001150fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1151 let element = Type::Ident(element.clone());
1152 let inner = to_typename(&out.namespace, &element);
1153 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001154
Myron Ahneba35cf2020-02-05 19:41:51 +07001155 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001156 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001157 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001158 writeln!(out, "}}");
1159
1160 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001161 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1162 writeln!(
1163 out,
David Tolnay69601622020-04-29 18:48:36 -07001164 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001165 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001166 );
1167 writeln!(out, "}}");
1168
1169 writeln!(out, "template <>");
1170 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001171 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001172 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001173
1174 writeln!(out, "template <>");
1175 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1176 writeln!(
1177 out,
David Tolnay69601622020-04-29 18:48:36 -07001178 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001179 instance,
1180 );
1181 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001182
1183 writeln!(out, "template <>");
1184 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001185 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001186 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001187}
1188
David Tolnay63da4d32020-04-25 09:41:12 -07001189fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1190 let ty = Type::Ident(ident.clone());
1191 let instance = to_mangled(&out.namespace, &ty);
1192
David Tolnay69601622020-04-29 18:48:36 -07001193 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1194 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001195
1196 write_unique_ptr_common(out, &ty, types);
1197
David Tolnay69601622020-04-29 18:48:36 -07001198 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001199}
1200
1201// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1202fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001203 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001204 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001205 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001206 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001207
David Tolnay63da4d32020-04-25 09:41:12 -07001208 let can_construct_from_value = match ty {
1209 Type::Ident(ident) => types.structs.contains_key(ident),
1210 _ => false,
1211 };
1212
David Tolnay7db73692019-10-20 14:51:12 -04001213 writeln!(
1214 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001215 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001216 inner,
1217 );
1218 writeln!(
1219 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001220 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001221 inner,
1222 );
1223 writeln!(
1224 out,
David Tolnay69601622020-04-29 18:48:36 -07001225 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001226 instance, inner,
1227 );
David Tolnay7e219b82020-03-01 13:14:51 -08001228 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001229 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001230 if can_construct_from_value {
1231 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001232 out,
David Tolnay69601622020-04-29 18:48:36 -07001233 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001234 instance, inner, inner,
1235 );
David Tolnay63da4d32020-04-25 09:41:12 -07001236 writeln!(
1237 out,
1238 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1239 inner, inner,
1240 );
1241 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001242 }
David Tolnay7db73692019-10-20 14:51:12 -04001243 writeln!(
1244 out,
David Tolnay69601622020-04-29 18:48:36 -07001245 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001246 instance, inner, inner,
1247 );
David Tolnay7e219b82020-03-01 13:14:51 -08001248 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001249 writeln!(out, "}}");
1250 writeln!(
1251 out,
David Tolnay69601622020-04-29 18:48:36 -07001252 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001253 inner, instance, inner,
1254 );
1255 writeln!(out, " return ptr.get();");
1256 writeln!(out, "}}");
1257 writeln!(
1258 out,
David Tolnay69601622020-04-29 18:48:36 -07001259 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001260 inner, instance, inner,
1261 );
1262 writeln!(out, " return ptr.release();");
1263 writeln!(out, "}}");
1264 writeln!(
1265 out,
David Tolnay69601622020-04-29 18:48:36 -07001266 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001267 instance, inner,
1268 );
1269 writeln!(out, " ptr->~unique_ptr();");
1270 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001271}
Myron Ahneba35cf2020-02-05 19:41:51 +07001272
David Tolnay92105da2020-04-25 11:15:31 -07001273fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001274 let element = Type::Ident(element.clone());
1275 let inner = to_typename(&out.namespace, &element);
1276 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001277
David Tolnay69601622020-04-29 18:48:36 -07001278 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1279 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001280 writeln!(
1281 out,
David Tolnay69601622020-04-29 18:48:36 -07001282 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001283 instance, inner,
1284 );
1285 writeln!(out, " return s.size();");
1286 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001287 writeln!(
1288 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001289 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001290 inner, instance, inner,
1291 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001292 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001293 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001294
1295 write_unique_ptr_common(out, vector_ty, types);
1296
David Tolnay69601622020-04-29 18:48:36 -07001297 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001298}