blob: 5ea84fc7c01ead651445ae2f27b71b00f2e931b3 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Joel Galensonc03402a2020-04-23 17:31:09 -07006use crate::syntax::{mangle, Api, Enum, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -07008use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04009
David Tolnay33d30292020-03-18 18:02:02 -070010pub(super) fn gen(
David Tolnay2ec14632020-05-04 00:47:10 -070011 namespace: &Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070012 apis: &[Api],
13 types: &Types,
David Tolnaya5cca312020-08-29 23:40:04 -070014 opt: &Opt,
David Tolnay33d30292020-03-18 18:02:02 -070015 header: bool,
16) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040017 let mut out_file = OutFile::new(namespace.clone(), header);
18 let out = &mut out_file;
19
David Tolnay54702b92020-07-31 11:50:09 -070020 if header {
21 writeln!(out.front, "#pragma once");
22 }
23
David Tolnaya5cca312020-08-29 23:40:04 -070024 out.include.extend(opt.include.clone());
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070027 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070035 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040036 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070045 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040046 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
David Tolnaya593d6e2020-08-29 19:48:08 -070065 if !types.cxx.contains(&strct.ident) {
66 write_struct(out, strct);
67 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070068 }
Joel Galensonc03402a2020-04-23 17:31:09 -070069 Api::Enum(enm) => {
70 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070071 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070072 check_enum(out, enm);
73 } else {
74 write_enum(out, enm);
75 }
Joel Galensonc03402a2020-04-23 17:31:09 -070076 }
David Tolnayc1fe0052020-04-17 15:15:06 -070077 Api::RustType(ety) => {
78 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070079 out.next_section();
80 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070081 }
David Tolnayc1fe0052020-04-17 15:15:06 -070082 }
Adrian Taylor405e8742020-09-25 14:01:25 -070083 Api::TypeAlias(ety) => {
84 if types.required_trivial_aliases.contains(&ety.ident) {
85 check_trivial_extern_type(out, &ety.ident)
86 }
87 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070088 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040089 }
90 }
91
92 if !header {
93 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070094 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040095 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070096 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040097 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
98 Api::RustFunction(efn) => (efn, write_rust_function_decl),
99 _ => continue,
100 };
101 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700102 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400103 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800104 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400105 }
106
107 for api in apis {
108 if let Api::RustFunction(efn) = api {
109 out.next_section();
110 write_rust_function_shim(out, efn, types);
111 }
112 }
113
114 out.next_section();
115 for name in namespace.iter().rev() {
116 writeln!(out, "}} // namespace {}", name);
117 }
118
119 if !header {
120 out.next_section();
121 write_generic_instantiations(out, types);
122 }
123
David Tolnay54702b92020-07-31 11:50:09 -0700124 write!(out.front, "{}", out.include);
125
126 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400127}
128
129fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400130 for ty in types {
131 match ty {
Adrian Taylor405e8742020-09-25 14:01:25 -0700132 Type::Ident(ident) => {
133 match Atom::from(ident) {
134 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16)
135 | Some(I32) | Some(I64) => out.include.cstdint = true,
136 Some(Usize) => out.include.cstddef = true,
137 Some(CxxString) => out.include.string = true,
138 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
139 };
140 if types.required_trivial_aliases.contains(&ident) {
141 out.include.type_traits = true;
142 };
143 }
David Tolnay9c68b1a2020-03-06 11:12:55 -0800144 Type::RustBox(_) => out.include.type_traits = true,
145 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700146 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700147 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700148 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400149 }
150 }
David Tolnay7db73692019-10-20 14:51:12 -0400151}
152
David Tolnayf51447e2020-03-06 14:14:27 -0800153fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay16ab1462020-09-02 15:10:09 -0700154 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700155 let mut needs_rust_string = false;
156 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700157 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400158 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700159 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700160 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700161 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700162 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400163 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700164 match ty {
165 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700166 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700167 out.include.type_traits = true;
168 needs_rust_box = true;
169 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700170 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700171 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700172 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700173 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700174 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700175 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700176 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700177 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 Type::Str(_) => {
179 out.include.cstdint = true;
180 out.include.string = true;
181 needs_rust_str = true;
182 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700183 Type::Fn(_) => {
184 needs_rust_fn = true;
185 }
David Tolnay4770b472020-04-14 16:32:59 -0700186 Type::Slice(_) | Type::SliceRefU8(_) => {
187 needs_rust_slice = true;
188 }
David Tolnay7c295462020-04-25 12:45:07 -0700189 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400190 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700191 needs_rust_isize = true;
192 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 ty if ty == RustString => {
194 out.include.array = true;
195 out.include.cstdint = true;
196 out.include.string = true;
197 needs_rust_string = true;
198 }
199 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400200 }
201 }
202
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800204 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800205 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700206 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800207 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700208 match api {
209 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700210 if efn.throws {
211 needs_trycatch = true;
212 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700214 let bitcopy = match arg.ty {
215 Type::RustVec(_) => true,
216 _ => arg.ty == RustString,
217 };
218 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 needs_unsafe_bitcopy = true;
220 break;
221 }
David Tolnay09011c32020-03-06 14:40:28 -0800222 }
223 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700224 Api::RustFunction(efn) if !out.header => {
225 if efn.throws {
226 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700227 out.include.string = true;
228 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700229 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200230 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 }
232 for arg in &efn.args {
233 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
234 needs_manually_drop = true;
235 break;
236 }
237 }
238 if let Some(ret) = &efn.ret {
239 if types.needs_indirect_abi(ret) {
240 needs_maybe_uninit = true;
241 }
David Tolnayf51447e2020-03-06 14:14:27 -0800242 }
243 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700244 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800245 }
246 }
247
David Tolnay750755e2020-03-01 13:04:08 -0800248 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700249 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800250
David Tolnay16ab1462020-09-02 15:10:09 -0700251 if needs_panic
252 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700253 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700254 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700255 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700256 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700257 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700258 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700259 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700260 || needs_unsafe_bitcopy
261 || needs_manually_drop
262 || needs_maybe_uninit
263 {
David Tolnay736cbca2020-03-11 16:49:18 -0700264 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800265 }
266
David Tolnay16ab1462020-09-02 15:10:09 -0700267 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
268
David Tolnay99a95e62020-09-02 15:05:53 -0700269 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700270 out.next_section();
271 writeln!(out, "struct unsafe_bitcopy_t;");
272 }
273
David Tolnay591dcb62020-09-01 23:00:38 -0700274 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
275 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
276 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
277 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700278 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700279 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
280 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
281 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
282 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800283
284 if needs_manually_drop {
285 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700286 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800287 writeln!(out, "template <typename T>");
288 writeln!(out, "union ManuallyDrop {{");
289 writeln!(out, " T value;");
290 writeln!(
291 out,
292 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
293 );
294 writeln!(out, " ~ManuallyDrop() {{}}");
295 writeln!(out, "}};");
296 }
297
David Tolnay09011c32020-03-06 14:40:28 -0800298 if needs_maybe_uninit {
299 out.next_section();
300 writeln!(out, "template <typename T>");
301 writeln!(out, "union MaybeUninit {{");
302 writeln!(out, " T value;");
303 writeln!(out, " MaybeUninit() {{}}");
304 writeln!(out, " ~MaybeUninit() {{}}");
305 writeln!(out, "}};");
306 }
307
David Tolnay591dcb62020-09-01 23:00:38 -0700308 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700309
David Tolnay5d121442020-03-17 22:14:40 -0700310 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700311 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700312 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700313 out.include.type_traits = true;
314 out.include.utility = true;
315 writeln!(out, "class missing {{}};");
316 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700317 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700318 writeln!(out, "template <typename Try, typename Fail>");
319 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700320 writeln!(
321 out,
David Tolnay04722332020-03-18 11:31:54 -0700322 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700323 );
David Tolnay04722332020-03-18 11:31:54 -0700324 writeln!(out, " missing>::value>::type");
325 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700326 writeln!(out, " func();");
327 writeln!(out, "}} catch (const ::std::exception &e) {{");
328 writeln!(out, " fail(e.what());");
329 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700330 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700331 }
332
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800333 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400334}
335
336fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700337 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700338 writeln!(out, "#ifndef {}", guard);
339 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400340 for line in strct.doc.to_string().lines() {
341 writeln!(out, "//{}", line);
342 }
343 writeln!(out, "struct {} final {{", strct.ident);
344 for field in &strct.fields {
345 write!(out, " ");
346 write_type_space(out, &field.ty);
347 writeln!(out, "{};", field.ident);
348 }
349 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700350 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400351}
352
353fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
354 writeln!(out, "struct {};", ident);
355}
356
David Tolnay8861bee2020-01-20 18:39:24 -0800357fn write_struct_using(out: &mut OutFile, ident: &Ident) {
358 writeln!(out, "using {} = {};", ident, ident);
359}
360
David Tolnayc1fe0052020-04-17 15:15:06 -0700361fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700362 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700363 writeln!(out, "#ifndef {}", guard);
364 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700365 for line in ety.doc.to_string().lines() {
366 writeln!(out, "//{}", line);
367 }
368 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700369 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700370 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700371 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700372 write!(out, " ");
373 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700374 let local_name = method.ident.to_string();
375 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700376 writeln!(out, ";");
377 }
378 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700379 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700380}
381
Joel Galensonc03402a2020-04-23 17:31:09 -0700382fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700383 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700384 writeln!(out, "#ifndef {}", guard);
385 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700386 for line in enm.doc.to_string().lines() {
387 writeln!(out, "//{}", line);
388 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700389 write!(out, "enum class {} : ", enm.ident);
390 write_atom(out, enm.repr);
391 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700392 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700393 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700394 }
395 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700396 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700397}
398
Joel Galenson905eb2e2020-05-04 14:58:14 -0700399fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700400 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
401 write_atom(out, enm.repr);
402 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700403 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700404 write!(out, "static_assert(static_cast<");
405 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700406 writeln!(
407 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700408 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700409 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700410 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700411 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700412}
413
Adrian Taylor405e8742020-09-25 14:01:25 -0700414fn check_trivial_extern_type(out: &mut OutFile, id: &Ident) {
415 writeln!(out, "static_assert(std::is_trivially_move_constructible<{}>::value,\"type {} marked as Trivial in Rust is not trivially move constructible in C++\");", id, id);
416 writeln!(out, "static_assert(std::is_trivially_destructible<{}>::value,\"type {} marked as Trivial in Rust is not trivially destructible in C++\");", id, id);
417}
418
David Tolnayebef4a22020-03-17 15:33:47 -0700419fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
420 let mut has_cxx_throws = false;
421 for api in apis {
422 if let Api::CxxFunction(efn) = api {
423 if efn.throws {
424 has_cxx_throws = true;
425 break;
426 }
427 }
428 }
429
430 if has_cxx_throws {
431 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700432 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700433 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700434 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700435 );
436 }
437}
438
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700439fn write_cxx_function_shim(
440 out: &mut OutFile,
441 efn: &ExternFn,
442 types: &Types,
443 impl_annotations: &Option<String>,
444) {
445 if !out.header {
446 if let Some(annotation) = impl_annotations {
447 write!(out, "{} ", annotation);
448 }
449 }
David Tolnayebef4a22020-03-17 15:33:47 -0700450 if efn.throws {
451 write!(out, "::rust::Str::Repr ");
452 } else {
David Tolnay99642622020-03-25 13:07:35 -0700453 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700454 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700455 let mangled = mangle::extern_fn(&out.namespace, efn);
456 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700457 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700458 if receiver.mutability.is_none() {
459 write!(out, "const ");
460 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700461 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700462 }
David Tolnay7db73692019-10-20 14:51:12 -0400463 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700464 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400465 write!(out, ", ");
466 }
David Tolnaya46a2372020-03-06 10:03:48 -0800467 if arg.ty == RustString {
468 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700469 } else if let Type::RustVec(_) = arg.ty {
470 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800471 }
David Tolnay7db73692019-10-20 14:51:12 -0400472 write_extern_arg(out, arg, types);
473 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700474 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400475 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700476 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400477 write!(out, ", ");
478 }
David Tolnay99642622020-03-25 13:07:35 -0700479 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400480 write!(out, "*return$");
481 }
482 writeln!(out, ") noexcept {{");
483 write!(out, " ");
484 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700485 match &efn.receiver {
486 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700487 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700488 }
David Tolnay7db73692019-10-20 14:51:12 -0400489 for (i, arg) in efn.args.iter().enumerate() {
490 if i > 0 {
491 write!(out, ", ");
492 }
493 write_type(out, &arg.ty);
494 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700495 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700496 if let Some(receiver) = &efn.receiver {
497 if receiver.mutability.is_none() {
498 write!(out, " const");
499 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700500 }
501 write!(out, " = ");
502 match &efn.receiver {
503 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700504 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700505 }
506 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400507 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700508 if efn.throws {
509 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700510 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700511 writeln!(out, " [&] {{");
512 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700513 }
David Tolnay7db73692019-10-20 14:51:12 -0400514 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700515 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400516 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700517 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400518 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700519 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400520 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700521 }
522 match &efn.ret {
523 Some(Type::Ref(_)) => write!(out, "&"),
524 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700525 Some(Type::SliceRefU8(_)) if !indirect_return => {
526 write!(out, "::rust::Slice<uint8_t>::Repr(")
527 }
David Tolnay99642622020-03-25 13:07:35 -0700528 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400529 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700530 match &efn.receiver {
531 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700532 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700533 }
David Tolnay7db73692019-10-20 14:51:12 -0400534 for (i, arg) in efn.args.iter().enumerate() {
535 if i > 0 {
536 write!(out, ", ");
537 }
538 if let Type::RustBox(_) = &arg.ty {
539 write_type(out, &arg.ty);
540 write!(out, "::from_raw({})", arg.ident);
541 } else if let Type::UniquePtr(_) = &arg.ty {
542 write_type(out, &arg.ty);
543 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800544 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800545 write!(
546 out,
547 "::rust::String(::rust::unsafe_bitcopy, *{})",
548 arg.ident,
549 );
David Tolnay313b10e2020-04-25 16:30:51 -0700550 } else if let Type::RustVec(_) = arg.ty {
551 write_type(out, &arg.ty);
552 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400553 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700554 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800555 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400556 } else {
557 write!(out, "{}", arg.ident);
558 }
559 }
560 write!(out, ")");
561 match &efn.ret {
562 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
563 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700564 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400565 _ => {}
566 }
567 if indirect_return {
568 write!(out, ")");
569 }
570 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700571 if efn.throws {
572 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700573 writeln!(out, " throw$.ptr = nullptr;");
574 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700575 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700576 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700577 writeln!(
578 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700579 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700580 );
David Tolnay5d121442020-03-17 22:14:40 -0700581 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700582 writeln!(out, " return throw$;");
583 }
David Tolnay7db73692019-10-20 14:51:12 -0400584 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700585 for arg in &efn.args {
586 if let Type::Fn(f) = &arg.ty {
587 let var = &arg.ident;
588 write_function_pointer_trampoline(out, efn, var, f, types);
589 }
590 }
591}
592
593fn write_function_pointer_trampoline(
594 out: &mut OutFile,
595 efn: &ExternFn,
596 var: &Ident,
597 f: &Signature,
598 types: &Types,
599) {
600 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700601 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700602 let indirect_call = true;
603 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
604
605 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700606 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700607 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400608}
609
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700610fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700611 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 let indirect_call = false;
613 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
614}
615
616fn write_rust_function_decl_impl(
617 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700618 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 sig: &Signature,
620 types: &Types,
621 indirect_call: bool,
622) {
623 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700624 write!(out, "::rust::Str::Repr ");
625 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700626 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700627 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 write!(out, "{}(", link_name);
629 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700630 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700631 if receiver.mutability.is_none() {
632 write!(out, "const ");
633 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700634 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700635 needs_comma = true;
636 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700637 for arg in &sig.args {
638 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400639 write!(out, ", ");
640 }
641 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700642 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400643 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 if indirect_return(sig, types) {
645 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400646 write!(out, ", ");
647 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700648 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400649 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700650 needs_comma = true;
651 }
652 if indirect_call {
653 if needs_comma {
654 write!(out, ", ");
655 }
656 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400657 }
658 writeln!(out, ") noexcept;");
659}
660
661fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400662 for line in efn.doc.to_string().lines() {
663 writeln!(out, "//{}", line);
664 }
David Tolnaya73853b2020-04-20 01:19:56 -0700665 let local_name = match &efn.sig.receiver {
666 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700667 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700668 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700669 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700670 let indirect_call = false;
671 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
672}
673
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700674fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700675 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700676 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700677 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700678 indirect_call: bool,
679) {
680 write_return_type(out, &sig.ret);
681 write!(out, "{}(", local_name);
682 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400683 if i > 0 {
684 write!(out, ", ");
685 }
686 write_type_space(out, &arg.ty);
687 write!(out, "{}", arg.ident);
688 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700689 if indirect_call {
690 if !sig.args.is_empty() {
691 write!(out, ", ");
692 }
693 write!(out, "void *extern$");
694 }
David Tolnay1e548172020-03-16 13:37:09 -0700695 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700696 if let Some(receiver) = &sig.receiver {
697 if receiver.mutability.is_none() {
698 write!(out, " const");
699 }
700 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700701 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700702 write!(out, " noexcept");
703 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700704}
705
706fn write_rust_function_shim_impl(
707 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700708 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700709 sig: &Signature,
710 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700711 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700712 indirect_call: bool,
713) {
714 if out.header && sig.receiver.is_some() {
715 // We've already defined this inside the struct.
716 return;
717 }
David Tolnaya73853b2020-04-20 01:19:56 -0700718 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400719 if out.header {
720 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700721 return;
David Tolnay7db73692019-10-20 14:51:12 -0400722 }
David Tolnay439cde22020-04-20 00:46:25 -0700723 writeln!(out, " {{");
724 for arg in &sig.args {
725 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
726 out.include.utility = true;
727 write!(out, " ::rust::ManuallyDrop<");
728 write_type(out, &arg.ty);
729 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
730 }
731 }
732 write!(out, " ");
733 let indirect_return = indirect_return(sig, types);
734 if indirect_return {
735 write!(out, "::rust::MaybeUninit<");
736 write_type(out, sig.ret.as_ref().unwrap());
737 writeln!(out, "> return$;");
738 write!(out, " ");
739 } else if let Some(ret) = &sig.ret {
740 write!(out, "return ");
741 match ret {
742 Type::RustBox(_) => {
743 write_type(out, ret);
744 write!(out, "::from_raw(");
745 }
746 Type::UniquePtr(_) => {
747 write_type(out, ret);
748 write!(out, "(");
749 }
750 Type::Ref(_) => write!(out, "*"),
751 _ => {}
752 }
753 }
754 if sig.throws {
755 write!(out, "::rust::Str::Repr error$ = ");
756 }
757 write!(out, "{}(", invoke);
758 if sig.receiver.is_some() {
759 write!(out, "*this");
760 }
761 for (i, arg) in sig.args.iter().enumerate() {
762 if i > 0 || sig.receiver.is_some() {
763 write!(out, ", ");
764 }
765 match &arg.ty {
766 Type::Str(_) => write!(out, "::rust::Str::Repr("),
767 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
768 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
769 _ => {}
770 }
771 write!(out, "{}", arg.ident);
772 match &arg.ty {
773 Type::RustBox(_) => write!(out, ".into_raw()"),
774 Type::UniquePtr(_) => write!(out, ".release()"),
775 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
776 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
777 _ => {}
778 }
779 }
780 if indirect_return {
781 if !sig.args.is_empty() {
782 write!(out, ", ");
783 }
784 write!(out, "&return$.value");
785 }
786 if indirect_call {
787 if !sig.args.is_empty() || indirect_return {
788 write!(out, ", ");
789 }
790 write!(out, "extern$");
791 }
792 write!(out, ")");
793 if let Some(ret) = &sig.ret {
794 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
795 write!(out, ")");
796 }
797 }
798 writeln!(out, ";");
799 if sig.throws {
800 writeln!(out, " if (error$.ptr) {{");
801 writeln!(out, " throw ::rust::Error(error$);");
802 writeln!(out, " }}");
803 }
804 if indirect_return {
805 out.include.utility = true;
806 writeln!(out, " return ::std::move(return$.value);");
807 }
808 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400809}
810
811fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
812 match ty {
813 None => write!(out, "void "),
814 Some(ty) => write_type_space(out, ty),
815 }
816}
817
David Tolnay75dca2e2020-03-25 20:17:52 -0700818fn indirect_return(sig: &Signature, types: &Types) -> bool {
819 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700820 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700821 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700822}
823
David Tolnay99642622020-03-25 13:07:35 -0700824fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
825 match ty {
826 Type::RustBox(ty) | Type::UniquePtr(ty) => {
827 write_type_space(out, &ty.inner);
828 write!(out, "*");
829 }
830 Type::Ref(ty) => {
831 if ty.mutability.is_none() {
832 write!(out, "const ");
833 }
834 write_type(out, &ty.inner);
835 write!(out, " *");
836 }
837 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700838 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700839 _ => write_type(out, ty),
840 }
841}
842
843fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
844 write_indirect_return_type(out, ty);
845 match ty {
846 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700847 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700848 _ => write_space_after_type(out, ty),
849 }
850}
851
852fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400853 match ty {
854 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
855 write_type_space(out, &ty.inner);
856 write!(out, "*");
857 }
David Tolnay4a441222020-01-25 16:24:27 -0800858 Some(Type::Ref(ty)) => {
859 if ty.mutability.is_none() {
860 write!(out, "const ");
861 }
862 write_type(out, &ty.inner);
863 write!(out, " *");
864 }
David Tolnay750755e2020-03-01 13:04:08 -0800865 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700866 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400867 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
868 _ => write_return_type(out, ty),
869 }
870}
871
872fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
873 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700874 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400875 write_type_space(out, &ty.inner);
876 write!(out, "*");
877 }
David Tolnay750755e2020-03-01 13:04:08 -0800878 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700879 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400880 _ => write_type_space(out, &arg.ty),
881 }
882 if types.needs_indirect_abi(&arg.ty) {
883 write!(out, "*");
884 }
885 write!(out, "{}", arg.ident);
886}
887
888fn write_type(out: &mut OutFile, ty: &Type) {
889 match ty {
890 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700891 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400892 None => write!(out, "{}", ident),
893 },
894 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800895 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400896 write_type(out, &ty.inner);
897 write!(out, ">");
898 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700899 Type::RustVec(ty) => {
900 write!(out, "::rust::Vec<");
901 write_type(out, &ty.inner);
902 write!(out, ">");
903 }
David Tolnay7db73692019-10-20 14:51:12 -0400904 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800905 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400906 write_type(out, &ptr.inner);
907 write!(out, ">");
908 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700909 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700910 write!(out, "::std::vector<");
911 write_type(out, &ty.inner);
912 write!(out, ">");
913 }
David Tolnay7db73692019-10-20 14:51:12 -0400914 Type::Ref(r) => {
915 if r.mutability.is_none() {
916 write!(out, "const ");
917 }
918 write_type(out, &r.inner);
919 write!(out, " &");
920 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700921 Type::Slice(_) => {
922 // For now, only U8 slices are supported, which are covered separately below
923 unreachable!()
924 }
David Tolnay7db73692019-10-20 14:51:12 -0400925 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800926 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400927 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700928 Type::SliceRefU8(_) => {
929 write!(out, "::rust::Slice<uint8_t>");
930 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700931 Type::Fn(f) => {
932 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
933 match &f.ret {
934 Some(ret) => write_type(out, ret),
935 None => write!(out, "void"),
936 }
937 write!(out, "(");
938 for (i, arg) in f.args.iter().enumerate() {
939 if i > 0 {
940 write!(out, ", ");
941 }
942 write_type(out, &arg.ty);
943 }
944 write!(out, ")>");
945 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700946 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400947 }
948}
949
David Tolnayf6a89f22020-05-10 23:39:27 -0700950fn write_atom(out: &mut OutFile, atom: Atom) {
951 match atom {
952 Bool => write!(out, "bool"),
953 U8 => write!(out, "uint8_t"),
954 U16 => write!(out, "uint16_t"),
955 U32 => write!(out, "uint32_t"),
956 U64 => write!(out, "uint64_t"),
957 Usize => write!(out, "size_t"),
958 I8 => write!(out, "int8_t"),
959 I16 => write!(out, "int16_t"),
960 I32 => write!(out, "int32_t"),
961 I64 => write!(out, "int64_t"),
962 Isize => write!(out, "::rust::isize"),
963 F32 => write!(out, "float"),
964 F64 => write!(out, "double"),
965 CxxString => write!(out, "::std::string"),
966 RustString => write!(out, "::rust::String"),
967 }
968}
969
David Tolnay7db73692019-10-20 14:51:12 -0400970fn write_type_space(out: &mut OutFile, ty: &Type) {
971 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700972 write_space_after_type(out, ty);
973}
974
975fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400976 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700977 Type::Ident(_)
978 | Type::RustBox(_)
979 | Type::UniquePtr(_)
980 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700981 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700982 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700983 | Type::SliceRefU8(_)
984 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400985 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700986 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400987 }
988}
989
David Tolnaycd08c442020-04-25 10:16:33 -0700990// Only called for legal referent types of unique_ptr and element types of
991// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700992fn to_typename(namespace: &Namespace, ty: &Type) -> String {
993 match ty {
994 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700995 let mut path = String::new();
996 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700997 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700998 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700999 }
David Tolnaycd08c442020-04-25 10:16:33 -07001000 path += &ident.to_string();
1001 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001002 }
1003 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001004 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001005 }
1006}
1007
David Tolnayacdf20a2020-04-25 12:40:53 -07001008// Only called for legal referent types of unique_ptr and element types of
1009// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001010fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1011 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001012 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001013 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001014 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001015 }
1016}
1017
David Tolnay7db73692019-10-20 14:51:12 -04001018fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001019 out.begin_block("extern \"C\"");
1020 for ty in types {
1021 if let Type::RustBox(ty) = ty {
1022 if let Type::Ident(inner) = &ty.inner {
1023 out.next_section();
1024 write_rust_box_extern(out, inner);
1025 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001026 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001027 if let Type::Ident(inner) = &ty.inner {
1028 if Atom::from(inner).is_none() {
1029 out.next_section();
1030 write_rust_vec_extern(out, inner);
1031 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001032 }
David Tolnay7db73692019-10-20 14:51:12 -04001033 } else if let Type::UniquePtr(ptr) = ty {
1034 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001035 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001036 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001037 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001038 }
1039 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001040 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001041 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001042 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001043 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001044 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001045 }
1046 }
1047 }
1048 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001049 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001050
David Tolnay750755e2020-03-01 13:04:08 -08001051 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001052 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001053 for ty in types {
1054 if let Type::RustBox(ty) = ty {
1055 if let Type::Ident(inner) = &ty.inner {
1056 write_rust_box_impl(out, inner);
1057 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001058 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001059 if let Type::Ident(inner) = &ty.inner {
1060 if Atom::from(inner).is_none() {
1061 write_rust_vec_impl(out, inner);
1062 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001063 }
David Tolnay7db73692019-10-20 14:51:12 -04001064 }
1065 }
David Tolnay591dcb62020-09-01 23:00:38 -07001066 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001067 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001068}
1069
1070fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1071 let mut inner = String::new();
1072 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001073 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001074 inner += "::";
1075 }
1076 inner += &ident.to_string();
1077 let instance = inner.replace("::", "$");
1078
David Tolnay591dcb62020-09-01 23:00:38 -07001079 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1080 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001081 writeln!(
1082 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001083 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001084 instance, inner,
1085 );
1086 writeln!(
1087 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001088 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001089 instance, inner,
1090 );
David Tolnay591dcb62020-09-01 23:00:38 -07001091 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001092}
1093
David Tolnay6787be62020-04-25 11:01:02 -07001094fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1095 let element = Type::Ident(element.clone());
1096 let inner = to_typename(&out.namespace, &element);
1097 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001098
David Tolnay591dcb62020-09-01 23:00:38 -07001099 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1100 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001101 writeln!(
1102 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001103 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001104 instance, inner,
1105 );
1106 writeln!(
1107 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001108 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001109 instance, inner,
1110 );
1111 writeln!(
1112 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001113 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001114 instance, inner,
1115 );
David Tolnay219c0792020-04-24 20:31:37 -07001116 writeln!(
1117 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001118 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001119 inner, instance,
1120 );
David Tolnay503d0192020-04-24 22:18:56 -07001121 writeln!(
1122 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001123 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001124 instance,
1125 );
David Tolnay591dcb62020-09-01 23:00:38 -07001126 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001127}
1128
David Tolnay7db73692019-10-20 14:51:12 -04001129fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1130 let mut inner = String::new();
1131 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001132 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001133 inner += "::";
1134 }
1135 inner += &ident.to_string();
1136 let instance = inner.replace("::", "$");
1137
1138 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001139 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001140 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001141 writeln!(out, "}}");
1142
1143 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001144 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001145 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001146 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001147}
1148
David Tolnay6787be62020-04-25 11:01:02 -07001149fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1150 let element = Type::Ident(element.clone());
1151 let inner = to_typename(&out.namespace, &element);
1152 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001153
Myron Ahneba35cf2020-02-05 19:41:51 +07001154 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001155 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001156 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001157 writeln!(out, "}}");
1158
1159 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001160 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1161 writeln!(
1162 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001163 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001164 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001165 );
1166 writeln!(out, "}}");
1167
1168 writeln!(out, "template <>");
1169 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001170 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001171 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001172
1173 writeln!(out, "template <>");
1174 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1175 writeln!(
1176 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001177 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001178 instance,
1179 );
1180 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001181
1182 writeln!(out, "template <>");
1183 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001184 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001185 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001186}
1187
David Tolnay63da4d32020-04-25 09:41:12 -07001188fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1189 let ty = Type::Ident(ident.clone());
1190 let instance = to_mangled(&out.namespace, &ty);
1191
David Tolnay591dcb62020-09-01 23:00:38 -07001192 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1193 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001194
1195 write_unique_ptr_common(out, &ty, types);
1196
David Tolnay591dcb62020-09-01 23:00:38 -07001197 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001198}
1199
1200// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1201fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001202 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001203 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001204 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001205 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001206
David Tolnay63da4d32020-04-25 09:41:12 -07001207 let can_construct_from_value = match ty {
1208 Type::Ident(ident) => types.structs.contains_key(ident),
1209 _ => false,
1210 };
1211
David Tolnay7db73692019-10-20 14:51:12 -04001212 writeln!(
1213 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001214 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001215 inner,
1216 );
1217 writeln!(
1218 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001219 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001220 inner,
1221 );
1222 writeln!(
1223 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001224 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001225 instance, inner,
1226 );
David Tolnay7e219b82020-03-01 13:14:51 -08001227 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001228 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001229 if can_construct_from_value {
1230 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001231 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001232 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001233 instance, inner, inner,
1234 );
David Tolnay63da4d32020-04-25 09:41:12 -07001235 writeln!(
1236 out,
1237 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1238 inner, inner,
1239 );
1240 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001241 }
David Tolnay7db73692019-10-20 14:51:12 -04001242 writeln!(
1243 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001244 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001245 instance, inner, inner,
1246 );
David Tolnay7e219b82020-03-01 13:14:51 -08001247 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001248 writeln!(out, "}}");
1249 writeln!(
1250 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001251 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001252 inner, instance, inner,
1253 );
1254 writeln!(out, " return ptr.get();");
1255 writeln!(out, "}}");
1256 writeln!(
1257 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001258 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001259 inner, instance, inner,
1260 );
1261 writeln!(out, " return ptr.release();");
1262 writeln!(out, "}}");
1263 writeln!(
1264 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001265 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001266 instance, inner,
1267 );
1268 writeln!(out, " ptr->~unique_ptr();");
1269 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001270}
Myron Ahneba35cf2020-02-05 19:41:51 +07001271
David Tolnay92105da2020-04-25 11:15:31 -07001272fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001273 let element = Type::Ident(element.clone());
1274 let inner = to_typename(&out.namespace, &element);
1275 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001276
David Tolnay591dcb62020-09-01 23:00:38 -07001277 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1278 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001279 writeln!(
1280 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001281 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001282 instance, inner,
1283 );
1284 writeln!(out, " return s.size();");
1285 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001286 writeln!(
1287 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001288 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001289 inner, instance, inner,
1290 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001291 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001292 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001293
1294 write_unique_ptr_common(out, vector_ty, types);
1295
David Tolnay591dcb62020-09-01 23:00:38 -07001296 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001297}