blob: a9b8ddee1d73894475ab3ffbee7d7baf4a193963 [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 Tolnaya25ea9c2020-08-27 22:59:38 -0700317 let guard = format!("CXXBRIDGE03_STRUCT_{}{}", out.namespace, strct.ident);
318 writeln!(out, "#ifndef {}", guard);
319 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400320 for line in strct.doc.to_string().lines() {
321 writeln!(out, "//{}", line);
322 }
323 writeln!(out, "struct {} final {{", strct.ident);
324 for field in &strct.fields {
325 write!(out, " ");
326 write_type_space(out, &field.ty);
327 writeln!(out, "{};", field.ident);
328 }
329 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700330 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400331}
332
333fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
334 writeln!(out, "struct {};", ident);
335}
336
David Tolnay8861bee2020-01-20 18:39:24 -0800337fn write_struct_using(out: &mut OutFile, ident: &Ident) {
338 writeln!(out, "using {} = {};", ident, ident);
339}
340
David Tolnayc1fe0052020-04-17 15:15:06 -0700341fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnaya25ea9c2020-08-27 22:59:38 -0700342 let guard = format!("CXXBRIDGE03_STRUCT_{}{}", out.namespace, ety.ident);
343 writeln!(out, "#ifndef {}", guard);
344 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700345 for line in ety.doc.to_string().lines() {
346 writeln!(out, "//{}", line);
347 }
348 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700349 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700350 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700351 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700352 write!(out, " ");
353 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700354 let local_name = method.ident.to_string();
355 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700356 writeln!(out, ";");
357 }
358 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700359 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700360}
361
Joel Galensonc03402a2020-04-23 17:31:09 -0700362fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnaya25ea9c2020-08-27 22:59:38 -0700363 let guard = format!("CXXBRIDGE03_ENUM_{}{}", out.namespace, enm.ident);
364 writeln!(out, "#ifndef {}", guard);
365 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700366 for line in enm.doc.to_string().lines() {
367 writeln!(out, "//{}", line);
368 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700369 write!(out, "enum class {} : ", enm.ident);
370 write_atom(out, enm.repr);
371 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700372 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700373 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700374 }
375 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700376 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700377}
378
Joel Galenson905eb2e2020-05-04 14:58:14 -0700379fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700380 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
381 write_atom(out, enm.repr);
382 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700383 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700384 write!(out, "static_assert(static_cast<");
385 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700386 writeln!(
387 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700388 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700389 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700390 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700391 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700392}
393
David Tolnayebef4a22020-03-17 15:33:47 -0700394fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
395 let mut has_cxx_throws = false;
396 for api in apis {
397 if let Api::CxxFunction(efn) = api {
398 if efn.throws {
399 has_cxx_throws = true;
400 break;
401 }
402 }
403 }
404
405 if has_cxx_throws {
406 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700407 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700408 out,
David Tolnay69601622020-04-29 18:48:36 -0700409 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700410 );
411 }
412}
413
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700414fn write_cxx_function_shim(
415 out: &mut OutFile,
416 efn: &ExternFn,
417 types: &Types,
418 impl_annotations: &Option<String>,
419) {
420 if !out.header {
421 if let Some(annotation) = impl_annotations {
422 write!(out, "{} ", annotation);
423 }
424 }
David Tolnayebef4a22020-03-17 15:33:47 -0700425 if efn.throws {
426 write!(out, "::rust::Str::Repr ");
427 } else {
David Tolnay99642622020-03-25 13:07:35 -0700428 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700429 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700430 let mangled = mangle::extern_fn(&out.namespace, efn);
431 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700432 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700433 if receiver.mutability.is_none() {
434 write!(out, "const ");
435 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700436 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700437 }
David Tolnay7db73692019-10-20 14:51:12 -0400438 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700439 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400440 write!(out, ", ");
441 }
David Tolnaya46a2372020-03-06 10:03:48 -0800442 if arg.ty == RustString {
443 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700444 } else if let Type::RustVec(_) = arg.ty {
445 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800446 }
David Tolnay7db73692019-10-20 14:51:12 -0400447 write_extern_arg(out, arg, types);
448 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700449 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400450 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700451 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400452 write!(out, ", ");
453 }
David Tolnay99642622020-03-25 13:07:35 -0700454 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400455 write!(out, "*return$");
456 }
457 writeln!(out, ") noexcept {{");
458 write!(out, " ");
459 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700460 match &efn.receiver {
461 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700462 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700463 }
David Tolnay7db73692019-10-20 14:51:12 -0400464 for (i, arg) in efn.args.iter().enumerate() {
465 if i > 0 {
466 write!(out, ", ");
467 }
468 write_type(out, &arg.ty);
469 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700470 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700471 if let Some(receiver) = &efn.receiver {
472 if receiver.mutability.is_none() {
473 write!(out, " const");
474 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700475 }
476 write!(out, " = ");
477 match &efn.receiver {
478 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700479 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700480 }
481 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400482 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700483 if efn.throws {
484 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700485 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700486 writeln!(out, " [&] {{");
487 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700488 }
David Tolnay7db73692019-10-20 14:51:12 -0400489 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700490 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400491 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700492 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400493 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700494 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400495 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700496 }
497 match &efn.ret {
498 Some(Type::Ref(_)) => write!(out, "&"),
499 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700500 Some(Type::SliceRefU8(_)) if !indirect_return => {
501 write!(out, "::rust::Slice<uint8_t>::Repr(")
502 }
David Tolnay99642622020-03-25 13:07:35 -0700503 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400504 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700505 match &efn.receiver {
506 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700507 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700508 }
David Tolnay7db73692019-10-20 14:51:12 -0400509 for (i, arg) in efn.args.iter().enumerate() {
510 if i > 0 {
511 write!(out, ", ");
512 }
513 if let Type::RustBox(_) = &arg.ty {
514 write_type(out, &arg.ty);
515 write!(out, "::from_raw({})", arg.ident);
516 } else if let Type::UniquePtr(_) = &arg.ty {
517 write_type(out, &arg.ty);
518 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800519 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800520 write!(
521 out,
522 "::rust::String(::rust::unsafe_bitcopy, *{})",
523 arg.ident,
524 );
David Tolnay313b10e2020-04-25 16:30:51 -0700525 } else if let Type::RustVec(_) = arg.ty {
526 write_type(out, &arg.ty);
527 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400528 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700529 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800530 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400531 } else {
532 write!(out, "{}", arg.ident);
533 }
534 }
535 write!(out, ")");
536 match &efn.ret {
537 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
538 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700539 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400540 _ => {}
541 }
542 if indirect_return {
543 write!(out, ")");
544 }
545 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700546 if efn.throws {
547 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700548 writeln!(out, " throw$.ptr = nullptr;");
549 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700550 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700551 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700552 writeln!(
553 out,
David Tolnay69601622020-04-29 18:48:36 -0700554 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700555 );
David Tolnay5d121442020-03-17 22:14:40 -0700556 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700557 writeln!(out, " return throw$;");
558 }
David Tolnay7db73692019-10-20 14:51:12 -0400559 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700560 for arg in &efn.args {
561 if let Type::Fn(f) = &arg.ty {
562 let var = &arg.ident;
563 write_function_pointer_trampoline(out, efn, var, f, types);
564 }
565 }
566}
567
568fn write_function_pointer_trampoline(
569 out: &mut OutFile,
570 efn: &ExternFn,
571 var: &Ident,
572 f: &Signature,
573 types: &Types,
574) {
575 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700576 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700577 let indirect_call = true;
578 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
579
580 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700581 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700582 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400583}
584
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700585fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700586 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700587 let indirect_call = false;
588 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
589}
590
591fn write_rust_function_decl_impl(
592 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700593 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 sig: &Signature,
595 types: &Types,
596 indirect_call: bool,
597) {
598 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700599 write!(out, "::rust::Str::Repr ");
600 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700601 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700602 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 write!(out, "{}(", link_name);
604 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700605 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700606 if receiver.mutability.is_none() {
607 write!(out, "const ");
608 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700609 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700610 needs_comma = true;
611 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 for arg in &sig.args {
613 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400614 write!(out, ", ");
615 }
616 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700617 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400618 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 if indirect_return(sig, types) {
620 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400621 write!(out, ", ");
622 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700623 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400624 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700625 needs_comma = true;
626 }
627 if indirect_call {
628 if needs_comma {
629 write!(out, ", ");
630 }
631 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400632 }
633 writeln!(out, ") noexcept;");
634}
635
636fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400637 for line in efn.doc.to_string().lines() {
638 writeln!(out, "//{}", line);
639 }
David Tolnaya73853b2020-04-20 01:19:56 -0700640 let local_name = match &efn.sig.receiver {
641 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700642 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700643 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700644 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700645 let indirect_call = false;
646 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
647}
648
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700649fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700650 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700651 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 indirect_call: bool,
654) {
655 write_return_type(out, &sig.ret);
656 write!(out, "{}(", local_name);
657 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400658 if i > 0 {
659 write!(out, ", ");
660 }
661 write_type_space(out, &arg.ty);
662 write!(out, "{}", arg.ident);
663 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700664 if indirect_call {
665 if !sig.args.is_empty() {
666 write!(out, ", ");
667 }
668 write!(out, "void *extern$");
669 }
David Tolnay1e548172020-03-16 13:37:09 -0700670 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700671 if let Some(receiver) = &sig.receiver {
672 if receiver.mutability.is_none() {
673 write!(out, " const");
674 }
675 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700676 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700677 write!(out, " noexcept");
678 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700679}
680
681fn write_rust_function_shim_impl(
682 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700683 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700684 sig: &Signature,
685 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700686 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700687 indirect_call: bool,
688) {
689 if out.header && sig.receiver.is_some() {
690 // We've already defined this inside the struct.
691 return;
692 }
David Tolnaya73853b2020-04-20 01:19:56 -0700693 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400694 if out.header {
695 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700696 return;
David Tolnay7db73692019-10-20 14:51:12 -0400697 }
David Tolnay439cde22020-04-20 00:46:25 -0700698 writeln!(out, " {{");
699 for arg in &sig.args {
700 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
701 out.include.utility = true;
702 write!(out, " ::rust::ManuallyDrop<");
703 write_type(out, &arg.ty);
704 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
705 }
706 }
707 write!(out, " ");
708 let indirect_return = indirect_return(sig, types);
709 if indirect_return {
710 write!(out, "::rust::MaybeUninit<");
711 write_type(out, sig.ret.as_ref().unwrap());
712 writeln!(out, "> return$;");
713 write!(out, " ");
714 } else if let Some(ret) = &sig.ret {
715 write!(out, "return ");
716 match ret {
717 Type::RustBox(_) => {
718 write_type(out, ret);
719 write!(out, "::from_raw(");
720 }
721 Type::UniquePtr(_) => {
722 write_type(out, ret);
723 write!(out, "(");
724 }
725 Type::Ref(_) => write!(out, "*"),
726 _ => {}
727 }
728 }
729 if sig.throws {
730 write!(out, "::rust::Str::Repr error$ = ");
731 }
732 write!(out, "{}(", invoke);
733 if sig.receiver.is_some() {
734 write!(out, "*this");
735 }
736 for (i, arg) in sig.args.iter().enumerate() {
737 if i > 0 || sig.receiver.is_some() {
738 write!(out, ", ");
739 }
740 match &arg.ty {
741 Type::Str(_) => write!(out, "::rust::Str::Repr("),
742 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
743 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
744 _ => {}
745 }
746 write!(out, "{}", arg.ident);
747 match &arg.ty {
748 Type::RustBox(_) => write!(out, ".into_raw()"),
749 Type::UniquePtr(_) => write!(out, ".release()"),
750 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
751 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
752 _ => {}
753 }
754 }
755 if indirect_return {
756 if !sig.args.is_empty() {
757 write!(out, ", ");
758 }
759 write!(out, "&return$.value");
760 }
761 if indirect_call {
762 if !sig.args.is_empty() || indirect_return {
763 write!(out, ", ");
764 }
765 write!(out, "extern$");
766 }
767 write!(out, ")");
768 if let Some(ret) = &sig.ret {
769 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
770 write!(out, ")");
771 }
772 }
773 writeln!(out, ";");
774 if sig.throws {
775 writeln!(out, " if (error$.ptr) {{");
776 writeln!(out, " throw ::rust::Error(error$);");
777 writeln!(out, " }}");
778 }
779 if indirect_return {
780 out.include.utility = true;
781 writeln!(out, " return ::std::move(return$.value);");
782 }
783 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400784}
785
786fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
787 match ty {
788 None => write!(out, "void "),
789 Some(ty) => write_type_space(out, ty),
790 }
791}
792
David Tolnay75dca2e2020-03-25 20:17:52 -0700793fn indirect_return(sig: &Signature, types: &Types) -> bool {
794 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700795 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700796 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700797}
798
David Tolnay99642622020-03-25 13:07:35 -0700799fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
800 match ty {
801 Type::RustBox(ty) | Type::UniquePtr(ty) => {
802 write_type_space(out, &ty.inner);
803 write!(out, "*");
804 }
805 Type::Ref(ty) => {
806 if ty.mutability.is_none() {
807 write!(out, "const ");
808 }
809 write_type(out, &ty.inner);
810 write!(out, " *");
811 }
812 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700813 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700814 _ => write_type(out, ty),
815 }
816}
817
818fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
819 write_indirect_return_type(out, ty);
820 match ty {
821 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700822 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700823 _ => write_space_after_type(out, ty),
824 }
825}
826
827fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400828 match ty {
829 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
830 write_type_space(out, &ty.inner);
831 write!(out, "*");
832 }
David Tolnay4a441222020-01-25 16:24:27 -0800833 Some(Type::Ref(ty)) => {
834 if ty.mutability.is_none() {
835 write!(out, "const ");
836 }
837 write_type(out, &ty.inner);
838 write!(out, " *");
839 }
David Tolnay750755e2020-03-01 13:04:08 -0800840 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700841 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400842 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
843 _ => write_return_type(out, ty),
844 }
845}
846
847fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
848 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700849 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400850 write_type_space(out, &ty.inner);
851 write!(out, "*");
852 }
David Tolnay750755e2020-03-01 13:04:08 -0800853 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700854 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400855 _ => write_type_space(out, &arg.ty),
856 }
857 if types.needs_indirect_abi(&arg.ty) {
858 write!(out, "*");
859 }
860 write!(out, "{}", arg.ident);
861}
862
863fn write_type(out: &mut OutFile, ty: &Type) {
864 match ty {
865 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700866 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400867 None => write!(out, "{}", ident),
868 },
869 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800870 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400871 write_type(out, &ty.inner);
872 write!(out, ">");
873 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700874 Type::RustVec(ty) => {
875 write!(out, "::rust::Vec<");
876 write_type(out, &ty.inner);
877 write!(out, ">");
878 }
David Tolnay7db73692019-10-20 14:51:12 -0400879 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800880 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400881 write_type(out, &ptr.inner);
882 write!(out, ">");
883 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700884 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700885 write!(out, "::std::vector<");
886 write_type(out, &ty.inner);
887 write!(out, ">");
888 }
David Tolnay7db73692019-10-20 14:51:12 -0400889 Type::Ref(r) => {
890 if r.mutability.is_none() {
891 write!(out, "const ");
892 }
893 write_type(out, &r.inner);
894 write!(out, " &");
895 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700896 Type::Slice(_) => {
897 // For now, only U8 slices are supported, which are covered separately below
898 unreachable!()
899 }
David Tolnay7db73692019-10-20 14:51:12 -0400900 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800901 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400902 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700903 Type::SliceRefU8(_) => {
904 write!(out, "::rust::Slice<uint8_t>");
905 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700906 Type::Fn(f) => {
907 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
908 match &f.ret {
909 Some(ret) => write_type(out, ret),
910 None => write!(out, "void"),
911 }
912 write!(out, "(");
913 for (i, arg) in f.args.iter().enumerate() {
914 if i > 0 {
915 write!(out, ", ");
916 }
917 write_type(out, &arg.ty);
918 }
919 write!(out, ")>");
920 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700921 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400922 }
923}
924
David Tolnayf6a89f22020-05-10 23:39:27 -0700925fn write_atom(out: &mut OutFile, atom: Atom) {
926 match atom {
927 Bool => write!(out, "bool"),
928 U8 => write!(out, "uint8_t"),
929 U16 => write!(out, "uint16_t"),
930 U32 => write!(out, "uint32_t"),
931 U64 => write!(out, "uint64_t"),
932 Usize => write!(out, "size_t"),
933 I8 => write!(out, "int8_t"),
934 I16 => write!(out, "int16_t"),
935 I32 => write!(out, "int32_t"),
936 I64 => write!(out, "int64_t"),
937 Isize => write!(out, "::rust::isize"),
938 F32 => write!(out, "float"),
939 F64 => write!(out, "double"),
940 CxxString => write!(out, "::std::string"),
941 RustString => write!(out, "::rust::String"),
942 }
943}
944
David Tolnay7db73692019-10-20 14:51:12 -0400945fn write_type_space(out: &mut OutFile, ty: &Type) {
946 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700947 write_space_after_type(out, ty);
948}
949
950fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400951 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700952 Type::Ident(_)
953 | Type::RustBox(_)
954 | Type::UniquePtr(_)
955 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700956 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700957 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700958 | Type::SliceRefU8(_)
959 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400960 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700961 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400962 }
963}
964
David Tolnaycd08c442020-04-25 10:16:33 -0700965// Only called for legal referent types of unique_ptr and element types of
966// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700967fn to_typename(namespace: &Namespace, ty: &Type) -> String {
968 match ty {
969 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700970 let mut path = String::new();
971 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700972 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700973 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700974 }
David Tolnaycd08c442020-04-25 10:16:33 -0700975 path += &ident.to_string();
976 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700977 }
978 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700979 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700980 }
981}
982
David Tolnayacdf20a2020-04-25 12:40:53 -0700983// Only called for legal referent types of unique_ptr and element types of
984// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700985fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
986 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700987 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700988 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700989 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700990 }
991}
992
David Tolnay7db73692019-10-20 14:51:12 -0400993fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400994 out.begin_block("extern \"C\"");
995 for ty in types {
996 if let Type::RustBox(ty) = ty {
997 if let Type::Ident(inner) = &ty.inner {
998 out.next_section();
999 write_rust_box_extern(out, inner);
1000 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001001 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001002 if let Type::Ident(inner) = &ty.inner {
1003 if Atom::from(inner).is_none() {
1004 out.next_section();
1005 write_rust_vec_extern(out, inner);
1006 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001007 }
David Tolnay7db73692019-10-20 14:51:12 -04001008 } else if let Type::UniquePtr(ptr) = ty {
1009 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001010 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001011 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001012 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001013 }
1014 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001015 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001016 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001017 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001018 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001019 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001020 }
1021 }
1022 }
1023 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001024 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001025
David Tolnay750755e2020-03-01 13:04:08 -08001026 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001027 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001028 for ty in types {
1029 if let Type::RustBox(ty) = ty {
1030 if let Type::Ident(inner) = &ty.inner {
1031 write_rust_box_impl(out, inner);
1032 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001033 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001034 if let Type::Ident(inner) = &ty.inner {
1035 if Atom::from(inner).is_none() {
1036 write_rust_vec_impl(out, inner);
1037 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001038 }
David Tolnay7db73692019-10-20 14:51:12 -04001039 }
1040 }
David Tolnay69601622020-04-29 18:48:36 -07001041 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001042 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001043}
1044
1045fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1046 let mut inner = String::new();
1047 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001048 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001049 inner += "::";
1050 }
1051 inner += &ident.to_string();
1052 let instance = inner.replace("::", "$");
1053
David Tolnay69601622020-04-29 18:48:36 -07001054 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1055 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001056 writeln!(
1057 out,
David Tolnay69601622020-04-29 18:48:36 -07001058 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001059 instance, inner,
1060 );
1061 writeln!(
1062 out,
David Tolnay69601622020-04-29 18:48:36 -07001063 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001064 instance, inner,
1065 );
David Tolnay69601622020-04-29 18:48:36 -07001066 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001067}
1068
David Tolnay6787be62020-04-25 11:01:02 -07001069fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1070 let element = Type::Ident(element.clone());
1071 let inner = to_typename(&out.namespace, &element);
1072 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001073
David Tolnay69601622020-04-29 18:48:36 -07001074 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1075 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001076 writeln!(
1077 out,
David Tolnay69601622020-04-29 18:48:36 -07001078 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001079 instance, inner,
1080 );
1081 writeln!(
1082 out,
David Tolnay69601622020-04-29 18:48:36 -07001083 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001084 instance, inner,
1085 );
1086 writeln!(
1087 out,
David Tolnay69601622020-04-29 18:48:36 -07001088 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001089 instance, inner,
1090 );
David Tolnay219c0792020-04-24 20:31:37 -07001091 writeln!(
1092 out,
David Tolnay69601622020-04-29 18:48:36 -07001093 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001094 inner, instance,
1095 );
David Tolnay503d0192020-04-24 22:18:56 -07001096 writeln!(
1097 out,
David Tolnay69601622020-04-29 18:48:36 -07001098 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001099 instance,
1100 );
David Tolnay69601622020-04-29 18:48:36 -07001101 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001102}
1103
David Tolnay7db73692019-10-20 14:51:12 -04001104fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1105 let mut inner = String::new();
1106 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001107 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001108 inner += "::";
1109 }
1110 inner += &ident.to_string();
1111 let instance = inner.replace("::", "$");
1112
1113 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001114 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001115 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001116 writeln!(out, "}}");
1117
1118 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001119 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001120 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001121 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001122}
1123
David Tolnay6787be62020-04-25 11:01:02 -07001124fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1125 let element = Type::Ident(element.clone());
1126 let inner = to_typename(&out.namespace, &element);
1127 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001128
Myron Ahneba35cf2020-02-05 19:41:51 +07001129 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001130 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001131 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001132 writeln!(out, "}}");
1133
1134 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001135 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1136 writeln!(
1137 out,
David Tolnay69601622020-04-29 18:48:36 -07001138 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001139 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001140 );
1141 writeln!(out, "}}");
1142
1143 writeln!(out, "template <>");
1144 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001145 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001146 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001147
1148 writeln!(out, "template <>");
1149 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1150 writeln!(
1151 out,
David Tolnay69601622020-04-29 18:48:36 -07001152 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001153 instance,
1154 );
1155 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001156
1157 writeln!(out, "template <>");
1158 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001159 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001160 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001161}
1162
David Tolnay63da4d32020-04-25 09:41:12 -07001163fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1164 let ty = Type::Ident(ident.clone());
1165 let instance = to_mangled(&out.namespace, &ty);
1166
David Tolnay69601622020-04-29 18:48:36 -07001167 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1168 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001169
1170 write_unique_ptr_common(out, &ty, types);
1171
David Tolnay69601622020-04-29 18:48:36 -07001172 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001173}
1174
1175// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1176fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001177 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001178 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001179 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001180 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001181
David Tolnay63da4d32020-04-25 09:41:12 -07001182 let can_construct_from_value = match ty {
1183 Type::Ident(ident) => types.structs.contains_key(ident),
1184 _ => false,
1185 };
1186
David Tolnay7db73692019-10-20 14:51:12 -04001187 writeln!(
1188 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001189 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001190 inner,
1191 );
1192 writeln!(
1193 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001194 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001195 inner,
1196 );
1197 writeln!(
1198 out,
David Tolnay69601622020-04-29 18:48:36 -07001199 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001200 instance, inner,
1201 );
David Tolnay7e219b82020-03-01 13:14:51 -08001202 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001203 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001204 if can_construct_from_value {
1205 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001206 out,
David Tolnay69601622020-04-29 18:48:36 -07001207 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001208 instance, inner, inner,
1209 );
David Tolnay63da4d32020-04-25 09:41:12 -07001210 writeln!(
1211 out,
1212 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1213 inner, inner,
1214 );
1215 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001216 }
David Tolnay7db73692019-10-20 14:51:12 -04001217 writeln!(
1218 out,
David Tolnay69601622020-04-29 18:48:36 -07001219 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001220 instance, inner, inner,
1221 );
David Tolnay7e219b82020-03-01 13:14:51 -08001222 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001223 writeln!(out, "}}");
1224 writeln!(
1225 out,
David Tolnay69601622020-04-29 18:48:36 -07001226 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001227 inner, instance, inner,
1228 );
1229 writeln!(out, " return ptr.get();");
1230 writeln!(out, "}}");
1231 writeln!(
1232 out,
David Tolnay69601622020-04-29 18:48:36 -07001233 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001234 inner, instance, inner,
1235 );
1236 writeln!(out, " return ptr.release();");
1237 writeln!(out, "}}");
1238 writeln!(
1239 out,
David Tolnay69601622020-04-29 18:48:36 -07001240 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001241 instance, inner,
1242 );
1243 writeln!(out, " ptr->~unique_ptr();");
1244 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001245}
Myron Ahneba35cf2020-02-05 19:41:51 +07001246
David Tolnay92105da2020-04-25 11:15:31 -07001247fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001248 let element = Type::Ident(element.clone());
1249 let inner = to_typename(&out.namespace, &element);
1250 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001251
David Tolnay69601622020-04-29 18:48:36 -07001252 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1253 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001254 writeln!(
1255 out,
David Tolnay69601622020-04-29 18:48:36 -07001256 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001257 instance, inner,
1258 );
1259 writeln!(out, " return s.size();");
1260 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001261 writeln!(
1262 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001263 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001264 inner, instance, inner,
1265 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001266 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001267 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001268
1269 write_unique_ptr_common(out, vector_ty, types);
1270
David Tolnay69601622020-04-29 18:48:36 -07001271 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001272}