blob: 51c4b02957f977fcc28441f9d2ea4e1ac3dcb009 [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 Tolnaydf4ca022020-07-31 18:33:44 -0700317 write_include_guard_start(out, &strct.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400318 for line in strct.doc.to_string().lines() {
319 writeln!(out, "//{}", line);
320 }
321 writeln!(out, "struct {} final {{", strct.ident);
322 for field in &strct.fields {
323 write!(out, " ");
324 write_type_space(out, &field.ty);
325 writeln!(out, "{};", field.ident);
326 }
327 writeln!(out, "}};");
David Tolnaydf4ca022020-07-31 18:33:44 -0700328 write_include_guard_end(out, &strct.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400329}
330
331fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
332 writeln!(out, "struct {};", ident);
333}
334
David Tolnay8861bee2020-01-20 18:39:24 -0800335fn write_struct_using(out: &mut OutFile, ident: &Ident) {
336 writeln!(out, "using {} = {};", ident, ident);
337}
338
Adrian Taylora1f890b2020-07-31 15:35:34 -0700339const INCLUDE_GUARD_PREFIX: &'static str = "CXXBRIDGE03_TYPE_";
340
David Tolnaydf4ca022020-07-31 18:33:44 -0700341fn write_include_guard_start(out: &mut OutFile, ident: &Ident) {
Adrian Taylora1f890b2020-07-31 15:35:34 -0700342 writeln!(
343 out,
344 "#ifndef {}{}{}",
David Tolnaydf4ca022020-07-31 18:33:44 -0700345 INCLUDE_GUARD_PREFIX, out.namespace, ident
Adrian Taylora1f890b2020-07-31 15:35:34 -0700346 );
347 writeln!(
348 out,
349 "#define {}{}{}",
David Tolnaydf4ca022020-07-31 18:33:44 -0700350 INCLUDE_GUARD_PREFIX, out.namespace, ident
Adrian Taylora1f890b2020-07-31 15:35:34 -0700351 );
352}
353
David Tolnaydf4ca022020-07-31 18:33:44 -0700354fn write_include_guard_end(out: &mut OutFile, ident: &Ident) {
Adrian Taylora1f890b2020-07-31 15:35:34 -0700355 writeln!(
356 out,
357 "#endif // {}{}{}",
David Tolnaydf4ca022020-07-31 18:33:44 -0700358 INCLUDE_GUARD_PREFIX, out.namespace, ident
Adrian Taylora1f890b2020-07-31 15:35:34 -0700359 );
360}
361
David Tolnayc1fe0052020-04-17 15:15:06 -0700362fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnaydf4ca022020-07-31 18:33:44 -0700363 write_include_guard_start(out, &ety.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700364 for line in ety.doc.to_string().lines() {
365 writeln!(out, "//{}", line);
366 }
367 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700368 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700369 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700370 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700371 write!(out, " ");
372 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700373 let local_name = method.ident.to_string();
374 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700375 writeln!(out, ";");
376 }
377 writeln!(out, "}};");
David Tolnaydf4ca022020-07-31 18:33:44 -0700378 write_include_guard_end(out, &ety.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700379}
380
Joel Galensonc03402a2020-04-23 17:31:09 -0700381fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnaydf4ca022020-07-31 18:33:44 -0700382 write_include_guard_start(out, &enm.ident);
Joel Galensonc03402a2020-04-23 17:31:09 -0700383 for line in enm.doc.to_string().lines() {
384 writeln!(out, "//{}", line);
385 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700386 write!(out, "enum class {} : ", enm.ident);
387 write_atom(out, enm.repr);
388 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700389 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700390 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700391 }
392 writeln!(out, "}};");
David Tolnaydf4ca022020-07-31 18:33:44 -0700393 write_include_guard_end(out, &enm.ident);
Joel Galensonc03402a2020-04-23 17:31:09 -0700394}
395
Joel Galenson905eb2e2020-05-04 14:58:14 -0700396fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700397 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
398 write_atom(out, enm.repr);
399 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700400 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700401 write!(out, "static_assert(static_cast<");
402 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700403 writeln!(
404 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700405 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700406 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700407 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700408 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700409}
410
David Tolnayebef4a22020-03-17 15:33:47 -0700411fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
412 let mut has_cxx_throws = false;
413 for api in apis {
414 if let Api::CxxFunction(efn) = api {
415 if efn.throws {
416 has_cxx_throws = true;
417 break;
418 }
419 }
420 }
421
422 if has_cxx_throws {
423 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700424 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700425 out,
David Tolnay69601622020-04-29 18:48:36 -0700426 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700427 );
428 }
429}
430
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700431fn write_cxx_function_shim(
432 out: &mut OutFile,
433 efn: &ExternFn,
434 types: &Types,
435 impl_annotations: &Option<String>,
436) {
437 if !out.header {
438 if let Some(annotation) = impl_annotations {
439 write!(out, "{} ", annotation);
440 }
441 }
David Tolnayebef4a22020-03-17 15:33:47 -0700442 if efn.throws {
443 write!(out, "::rust::Str::Repr ");
444 } else {
David Tolnay99642622020-03-25 13:07:35 -0700445 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700446 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700447 let mangled = mangle::extern_fn(&out.namespace, efn);
448 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700449 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700450 if receiver.mutability.is_none() {
451 write!(out, "const ");
452 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700453 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700454 }
David Tolnay7db73692019-10-20 14:51:12 -0400455 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700456 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400457 write!(out, ", ");
458 }
David Tolnaya46a2372020-03-06 10:03:48 -0800459 if arg.ty == RustString {
460 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700461 } else if let Type::RustVec(_) = arg.ty {
462 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800463 }
David Tolnay7db73692019-10-20 14:51:12 -0400464 write_extern_arg(out, arg, types);
465 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700466 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400467 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700468 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400469 write!(out, ", ");
470 }
David Tolnay99642622020-03-25 13:07:35 -0700471 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400472 write!(out, "*return$");
473 }
474 writeln!(out, ") noexcept {{");
475 write!(out, " ");
476 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700477 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 }
David Tolnay7db73692019-10-20 14:51:12 -0400481 for (i, arg) in efn.args.iter().enumerate() {
482 if i > 0 {
483 write!(out, ", ");
484 }
485 write_type(out, &arg.ty);
486 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700487 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700488 if let Some(receiver) = &efn.receiver {
489 if receiver.mutability.is_none() {
490 write!(out, " const");
491 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700492 }
493 write!(out, " = ");
494 match &efn.receiver {
495 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700496 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700497 }
498 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400499 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700500 if efn.throws {
501 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700502 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700503 writeln!(out, " [&] {{");
504 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700505 }
David Tolnay7db73692019-10-20 14:51:12 -0400506 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700507 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400508 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700509 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400510 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700511 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400512 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700513 }
514 match &efn.ret {
515 Some(Type::Ref(_)) => write!(out, "&"),
516 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700517 Some(Type::SliceRefU8(_)) if !indirect_return => {
518 write!(out, "::rust::Slice<uint8_t>::Repr(")
519 }
David Tolnay99642622020-03-25 13:07:35 -0700520 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400521 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700522 match &efn.receiver {
523 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700524 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700525 }
David Tolnay7db73692019-10-20 14:51:12 -0400526 for (i, arg) in efn.args.iter().enumerate() {
527 if i > 0 {
528 write!(out, ", ");
529 }
530 if let Type::RustBox(_) = &arg.ty {
531 write_type(out, &arg.ty);
532 write!(out, "::from_raw({})", arg.ident);
533 } else if let Type::UniquePtr(_) = &arg.ty {
534 write_type(out, &arg.ty);
535 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800536 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800537 write!(
538 out,
539 "::rust::String(::rust::unsafe_bitcopy, *{})",
540 arg.ident,
541 );
David Tolnay313b10e2020-04-25 16:30:51 -0700542 } else if let Type::RustVec(_) = arg.ty {
543 write_type(out, &arg.ty);
544 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400545 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700546 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800547 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400548 } else {
549 write!(out, "{}", arg.ident);
550 }
551 }
552 write!(out, ")");
553 match &efn.ret {
554 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
555 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700556 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400557 _ => {}
558 }
559 if indirect_return {
560 write!(out, ")");
561 }
562 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700563 if efn.throws {
564 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700565 writeln!(out, " throw$.ptr = nullptr;");
566 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700567 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700568 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700569 writeln!(
570 out,
David Tolnay69601622020-04-29 18:48:36 -0700571 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700572 );
David Tolnay5d121442020-03-17 22:14:40 -0700573 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700574 writeln!(out, " return throw$;");
575 }
David Tolnay7db73692019-10-20 14:51:12 -0400576 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700577 for arg in &efn.args {
578 if let Type::Fn(f) = &arg.ty {
579 let var = &arg.ident;
580 write_function_pointer_trampoline(out, efn, var, f, types);
581 }
582 }
583}
584
585fn write_function_pointer_trampoline(
586 out: &mut OutFile,
587 efn: &ExternFn,
588 var: &Ident,
589 f: &Signature,
590 types: &Types,
591) {
592 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700593 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 let indirect_call = true;
595 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
596
597 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700598 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700599 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400600}
601
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700602fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700603 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700604 let indirect_call = false;
605 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
606}
607
608fn write_rust_function_decl_impl(
609 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700610 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700611 sig: &Signature,
612 types: &Types,
613 indirect_call: bool,
614) {
615 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700616 write!(out, "::rust::Str::Repr ");
617 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700618 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700619 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700620 write!(out, "{}(", link_name);
621 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700622 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700623 if receiver.mutability.is_none() {
624 write!(out, "const ");
625 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700626 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700627 needs_comma = true;
628 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700629 for arg in &sig.args {
630 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400631 write!(out, ", ");
632 }
633 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700634 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400635 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700636 if indirect_return(sig, types) {
637 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400638 write!(out, ", ");
639 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700640 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400641 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700642 needs_comma = true;
643 }
644 if indirect_call {
645 if needs_comma {
646 write!(out, ", ");
647 }
648 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400649 }
650 writeln!(out, ") noexcept;");
651}
652
653fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400654 for line in efn.doc.to_string().lines() {
655 writeln!(out, "//{}", line);
656 }
David Tolnaya73853b2020-04-20 01:19:56 -0700657 let local_name = match &efn.sig.receiver {
658 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700659 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700660 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700661 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700662 let indirect_call = false;
663 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
664}
665
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700666fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700667 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700668 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700669 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700670 indirect_call: bool,
671) {
672 write_return_type(out, &sig.ret);
673 write!(out, "{}(", local_name);
674 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400675 if i > 0 {
676 write!(out, ", ");
677 }
678 write_type_space(out, &arg.ty);
679 write!(out, "{}", arg.ident);
680 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700681 if indirect_call {
682 if !sig.args.is_empty() {
683 write!(out, ", ");
684 }
685 write!(out, "void *extern$");
686 }
David Tolnay1e548172020-03-16 13:37:09 -0700687 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700688 if let Some(receiver) = &sig.receiver {
689 if receiver.mutability.is_none() {
690 write!(out, " const");
691 }
692 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700693 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700694 write!(out, " noexcept");
695 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700696}
697
698fn write_rust_function_shim_impl(
699 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700700 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700701 sig: &Signature,
702 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700703 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700704 indirect_call: bool,
705) {
706 if out.header && sig.receiver.is_some() {
707 // We've already defined this inside the struct.
708 return;
709 }
David Tolnaya73853b2020-04-20 01:19:56 -0700710 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400711 if out.header {
712 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700713 return;
David Tolnay7db73692019-10-20 14:51:12 -0400714 }
David Tolnay439cde22020-04-20 00:46:25 -0700715 writeln!(out, " {{");
716 for arg in &sig.args {
717 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
718 out.include.utility = true;
719 write!(out, " ::rust::ManuallyDrop<");
720 write_type(out, &arg.ty);
721 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
722 }
723 }
724 write!(out, " ");
725 let indirect_return = indirect_return(sig, types);
726 if indirect_return {
727 write!(out, "::rust::MaybeUninit<");
728 write_type(out, sig.ret.as_ref().unwrap());
729 writeln!(out, "> return$;");
730 write!(out, " ");
731 } else if let Some(ret) = &sig.ret {
732 write!(out, "return ");
733 match ret {
734 Type::RustBox(_) => {
735 write_type(out, ret);
736 write!(out, "::from_raw(");
737 }
738 Type::UniquePtr(_) => {
739 write_type(out, ret);
740 write!(out, "(");
741 }
742 Type::Ref(_) => write!(out, "*"),
743 _ => {}
744 }
745 }
746 if sig.throws {
747 write!(out, "::rust::Str::Repr error$ = ");
748 }
749 write!(out, "{}(", invoke);
750 if sig.receiver.is_some() {
751 write!(out, "*this");
752 }
753 for (i, arg) in sig.args.iter().enumerate() {
754 if i > 0 || sig.receiver.is_some() {
755 write!(out, ", ");
756 }
757 match &arg.ty {
758 Type::Str(_) => write!(out, "::rust::Str::Repr("),
759 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
760 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
761 _ => {}
762 }
763 write!(out, "{}", arg.ident);
764 match &arg.ty {
765 Type::RustBox(_) => write!(out, ".into_raw()"),
766 Type::UniquePtr(_) => write!(out, ".release()"),
767 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
768 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
769 _ => {}
770 }
771 }
772 if indirect_return {
773 if !sig.args.is_empty() {
774 write!(out, ", ");
775 }
776 write!(out, "&return$.value");
777 }
778 if indirect_call {
779 if !sig.args.is_empty() || indirect_return {
780 write!(out, ", ");
781 }
782 write!(out, "extern$");
783 }
784 write!(out, ")");
785 if let Some(ret) = &sig.ret {
786 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
787 write!(out, ")");
788 }
789 }
790 writeln!(out, ";");
791 if sig.throws {
792 writeln!(out, " if (error$.ptr) {{");
793 writeln!(out, " throw ::rust::Error(error$);");
794 writeln!(out, " }}");
795 }
796 if indirect_return {
797 out.include.utility = true;
798 writeln!(out, " return ::std::move(return$.value);");
799 }
800 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400801}
802
803fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
804 match ty {
805 None => write!(out, "void "),
806 Some(ty) => write_type_space(out, ty),
807 }
808}
809
David Tolnay75dca2e2020-03-25 20:17:52 -0700810fn indirect_return(sig: &Signature, types: &Types) -> bool {
811 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700812 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700813 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700814}
815
David Tolnay99642622020-03-25 13:07:35 -0700816fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
817 match ty {
818 Type::RustBox(ty) | Type::UniquePtr(ty) => {
819 write_type_space(out, &ty.inner);
820 write!(out, "*");
821 }
822 Type::Ref(ty) => {
823 if ty.mutability.is_none() {
824 write!(out, "const ");
825 }
826 write_type(out, &ty.inner);
827 write!(out, " *");
828 }
829 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700830 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700831 _ => write_type(out, ty),
832 }
833}
834
835fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
836 write_indirect_return_type(out, ty);
837 match ty {
838 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700839 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700840 _ => write_space_after_type(out, ty),
841 }
842}
843
844fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400845 match ty {
846 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
847 write_type_space(out, &ty.inner);
848 write!(out, "*");
849 }
David Tolnay4a441222020-01-25 16:24:27 -0800850 Some(Type::Ref(ty)) => {
851 if ty.mutability.is_none() {
852 write!(out, "const ");
853 }
854 write_type(out, &ty.inner);
855 write!(out, " *");
856 }
David Tolnay750755e2020-03-01 13:04:08 -0800857 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700858 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400859 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
860 _ => write_return_type(out, ty),
861 }
862}
863
864fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
865 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700866 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400867 write_type_space(out, &ty.inner);
868 write!(out, "*");
869 }
David Tolnay750755e2020-03-01 13:04:08 -0800870 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700871 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400872 _ => write_type_space(out, &arg.ty),
873 }
874 if types.needs_indirect_abi(&arg.ty) {
875 write!(out, "*");
876 }
877 write!(out, "{}", arg.ident);
878}
879
880fn write_type(out: &mut OutFile, ty: &Type) {
881 match ty {
882 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700883 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400884 None => write!(out, "{}", ident),
885 },
886 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800887 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400888 write_type(out, &ty.inner);
889 write!(out, ">");
890 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700891 Type::RustVec(ty) => {
892 write!(out, "::rust::Vec<");
893 write_type(out, &ty.inner);
894 write!(out, ">");
895 }
David Tolnay7db73692019-10-20 14:51:12 -0400896 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800897 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400898 write_type(out, &ptr.inner);
899 write!(out, ">");
900 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700901 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700902 write!(out, "::std::vector<");
903 write_type(out, &ty.inner);
904 write!(out, ">");
905 }
David Tolnay7db73692019-10-20 14:51:12 -0400906 Type::Ref(r) => {
907 if r.mutability.is_none() {
908 write!(out, "const ");
909 }
910 write_type(out, &r.inner);
911 write!(out, " &");
912 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700913 Type::Slice(_) => {
914 // For now, only U8 slices are supported, which are covered separately below
915 unreachable!()
916 }
David Tolnay7db73692019-10-20 14:51:12 -0400917 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800918 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400919 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700920 Type::SliceRefU8(_) => {
921 write!(out, "::rust::Slice<uint8_t>");
922 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700923 Type::Fn(f) => {
924 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
925 match &f.ret {
926 Some(ret) => write_type(out, ret),
927 None => write!(out, "void"),
928 }
929 write!(out, "(");
930 for (i, arg) in f.args.iter().enumerate() {
931 if i > 0 {
932 write!(out, ", ");
933 }
934 write_type(out, &arg.ty);
935 }
936 write!(out, ")>");
937 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700938 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400939 }
940}
941
David Tolnayf6a89f22020-05-10 23:39:27 -0700942fn write_atom(out: &mut OutFile, atom: Atom) {
943 match atom {
944 Bool => write!(out, "bool"),
945 U8 => write!(out, "uint8_t"),
946 U16 => write!(out, "uint16_t"),
947 U32 => write!(out, "uint32_t"),
948 U64 => write!(out, "uint64_t"),
949 Usize => write!(out, "size_t"),
950 I8 => write!(out, "int8_t"),
951 I16 => write!(out, "int16_t"),
952 I32 => write!(out, "int32_t"),
953 I64 => write!(out, "int64_t"),
954 Isize => write!(out, "::rust::isize"),
955 F32 => write!(out, "float"),
956 F64 => write!(out, "double"),
957 CxxString => write!(out, "::std::string"),
958 RustString => write!(out, "::rust::String"),
959 }
960}
961
David Tolnay7db73692019-10-20 14:51:12 -0400962fn write_type_space(out: &mut OutFile, ty: &Type) {
963 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700964 write_space_after_type(out, ty);
965}
966
967fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400968 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700969 Type::Ident(_)
970 | Type::RustBox(_)
971 | Type::UniquePtr(_)
972 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700973 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700974 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700975 | Type::SliceRefU8(_)
976 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400977 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700978 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400979 }
980}
981
David Tolnaycd08c442020-04-25 10:16:33 -0700982// Only called for legal referent types of unique_ptr and element types of
983// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700984fn to_typename(namespace: &Namespace, ty: &Type) -> String {
985 match ty {
986 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700987 let mut path = String::new();
988 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700989 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700990 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700991 }
David Tolnaycd08c442020-04-25 10:16:33 -0700992 path += &ident.to_string();
993 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700994 }
995 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700996 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700997 }
998}
999
David Tolnayacdf20a2020-04-25 12:40:53 -07001000// Only called for legal referent types of unique_ptr and element types of
1001// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001002fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1003 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001004 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001005 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001006 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001007 }
1008}
1009
David Tolnay7db73692019-10-20 14:51:12 -04001010fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001011 out.begin_block("extern \"C\"");
1012 for ty in types {
1013 if let Type::RustBox(ty) = ty {
1014 if let Type::Ident(inner) = &ty.inner {
1015 out.next_section();
1016 write_rust_box_extern(out, inner);
1017 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001018 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001019 if let Type::Ident(inner) = &ty.inner {
1020 if Atom::from(inner).is_none() {
1021 out.next_section();
1022 write_rust_vec_extern(out, inner);
1023 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001024 }
David Tolnay7db73692019-10-20 14:51:12 -04001025 } else if let Type::UniquePtr(ptr) = ty {
1026 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001027 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001028 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001029 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001030 }
1031 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001032 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001033 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001034 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001035 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001036 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001037 }
1038 }
1039 }
1040 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001041 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001042
David Tolnay750755e2020-03-01 13:04:08 -08001043 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001044 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001045 for ty in types {
1046 if let Type::RustBox(ty) = ty {
1047 if let Type::Ident(inner) = &ty.inner {
1048 write_rust_box_impl(out, inner);
1049 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001050 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001051 if let Type::Ident(inner) = &ty.inner {
1052 if Atom::from(inner).is_none() {
1053 write_rust_vec_impl(out, inner);
1054 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001055 }
David Tolnay7db73692019-10-20 14:51:12 -04001056 }
1057 }
David Tolnay69601622020-04-29 18:48:36 -07001058 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001059 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001060}
1061
1062fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1063 let mut inner = String::new();
1064 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001065 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001066 inner += "::";
1067 }
1068 inner += &ident.to_string();
1069 let instance = inner.replace("::", "$");
1070
David Tolnay69601622020-04-29 18:48:36 -07001071 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1072 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001073 writeln!(
1074 out,
David Tolnay69601622020-04-29 18:48:36 -07001075 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001076 instance, inner,
1077 );
1078 writeln!(
1079 out,
David Tolnay69601622020-04-29 18:48:36 -07001080 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001081 instance, inner,
1082 );
David Tolnay69601622020-04-29 18:48:36 -07001083 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001084}
1085
David Tolnay6787be62020-04-25 11:01:02 -07001086fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1087 let element = Type::Ident(element.clone());
1088 let inner = to_typename(&out.namespace, &element);
1089 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001090
David Tolnay69601622020-04-29 18:48:36 -07001091 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1092 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001093 writeln!(
1094 out,
David Tolnay69601622020-04-29 18:48:36 -07001095 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001096 instance, inner,
1097 );
1098 writeln!(
1099 out,
David Tolnay69601622020-04-29 18:48:36 -07001100 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001101 instance, inner,
1102 );
1103 writeln!(
1104 out,
David Tolnay69601622020-04-29 18:48:36 -07001105 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001106 instance, inner,
1107 );
David Tolnay219c0792020-04-24 20:31:37 -07001108 writeln!(
1109 out,
David Tolnay69601622020-04-29 18:48:36 -07001110 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001111 inner, instance,
1112 );
David Tolnay503d0192020-04-24 22:18:56 -07001113 writeln!(
1114 out,
David Tolnay69601622020-04-29 18:48:36 -07001115 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001116 instance,
1117 );
David Tolnay69601622020-04-29 18:48:36 -07001118 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001119}
1120
David Tolnay7db73692019-10-20 14:51:12 -04001121fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1122 let mut inner = String::new();
1123 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001124 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001125 inner += "::";
1126 }
1127 inner += &ident.to_string();
1128 let instance = inner.replace("::", "$");
1129
1130 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001131 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001132 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001133 writeln!(out, "}}");
1134
1135 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001136 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001137 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001138 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001139}
1140
David Tolnay6787be62020-04-25 11:01:02 -07001141fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1142 let element = Type::Ident(element.clone());
1143 let inner = to_typename(&out.namespace, &element);
1144 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001145
Myron Ahneba35cf2020-02-05 19:41:51 +07001146 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001147 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001148 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001149 writeln!(out, "}}");
1150
1151 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001152 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1153 writeln!(
1154 out,
David Tolnay69601622020-04-29 18:48:36 -07001155 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001156 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001157 );
1158 writeln!(out, "}}");
1159
1160 writeln!(out, "template <>");
1161 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001162 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001163 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001164
1165 writeln!(out, "template <>");
1166 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1167 writeln!(
1168 out,
David Tolnay69601622020-04-29 18:48:36 -07001169 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001170 instance,
1171 );
1172 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001173
1174 writeln!(out, "template <>");
1175 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001176 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001177 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001178}
1179
David Tolnay63da4d32020-04-25 09:41:12 -07001180fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1181 let ty = Type::Ident(ident.clone());
1182 let instance = to_mangled(&out.namespace, &ty);
1183
David Tolnay69601622020-04-29 18:48:36 -07001184 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1185 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001186
1187 write_unique_ptr_common(out, &ty, types);
1188
David Tolnay69601622020-04-29 18:48:36 -07001189 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001190}
1191
1192// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1193fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001194 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001195 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001196 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001197 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001198
David Tolnay63da4d32020-04-25 09:41:12 -07001199 let can_construct_from_value = match ty {
1200 Type::Ident(ident) => types.structs.contains_key(ident),
1201 _ => false,
1202 };
1203
David Tolnay7db73692019-10-20 14:51:12 -04001204 writeln!(
1205 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001206 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001207 inner,
1208 );
1209 writeln!(
1210 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001211 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001212 inner,
1213 );
1214 writeln!(
1215 out,
David Tolnay69601622020-04-29 18:48:36 -07001216 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001217 instance, inner,
1218 );
David Tolnay7e219b82020-03-01 13:14:51 -08001219 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001220 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001221 if can_construct_from_value {
1222 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001223 out,
David Tolnay69601622020-04-29 18:48:36 -07001224 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001225 instance, inner, inner,
1226 );
David Tolnay63da4d32020-04-25 09:41:12 -07001227 writeln!(
1228 out,
1229 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1230 inner, inner,
1231 );
1232 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001233 }
David Tolnay7db73692019-10-20 14:51:12 -04001234 writeln!(
1235 out,
David Tolnay69601622020-04-29 18:48:36 -07001236 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001237 instance, inner, inner,
1238 );
David Tolnay7e219b82020-03-01 13:14:51 -08001239 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001240 writeln!(out, "}}");
1241 writeln!(
1242 out,
David Tolnay69601622020-04-29 18:48:36 -07001243 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001244 inner, instance, inner,
1245 );
1246 writeln!(out, " return ptr.get();");
1247 writeln!(out, "}}");
1248 writeln!(
1249 out,
David Tolnay69601622020-04-29 18:48:36 -07001250 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001251 inner, instance, inner,
1252 );
1253 writeln!(out, " return ptr.release();");
1254 writeln!(out, "}}");
1255 writeln!(
1256 out,
David Tolnay69601622020-04-29 18:48:36 -07001257 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001258 instance, inner,
1259 );
1260 writeln!(out, " ptr->~unique_ptr();");
1261 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001262}
Myron Ahneba35cf2020-02-05 19:41:51 +07001263
David Tolnay92105da2020-04-25 11:15:31 -07001264fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001265 let element = Type::Ident(element.clone());
1266 let inner = to_typename(&out.namespace, &element);
1267 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001268
David Tolnay69601622020-04-29 18:48:36 -07001269 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1270 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001271 writeln!(
1272 out,
David Tolnay69601622020-04-29 18:48:36 -07001273 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001274 instance, inner,
1275 );
1276 writeln!(out, " return s.size();");
1277 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001278 writeln!(
1279 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001280 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001281 inner, instance, inner,
1282 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001283 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001284 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001285
1286 write_unique_ptr_common(out, vector_ty, types);
1287
David Tolnay69601622020-04-29 18:48:36 -07001288 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001289}