blob: 1196837d05e796f893fc8b3effac901ae2e7bdc4 [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 Tolnay54702b92020-07-31 11:50:09 -070020 if header {
21 writeln!(out.front, "#pragma once");
22 }
23
David Tolnay33d30292020-03-18 18:02:02 -070024 out.include.extend(opt.include);
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();
65 write_struct(out, strct);
66 }
Joel Galensonc03402a2020-04-23 17:31:09 -070067 Api::Enum(enm) => {
68 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070069 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070070 check_enum(out, enm);
71 } else {
72 write_enum(out, enm);
73 }
Joel Galensonc03402a2020-04-23 17:31:09 -070074 }
David Tolnayc1fe0052020-04-17 15:15:06 -070075 Api::RustType(ety) => {
76 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070077 out.next_section();
78 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070079 }
David Tolnayc1fe0052020-04-17 15:15:06 -070080 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070081 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040082 }
83 }
84
85 if !header {
86 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070087 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040088 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070089 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040090 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
91 Api::RustFunction(efn) => (efn, write_rust_function_decl),
92 _ => continue,
93 };
94 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -070095 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -040096 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080097 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040098 }
99
100 for api in apis {
101 if let Api::RustFunction(efn) = api {
102 out.next_section();
103 write_rust_function_shim(out, efn, types);
104 }
105 }
106
107 out.next_section();
108 for name in namespace.iter().rev() {
109 writeln!(out, "}} // namespace {}", name);
110 }
111
112 if !header {
113 out.next_section();
114 write_generic_instantiations(out, types);
115 }
116
David Tolnay54702b92020-07-31 11:50:09 -0700117 write!(out.front, "{}", out.include);
118
119 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400120}
121
122fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400123 for ty in types {
124 match ty {
125 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700126 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
127 | Some(I64) => out.include.cstdint = true,
128 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800129 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700130 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400131 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800132 Type::RustBox(_) => out.include.type_traits = true,
133 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700134 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700135 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700136 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400137 }
138 }
David Tolnay7db73692019-10-20 14:51:12 -0400139}
140
David Tolnayf51447e2020-03-06 14:14:27 -0800141fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 let mut needs_rust_string = false;
143 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700144 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400145 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700146 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700147 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700148 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400149 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700150 match ty {
151 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700152 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700153 out.include.type_traits = true;
154 needs_rust_box = true;
155 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700156 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700157 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700158 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700159 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700160 needs_rust_vec = true;
161 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700162 Type::Str(_) => {
163 out.include.cstdint = true;
164 out.include.string = true;
165 needs_rust_str = true;
166 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700167 Type::Fn(_) => {
168 needs_rust_fn = true;
169 }
David Tolnay4770b472020-04-14 16:32:59 -0700170 Type::Slice(_) | Type::SliceRefU8(_) => {
171 needs_rust_slice = true;
172 }
David Tolnay7c295462020-04-25 12:45:07 -0700173 ty if ty == Isize => {
174 out.include.base_tsd = true;
175 needs_rust_isize = true;
176 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700177 ty if ty == RustString => {
178 out.include.array = true;
179 out.include.cstdint = true;
180 out.include.string = true;
181 needs_rust_string = true;
182 }
183 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400184 }
185 }
186
David Tolnayb7a7cb62020-03-17 21:18:40 -0700187 let mut needs_rust_error = false;
188 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800189 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800190 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700191 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800192 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 match api {
194 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700195 if efn.throws {
196 needs_trycatch = true;
197 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700198 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700199 let bitcopy = match arg.ty {
200 Type::RustVec(_) => true,
201 _ => arg.ty == RustString,
202 };
203 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700204 needs_unsafe_bitcopy = true;
205 break;
206 }
David Tolnay09011c32020-03-06 14:40:28 -0800207 }
208 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700209 Api::RustFunction(efn) if !out.header => {
210 if efn.throws {
211 out.include.exception = true;
212 needs_rust_error = true;
213 }
214 for arg in &efn.args {
215 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
216 needs_manually_drop = true;
217 break;
218 }
219 }
220 if let Some(ret) = &efn.ret {
221 if types.needs_indirect_abi(ret) {
222 needs_maybe_uninit = true;
223 }
David Tolnayf51447e2020-03-06 14:14:27 -0800224 }
225 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800227 }
228 }
229
David Tolnay750755e2020-03-01 13:04:08 -0800230 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700231 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800232
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 if needs_rust_string
234 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700235 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700236 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700237 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700238 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700240 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 || needs_unsafe_bitcopy
242 || needs_manually_drop
243 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700244 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 {
David Tolnay736cbca2020-03-11 16:49:18 -0700246 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800247 }
248
David Tolnay32ee9d32020-05-12 20:01:09 -0700249 if needs_rust_string || needs_rust_vec {
David Tolnayd1402742020-03-25 22:21:42 -0700250 out.next_section();
251 writeln!(out, "struct unsafe_bitcopy_t;");
252 }
253
David Tolnaye54f3382020-05-12 19:58:36 -0700254 include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
255 include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
256 include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
257 include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
258 include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
259 include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
260 include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
261 include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
262 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800263
264 if needs_manually_drop {
265 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700266 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800267 writeln!(out, "template <typename T>");
268 writeln!(out, "union ManuallyDrop {{");
269 writeln!(out, " T value;");
270 writeln!(
271 out,
272 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
273 );
274 writeln!(out, " ~ManuallyDrop() {{}}");
275 writeln!(out, "}};");
276 }
277
David Tolnay09011c32020-03-06 14:40:28 -0800278 if needs_maybe_uninit {
279 out.next_section();
280 writeln!(out, "template <typename T>");
281 writeln!(out, "union MaybeUninit {{");
282 writeln!(out, " T value;");
283 writeln!(out, " MaybeUninit() {{}}");
284 writeln!(out, " ~MaybeUninit() {{}}");
285 writeln!(out, "}};");
286 }
287
David Tolnay69601622020-04-29 18:48:36 -0700288 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700289
David Tolnay5d121442020-03-17 22:14:40 -0700290 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700291 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700292 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700293 out.include.type_traits = true;
294 out.include.utility = true;
295 writeln!(out, "class missing {{}};");
296 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700297 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700298 writeln!(out, "template <typename Try, typename Fail>");
299 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700300 writeln!(
301 out,
David Tolnay04722332020-03-18 11:31:54 -0700302 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700303 );
David Tolnay04722332020-03-18 11:31:54 -0700304 writeln!(out, " missing>::value>::type");
305 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700306 writeln!(out, " func();");
307 writeln!(out, "}} catch (const ::std::exception &e) {{");
308 writeln!(out, " fail(e.what());");
309 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700310 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700311 }
312
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800313 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400314}
315
316fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay57003b02020-07-31 20:30:29 -0700317 writeln!(
318 out,
319 "#ifndef CXXBRIDGE03_STRUCT_{}{}",
320 out.namespace, strct.ident,
321 );
322 writeln!(
323 out,
324 "#define CXXBRIDGE03_STRUCT_{}{}",
325 out.namespace, strct.ident,
326 );
David Tolnay7db73692019-10-20 14:51:12 -0400327 for line in strct.doc.to_string().lines() {
328 writeln!(out, "//{}", line);
329 }
330 writeln!(out, "struct {} final {{", strct.ident);
331 for field in &strct.fields {
332 write!(out, " ");
333 write_type_space(out, &field.ty);
334 writeln!(out, "{};", field.ident);
335 }
336 writeln!(out, "}};");
David Tolnay57003b02020-07-31 20:30:29 -0700337 writeln!(
338 out,
339 "#endif // CXXBRIDGE03_STRUCT_{}{}",
340 out.namespace, strct.ident,
341 );
David Tolnay7db73692019-10-20 14:51:12 -0400342}
343
344fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
345 writeln!(out, "struct {};", ident);
346}
347
David Tolnay8861bee2020-01-20 18:39:24 -0800348fn write_struct_using(out: &mut OutFile, ident: &Ident) {
349 writeln!(out, "using {} = {};", ident, ident);
350}
351
David Tolnayc1fe0052020-04-17 15:15:06 -0700352fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay57003b02020-07-31 20:30:29 -0700353 writeln!(
354 out,
355 "#ifndef CXXBRIDGE03_STRUCT_{}{}",
356 out.namespace, ety.ident,
357 );
358 writeln!(
359 out,
360 "#define CXXBRIDGE03_STRUCT_{}{}",
361 out.namespace, ety.ident,
362 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700363 for line in ety.doc.to_string().lines() {
364 writeln!(out, "//{}", line);
365 }
366 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700367 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700368 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700369 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700370 write!(out, " ");
371 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700372 let local_name = method.ident.to_string();
373 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700374 writeln!(out, ";");
375 }
376 writeln!(out, "}};");
David Tolnay57003b02020-07-31 20:30:29 -0700377 writeln!(
378 out,
379 "#endif // CXXBRIDGE03_STRUCT_{}{}",
380 out.namespace, ety.ident,
381 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700382}
383
Joel Galensonc03402a2020-04-23 17:31:09 -0700384fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay57003b02020-07-31 20:30:29 -0700385 writeln!(
386 out,
387 "#ifndef CXXBRIDGE03_ENUM_{}{}",
388 out.namespace, enm.ident,
389 );
390 writeln!(
391 out,
392 "#define CXXBRIDGE03_ENUM_{}{}",
393 out.namespace, enm.ident,
394 );
Joel Galensonc03402a2020-04-23 17:31:09 -0700395 for line in enm.doc.to_string().lines() {
396 writeln!(out, "//{}", line);
397 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700398 write!(out, "enum class {} : ", enm.ident);
399 write_atom(out, enm.repr);
400 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700401 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700402 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700403 }
404 writeln!(out, "}};");
David Tolnay57003b02020-07-31 20:30:29 -0700405 writeln!(
406 out,
407 "#endif // CXXBRIDGE03_ENUM_{}{}",
408 out.namespace, enm.ident,
409 );
Joel Galensonc03402a2020-04-23 17:31:09 -0700410}
411
Joel Galenson905eb2e2020-05-04 14:58:14 -0700412fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700413 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
414 write_atom(out, enm.repr);
415 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700416 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700417 write!(out, "static_assert(static_cast<");
418 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700419 writeln!(
420 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700421 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700422 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700423 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700424 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700425}
426
David Tolnayebef4a22020-03-17 15:33:47 -0700427fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
428 let mut has_cxx_throws = false;
429 for api in apis {
430 if let Api::CxxFunction(efn) = api {
431 if efn.throws {
432 has_cxx_throws = true;
433 break;
434 }
435 }
436 }
437
438 if has_cxx_throws {
439 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700440 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700441 out,
David Tolnay69601622020-04-29 18:48:36 -0700442 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700443 );
444 }
445}
446
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700447fn write_cxx_function_shim(
448 out: &mut OutFile,
449 efn: &ExternFn,
450 types: &Types,
451 impl_annotations: &Option<String>,
452) {
453 if !out.header {
454 if let Some(annotation) = impl_annotations {
455 write!(out, "{} ", annotation);
456 }
457 }
David Tolnayebef4a22020-03-17 15:33:47 -0700458 if efn.throws {
459 write!(out, "::rust::Str::Repr ");
460 } else {
David Tolnay99642622020-03-25 13:07:35 -0700461 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700462 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700463 let mangled = mangle::extern_fn(&out.namespace, efn);
464 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700465 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700466 if receiver.mutability.is_none() {
467 write!(out, "const ");
468 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700469 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700470 }
David Tolnay7db73692019-10-20 14:51:12 -0400471 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700472 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400473 write!(out, ", ");
474 }
David Tolnaya46a2372020-03-06 10:03:48 -0800475 if arg.ty == RustString {
476 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700477 } else if let Type::RustVec(_) = arg.ty {
478 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800479 }
David Tolnay7db73692019-10-20 14:51:12 -0400480 write_extern_arg(out, arg, types);
481 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700482 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400483 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700484 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400485 write!(out, ", ");
486 }
David Tolnay99642622020-03-25 13:07:35 -0700487 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400488 write!(out, "*return$");
489 }
490 writeln!(out, ") noexcept {{");
491 write!(out, " ");
492 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700493 match &efn.receiver {
494 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700495 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700496 }
David Tolnay7db73692019-10-20 14:51:12 -0400497 for (i, arg) in efn.args.iter().enumerate() {
498 if i > 0 {
499 write!(out, ", ");
500 }
501 write_type(out, &arg.ty);
502 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700503 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700504 if let Some(receiver) = &efn.receiver {
505 if receiver.mutability.is_none() {
506 write!(out, " const");
507 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700508 }
509 write!(out, " = ");
510 match &efn.receiver {
511 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700512 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700513 }
514 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400515 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700516 if efn.throws {
517 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700518 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700519 writeln!(out, " [&] {{");
520 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700521 }
David Tolnay7db73692019-10-20 14:51:12 -0400522 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700523 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400524 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700525 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400526 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700527 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400528 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700529 }
530 match &efn.ret {
531 Some(Type::Ref(_)) => write!(out, "&"),
532 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700533 Some(Type::SliceRefU8(_)) if !indirect_return => {
534 write!(out, "::rust::Slice<uint8_t>::Repr(")
535 }
David Tolnay99642622020-03-25 13:07:35 -0700536 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400537 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700538 match &efn.receiver {
539 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700540 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700541 }
David Tolnay7db73692019-10-20 14:51:12 -0400542 for (i, arg) in efn.args.iter().enumerate() {
543 if i > 0 {
544 write!(out, ", ");
545 }
546 if let Type::RustBox(_) = &arg.ty {
547 write_type(out, &arg.ty);
548 write!(out, "::from_raw({})", arg.ident);
549 } else if let Type::UniquePtr(_) = &arg.ty {
550 write_type(out, &arg.ty);
551 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800552 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800553 write!(
554 out,
555 "::rust::String(::rust::unsafe_bitcopy, *{})",
556 arg.ident,
557 );
David Tolnay313b10e2020-04-25 16:30:51 -0700558 } else if let Type::RustVec(_) = arg.ty {
559 write_type(out, &arg.ty);
560 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400561 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700562 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800563 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400564 } else {
565 write!(out, "{}", arg.ident);
566 }
567 }
568 write!(out, ")");
569 match &efn.ret {
570 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
571 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700572 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400573 _ => {}
574 }
575 if indirect_return {
576 write!(out, ")");
577 }
578 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700579 if efn.throws {
580 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700581 writeln!(out, " throw$.ptr = nullptr;");
582 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700583 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700584 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700585 writeln!(
586 out,
David Tolnay69601622020-04-29 18:48:36 -0700587 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700588 );
David Tolnay5d121442020-03-17 22:14:40 -0700589 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700590 writeln!(out, " return throw$;");
591 }
David Tolnay7db73692019-10-20 14:51:12 -0400592 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700593 for arg in &efn.args {
594 if let Type::Fn(f) = &arg.ty {
595 let var = &arg.ident;
596 write_function_pointer_trampoline(out, efn, var, f, types);
597 }
598 }
599}
600
601fn write_function_pointer_trampoline(
602 out: &mut OutFile,
603 efn: &ExternFn,
604 var: &Ident,
605 f: &Signature,
606 types: &Types,
607) {
608 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700609 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700610 let indirect_call = true;
611 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
612
613 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700614 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700615 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400616}
617
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700618fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700619 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700620 let indirect_call = false;
621 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
622}
623
624fn write_rust_function_decl_impl(
625 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700626 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 sig: &Signature,
628 types: &Types,
629 indirect_call: bool,
630) {
631 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700632 write!(out, "::rust::Str::Repr ");
633 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700634 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700635 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700636 write!(out, "{}(", link_name);
637 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700638 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700639 if receiver.mutability.is_none() {
640 write!(out, "const ");
641 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700642 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700643 needs_comma = true;
644 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700645 for arg in &sig.args {
646 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400647 write!(out, ", ");
648 }
649 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700650 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400651 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 if indirect_return(sig, types) {
653 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400654 write!(out, ", ");
655 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400657 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700658 needs_comma = true;
659 }
660 if indirect_call {
661 if needs_comma {
662 write!(out, ", ");
663 }
664 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400665 }
666 writeln!(out, ") noexcept;");
667}
668
669fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400670 for line in efn.doc.to_string().lines() {
671 writeln!(out, "//{}", line);
672 }
David Tolnaya73853b2020-04-20 01:19:56 -0700673 let local_name = match &efn.sig.receiver {
674 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700675 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700676 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700677 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700678 let indirect_call = false;
679 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
680}
681
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700682fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700683 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700684 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700685 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700686 indirect_call: bool,
687) {
688 write_return_type(out, &sig.ret);
689 write!(out, "{}(", local_name);
690 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400691 if i > 0 {
692 write!(out, ", ");
693 }
694 write_type_space(out, &arg.ty);
695 write!(out, "{}", arg.ident);
696 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700697 if indirect_call {
698 if !sig.args.is_empty() {
699 write!(out, ", ");
700 }
701 write!(out, "void *extern$");
702 }
David Tolnay1e548172020-03-16 13:37:09 -0700703 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700704 if let Some(receiver) = &sig.receiver {
705 if receiver.mutability.is_none() {
706 write!(out, " const");
707 }
708 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700709 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700710 write!(out, " noexcept");
711 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700712}
713
714fn write_rust_function_shim_impl(
715 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700716 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700717 sig: &Signature,
718 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700719 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700720 indirect_call: bool,
721) {
722 if out.header && sig.receiver.is_some() {
723 // We've already defined this inside the struct.
724 return;
725 }
David Tolnaya73853b2020-04-20 01:19:56 -0700726 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400727 if out.header {
728 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700729 return;
David Tolnay7db73692019-10-20 14:51:12 -0400730 }
David Tolnay439cde22020-04-20 00:46:25 -0700731 writeln!(out, " {{");
732 for arg in &sig.args {
733 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
734 out.include.utility = true;
735 write!(out, " ::rust::ManuallyDrop<");
736 write_type(out, &arg.ty);
737 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
738 }
739 }
740 write!(out, " ");
741 let indirect_return = indirect_return(sig, types);
742 if indirect_return {
743 write!(out, "::rust::MaybeUninit<");
744 write_type(out, sig.ret.as_ref().unwrap());
745 writeln!(out, "> return$;");
746 write!(out, " ");
747 } else if let Some(ret) = &sig.ret {
748 write!(out, "return ");
749 match ret {
750 Type::RustBox(_) => {
751 write_type(out, ret);
752 write!(out, "::from_raw(");
753 }
754 Type::UniquePtr(_) => {
755 write_type(out, ret);
756 write!(out, "(");
757 }
758 Type::Ref(_) => write!(out, "*"),
759 _ => {}
760 }
761 }
762 if sig.throws {
763 write!(out, "::rust::Str::Repr error$ = ");
764 }
765 write!(out, "{}(", invoke);
766 if sig.receiver.is_some() {
767 write!(out, "*this");
768 }
769 for (i, arg) in sig.args.iter().enumerate() {
770 if i > 0 || sig.receiver.is_some() {
771 write!(out, ", ");
772 }
773 match &arg.ty {
774 Type::Str(_) => write!(out, "::rust::Str::Repr("),
775 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
776 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
777 _ => {}
778 }
779 write!(out, "{}", arg.ident);
780 match &arg.ty {
781 Type::RustBox(_) => write!(out, ".into_raw()"),
782 Type::UniquePtr(_) => write!(out, ".release()"),
783 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
784 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
785 _ => {}
786 }
787 }
788 if indirect_return {
789 if !sig.args.is_empty() {
790 write!(out, ", ");
791 }
792 write!(out, "&return$.value");
793 }
794 if indirect_call {
795 if !sig.args.is_empty() || indirect_return {
796 write!(out, ", ");
797 }
798 write!(out, "extern$");
799 }
800 write!(out, ")");
801 if let Some(ret) = &sig.ret {
802 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
803 write!(out, ")");
804 }
805 }
806 writeln!(out, ";");
807 if sig.throws {
808 writeln!(out, " if (error$.ptr) {{");
809 writeln!(out, " throw ::rust::Error(error$);");
810 writeln!(out, " }}");
811 }
812 if indirect_return {
813 out.include.utility = true;
814 writeln!(out, " return ::std::move(return$.value);");
815 }
816 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400817}
818
819fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
820 match ty {
821 None => write!(out, "void "),
822 Some(ty) => write_type_space(out, ty),
823 }
824}
825
David Tolnay75dca2e2020-03-25 20:17:52 -0700826fn indirect_return(sig: &Signature, types: &Types) -> bool {
827 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700828 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700829 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700830}
831
David Tolnay99642622020-03-25 13:07:35 -0700832fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
833 match ty {
834 Type::RustBox(ty) | Type::UniquePtr(ty) => {
835 write_type_space(out, &ty.inner);
836 write!(out, "*");
837 }
838 Type::Ref(ty) => {
839 if ty.mutability.is_none() {
840 write!(out, "const ");
841 }
842 write_type(out, &ty.inner);
843 write!(out, " *");
844 }
845 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700846 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700847 _ => write_type(out, ty),
848 }
849}
850
851fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
852 write_indirect_return_type(out, ty);
853 match ty {
854 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700855 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700856 _ => write_space_after_type(out, ty),
857 }
858}
859
860fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400861 match ty {
862 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
863 write_type_space(out, &ty.inner);
864 write!(out, "*");
865 }
David Tolnay4a441222020-01-25 16:24:27 -0800866 Some(Type::Ref(ty)) => {
867 if ty.mutability.is_none() {
868 write!(out, "const ");
869 }
870 write_type(out, &ty.inner);
871 write!(out, " *");
872 }
David Tolnay750755e2020-03-01 13:04:08 -0800873 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700874 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400875 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
876 _ => write_return_type(out, ty),
877 }
878}
879
880fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
881 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700882 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400883 write_type_space(out, &ty.inner);
884 write!(out, "*");
885 }
David Tolnay750755e2020-03-01 13:04:08 -0800886 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700887 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400888 _ => write_type_space(out, &arg.ty),
889 }
890 if types.needs_indirect_abi(&arg.ty) {
891 write!(out, "*");
892 }
893 write!(out, "{}", arg.ident);
894}
895
896fn write_type(out: &mut OutFile, ty: &Type) {
897 match ty {
898 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700899 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400900 None => write!(out, "{}", ident),
901 },
902 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800903 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400904 write_type(out, &ty.inner);
905 write!(out, ">");
906 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700907 Type::RustVec(ty) => {
908 write!(out, "::rust::Vec<");
909 write_type(out, &ty.inner);
910 write!(out, ">");
911 }
David Tolnay7db73692019-10-20 14:51:12 -0400912 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800913 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400914 write_type(out, &ptr.inner);
915 write!(out, ">");
916 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700917 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700918 write!(out, "::std::vector<");
919 write_type(out, &ty.inner);
920 write!(out, ">");
921 }
David Tolnay7db73692019-10-20 14:51:12 -0400922 Type::Ref(r) => {
923 if r.mutability.is_none() {
924 write!(out, "const ");
925 }
926 write_type(out, &r.inner);
927 write!(out, " &");
928 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700929 Type::Slice(_) => {
930 // For now, only U8 slices are supported, which are covered separately below
931 unreachable!()
932 }
David Tolnay7db73692019-10-20 14:51:12 -0400933 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800934 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400935 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700936 Type::SliceRefU8(_) => {
937 write!(out, "::rust::Slice<uint8_t>");
938 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700939 Type::Fn(f) => {
940 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
941 match &f.ret {
942 Some(ret) => write_type(out, ret),
943 None => write!(out, "void"),
944 }
945 write!(out, "(");
946 for (i, arg) in f.args.iter().enumerate() {
947 if i > 0 {
948 write!(out, ", ");
949 }
950 write_type(out, &arg.ty);
951 }
952 write!(out, ")>");
953 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700954 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400955 }
956}
957
David Tolnayf6a89f22020-05-10 23:39:27 -0700958fn write_atom(out: &mut OutFile, atom: Atom) {
959 match atom {
960 Bool => write!(out, "bool"),
961 U8 => write!(out, "uint8_t"),
962 U16 => write!(out, "uint16_t"),
963 U32 => write!(out, "uint32_t"),
964 U64 => write!(out, "uint64_t"),
965 Usize => write!(out, "size_t"),
966 I8 => write!(out, "int8_t"),
967 I16 => write!(out, "int16_t"),
968 I32 => write!(out, "int32_t"),
969 I64 => write!(out, "int64_t"),
970 Isize => write!(out, "::rust::isize"),
971 F32 => write!(out, "float"),
972 F64 => write!(out, "double"),
973 CxxString => write!(out, "::std::string"),
974 RustString => write!(out, "::rust::String"),
975 }
976}
977
David Tolnay7db73692019-10-20 14:51:12 -0400978fn write_type_space(out: &mut OutFile, ty: &Type) {
979 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700980 write_space_after_type(out, ty);
981}
982
983fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400984 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700985 Type::Ident(_)
986 | Type::RustBox(_)
987 | Type::UniquePtr(_)
988 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700989 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700990 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700991 | Type::SliceRefU8(_)
992 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400993 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700994 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400995 }
996}
997
David Tolnaycd08c442020-04-25 10:16:33 -0700998// Only called for legal referent types of unique_ptr and element types of
999// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -07001000fn to_typename(namespace: &Namespace, ty: &Type) -> String {
1001 match ty {
1002 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -07001003 let mut path = String::new();
1004 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001005 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -07001006 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001007 }
David Tolnaycd08c442020-04-25 10:16:33 -07001008 path += &ident.to_string();
1009 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001010 }
1011 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001012 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001013 }
1014}
1015
David Tolnayacdf20a2020-04-25 12:40:53 -07001016// Only called for legal referent types of unique_ptr and element types of
1017// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001018fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1019 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001020 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001021 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001022 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001023 }
1024}
1025
David Tolnay7db73692019-10-20 14:51:12 -04001026fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001027 out.begin_block("extern \"C\"");
1028 for ty in types {
1029 if let Type::RustBox(ty) = ty {
1030 if let Type::Ident(inner) = &ty.inner {
1031 out.next_section();
1032 write_rust_box_extern(out, inner);
1033 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001034 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001035 if let Type::Ident(inner) = &ty.inner {
1036 if Atom::from(inner).is_none() {
1037 out.next_section();
1038 write_rust_vec_extern(out, inner);
1039 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001040 }
David Tolnay7db73692019-10-20 14:51:12 -04001041 } else if let Type::UniquePtr(ptr) = ty {
1042 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) {
David Tolnay7db73692019-10-20 14:51:12 -04001044 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001045 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001046 }
1047 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001048 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001049 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001050 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001051 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001052 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001053 }
1054 }
1055 }
1056 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001057 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001058
David Tolnay750755e2020-03-01 13:04:08 -08001059 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001060 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001061 for ty in types {
1062 if let Type::RustBox(ty) = ty {
1063 if let Type::Ident(inner) = &ty.inner {
1064 write_rust_box_impl(out, inner);
1065 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001066 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001067 if let Type::Ident(inner) = &ty.inner {
1068 if Atom::from(inner).is_none() {
1069 write_rust_vec_impl(out, inner);
1070 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001071 }
David Tolnay7db73692019-10-20 14:51:12 -04001072 }
1073 }
David Tolnay69601622020-04-29 18:48:36 -07001074 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001075 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001076}
1077
1078fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1079 let mut inner = String::new();
1080 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001081 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001082 inner += "::";
1083 }
1084 inner += &ident.to_string();
1085 let instance = inner.replace("::", "$");
1086
David Tolnay69601622020-04-29 18:48:36 -07001087 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1088 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001089 writeln!(
1090 out,
David Tolnay69601622020-04-29 18:48:36 -07001091 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001092 instance, inner,
1093 );
1094 writeln!(
1095 out,
David Tolnay69601622020-04-29 18:48:36 -07001096 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001097 instance, inner,
1098 );
David Tolnay69601622020-04-29 18:48:36 -07001099 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001100}
1101
David Tolnay6787be62020-04-25 11:01:02 -07001102fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1103 let element = Type::Ident(element.clone());
1104 let inner = to_typename(&out.namespace, &element);
1105 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001106
David Tolnay69601622020-04-29 18:48:36 -07001107 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1108 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001109 writeln!(
1110 out,
David Tolnay69601622020-04-29 18:48:36 -07001111 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001112 instance, inner,
1113 );
1114 writeln!(
1115 out,
David Tolnay69601622020-04-29 18:48:36 -07001116 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001117 instance, inner,
1118 );
1119 writeln!(
1120 out,
David Tolnay69601622020-04-29 18:48:36 -07001121 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001122 instance, inner,
1123 );
David Tolnay219c0792020-04-24 20:31:37 -07001124 writeln!(
1125 out,
David Tolnay69601622020-04-29 18:48:36 -07001126 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001127 inner, instance,
1128 );
David Tolnay503d0192020-04-24 22:18:56 -07001129 writeln!(
1130 out,
David Tolnay69601622020-04-29 18:48:36 -07001131 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001132 instance,
1133 );
David Tolnay69601622020-04-29 18:48:36 -07001134 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001135}
1136
David Tolnay7db73692019-10-20 14:51:12 -04001137fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1138 let mut inner = String::new();
1139 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001140 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001141 inner += "::";
1142 }
1143 inner += &ident.to_string();
1144 let instance = inner.replace("::", "$");
1145
1146 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001147 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001148 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001149 writeln!(out, "}}");
1150
1151 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001152 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001153 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001154 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001155}
1156
David Tolnay6787be62020-04-25 11:01:02 -07001157fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1158 let element = Type::Ident(element.clone());
1159 let inner = to_typename(&out.namespace, &element);
1160 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001161
Myron Ahneba35cf2020-02-05 19:41:51 +07001162 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001163 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001164 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001165 writeln!(out, "}}");
1166
1167 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001168 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1169 writeln!(
1170 out,
David Tolnay69601622020-04-29 18:48:36 -07001171 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001172 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001173 );
1174 writeln!(out, "}}");
1175
1176 writeln!(out, "template <>");
1177 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001178 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001179 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001180
1181 writeln!(out, "template <>");
1182 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1183 writeln!(
1184 out,
David Tolnay69601622020-04-29 18:48:36 -07001185 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001186 instance,
1187 );
1188 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001189
1190 writeln!(out, "template <>");
1191 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001192 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001193 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001194}
1195
David Tolnay63da4d32020-04-25 09:41:12 -07001196fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1197 let ty = Type::Ident(ident.clone());
1198 let instance = to_mangled(&out.namespace, &ty);
1199
David Tolnay69601622020-04-29 18:48:36 -07001200 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1201 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001202
1203 write_unique_ptr_common(out, &ty, types);
1204
David Tolnay69601622020-04-29 18:48:36 -07001205 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001206}
1207
1208// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1209fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001210 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001211 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001212 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001213 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001214
David Tolnay63da4d32020-04-25 09:41:12 -07001215 let can_construct_from_value = match ty {
1216 Type::Ident(ident) => types.structs.contains_key(ident),
1217 _ => false,
1218 };
1219
David Tolnay7db73692019-10-20 14:51:12 -04001220 writeln!(
1221 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001222 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001223 inner,
1224 );
1225 writeln!(
1226 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001227 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001228 inner,
1229 );
1230 writeln!(
1231 out,
David Tolnay69601622020-04-29 18:48:36 -07001232 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001233 instance, inner,
1234 );
David Tolnay7e219b82020-03-01 13:14:51 -08001235 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001236 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001237 if can_construct_from_value {
1238 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001239 out,
David Tolnay69601622020-04-29 18:48:36 -07001240 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001241 instance, inner, inner,
1242 );
David Tolnay63da4d32020-04-25 09:41:12 -07001243 writeln!(
1244 out,
1245 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1246 inner, inner,
1247 );
1248 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001249 }
David Tolnay7db73692019-10-20 14:51:12 -04001250 writeln!(
1251 out,
David Tolnay69601622020-04-29 18:48:36 -07001252 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001253 instance, inner, inner,
1254 );
David Tolnay7e219b82020-03-01 13:14:51 -08001255 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001256 writeln!(out, "}}");
1257 writeln!(
1258 out,
David Tolnay69601622020-04-29 18:48:36 -07001259 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001260 inner, instance, inner,
1261 );
1262 writeln!(out, " return ptr.get();");
1263 writeln!(out, "}}");
1264 writeln!(
1265 out,
David Tolnay69601622020-04-29 18:48:36 -07001266 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001267 inner, instance, inner,
1268 );
1269 writeln!(out, " return ptr.release();");
1270 writeln!(out, "}}");
1271 writeln!(
1272 out,
David Tolnay69601622020-04-29 18:48:36 -07001273 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001274 instance, inner,
1275 );
1276 writeln!(out, " ptr->~unique_ptr();");
1277 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001278}
Myron Ahneba35cf2020-02-05 19:41:51 +07001279
David Tolnay92105da2020-04-25 11:15:31 -07001280fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001281 let element = Type::Ident(element.clone());
1282 let inner = to_typename(&out.namespace, &element);
1283 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001284
David Tolnay69601622020-04-29 18:48:36 -07001285 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1286 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001287 writeln!(
1288 out,
David Tolnay69601622020-04-29 18:48:36 -07001289 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001290 instance, inner,
1291 );
1292 writeln!(out, " return s.size();");
1293 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001294 writeln!(
1295 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001296 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001297 inner, instance, inner,
1298 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001299 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001300 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001301
1302 write_unique_ptr_common(out, vector_ty, types);
1303
David Tolnay69601622020-04-29 18:48:36 -07001304 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001305}