blob: 10482722586186e5fbc372b8cb1e04e67dbba7c4 [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 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070083 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040084 }
85 }
86
David Tolnayfabca772020-10-03 21:25:41 -070087 out.next_section();
88 for api in apis {
89 if let Api::TypeAlias(ety) = api {
90 if types.required_trivial.contains_key(&ety.ident) {
91 check_trivial_extern_type(out, &ety.ident)
92 }
93 }
94 }
95
David Tolnay7db73692019-10-20 14:51:12 -040096 if !header {
97 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070098 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040099 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700100 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400101 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
102 Api::RustFunction(efn) => (efn, write_rust_function_decl),
103 _ => continue,
104 };
105 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700106 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400107 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800108 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400109 }
110
111 for api in apis {
112 if let Api::RustFunction(efn) = api {
113 out.next_section();
114 write_rust_function_shim(out, efn, types);
115 }
116 }
117
118 out.next_section();
119 for name in namespace.iter().rev() {
120 writeln!(out, "}} // namespace {}", name);
121 }
122
123 if !header {
124 out.next_section();
125 write_generic_instantiations(out, types);
126 }
127
David Tolnay54702b92020-07-31 11:50:09 -0700128 write!(out.front, "{}", out.include);
129
130 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400131}
132
133fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400134 for ty in types {
135 match ty {
David Tolnay89e386d2020-10-03 19:02:19 -0700136 Type::Ident(ident) => match Atom::from(ident) {
137 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
138 | Some(I64) => out.include.cstdint = true,
139 Some(Usize) => out.include.cstddef = true,
140 Some(CxxString) => out.include.string = true,
David Tolnayf57f7562020-10-04 19:56:26 -0700141 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700142 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800143 Type::RustBox(_) => out.include.type_traits = true,
144 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700145 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700146 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700147 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400148 }
149 }
David Tolnay7db73692019-10-20 14:51:12 -0400150}
151
David Tolnayf51447e2020-03-06 14:14:27 -0800152fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay16ab1462020-09-02 15:10:09 -0700153 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700154 let mut needs_rust_string = false;
155 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700156 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400157 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700158 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700159 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700160 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700161 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400162 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700163 match ty {
164 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700165 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 out.include.type_traits = true;
167 needs_rust_box = true;
168 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700169 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700170 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700171 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700172 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700173 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700174 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700175 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700176 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700177 Type::Str(_) => {
178 out.include.cstdint = true;
179 out.include.string = true;
180 needs_rust_str = true;
181 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700182 Type::Fn(_) => {
183 needs_rust_fn = true;
184 }
David Tolnay4770b472020-04-14 16:32:59 -0700185 Type::Slice(_) | Type::SliceRefU8(_) => {
186 needs_rust_slice = true;
187 }
David Tolnay7c295462020-04-25 12:45:07 -0700188 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400189 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700190 needs_rust_isize = true;
191 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700192 ty if ty == RustString => {
193 out.include.array = true;
194 out.include.cstdint = true;
195 out.include.string = true;
196 needs_rust_string = true;
197 }
198 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400199 }
200 }
201
David Tolnayb7a7cb62020-03-17 21:18:40 -0700202 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800203 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800204 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700205 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800206 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 match api {
208 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700209 if efn.throws {
210 needs_trycatch = true;
211 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700212 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700213 let bitcopy = match arg.ty {
214 Type::RustVec(_) => true,
215 _ => arg.ty == RustString,
216 };
217 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700218 needs_unsafe_bitcopy = true;
219 break;
220 }
David Tolnay09011c32020-03-06 14:40:28 -0800221 }
222 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700223 Api::RustFunction(efn) if !out.header => {
224 if efn.throws {
225 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700226 out.include.string = true;
227 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200229 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700230 }
231 for arg in &efn.args {
232 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
233 needs_manually_drop = true;
234 break;
235 }
236 }
237 if let Some(ret) = &efn.ret {
238 if types.needs_indirect_abi(ret) {
239 needs_maybe_uninit = true;
240 }
David Tolnayf51447e2020-03-06 14:14:27 -0800241 }
242 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800244 }
245 }
246
David Tolnay750755e2020-03-01 13:04:08 -0800247 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700248 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800249
David Tolnay16ab1462020-09-02 15:10:09 -0700250 if needs_panic
251 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700252 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700253 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700254 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700255 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700256 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700257 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700258 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700259 || needs_unsafe_bitcopy
260 || needs_manually_drop
261 || needs_maybe_uninit
262 {
David Tolnay736cbca2020-03-11 16:49:18 -0700263 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800264 }
265
David Tolnay16ab1462020-09-02 15:10:09 -0700266 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
267
David Tolnay99a95e62020-09-02 15:05:53 -0700268 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700269 out.next_section();
270 writeln!(out, "struct unsafe_bitcopy_t;");
271 }
272
David Tolnay591dcb62020-09-01 23:00:38 -0700273 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
274 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
275 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
276 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700277 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700278 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
279 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
280 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
281 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800282
283 if needs_manually_drop {
284 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700285 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800286 writeln!(out, "template <typename T>");
287 writeln!(out, "union ManuallyDrop {{");
288 writeln!(out, " T value;");
289 writeln!(
290 out,
291 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
292 );
293 writeln!(out, " ~ManuallyDrop() {{}}");
294 writeln!(out, "}};");
295 }
296
David Tolnay09011c32020-03-06 14:40:28 -0800297 if needs_maybe_uninit {
298 out.next_section();
299 writeln!(out, "template <typename T>");
300 writeln!(out, "union MaybeUninit {{");
301 writeln!(out, " T value;");
302 writeln!(out, " MaybeUninit() {{}}");
303 writeln!(out, " ~MaybeUninit() {{}}");
304 writeln!(out, "}};");
305 }
306
David Tolnay591dcb62020-09-01 23:00:38 -0700307 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700308
David Tolnay5d121442020-03-17 22:14:40 -0700309 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700310 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700311 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700312 out.include.type_traits = true;
313 out.include.utility = true;
314 writeln!(out, "class missing {{}};");
315 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700316 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700317 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700318 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700319 writeln!(
320 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700321 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700322 );
David Tolnay04722332020-03-18 11:31:54 -0700323 writeln!(out, " missing>::value>::type");
324 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700325 writeln!(out, " func();");
326 writeln!(out, "}} catch (const ::std::exception &e) {{");
327 writeln!(out, " fail(e.what());");
328 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700329 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700330 }
331
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800332 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400333}
334
335fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700336 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700337 writeln!(out, "#ifndef {}", guard);
338 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400339 for line in strct.doc.to_string().lines() {
340 writeln!(out, "//{}", line);
341 }
342 writeln!(out, "struct {} final {{", strct.ident);
343 for field in &strct.fields {
344 write!(out, " ");
345 write_type_space(out, &field.ty);
346 writeln!(out, "{};", field.ident);
347 }
348 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700349 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400350}
351
352fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
353 writeln!(out, "struct {};", ident);
354}
355
David Tolnay8861bee2020-01-20 18:39:24 -0800356fn write_struct_using(out: &mut OutFile, ident: &Ident) {
357 writeln!(out, "using {} = {};", ident, ident);
358}
359
David Tolnayc1fe0052020-04-17 15:15:06 -0700360fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700361 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700362 writeln!(out, "#ifndef {}", guard);
363 writeln!(out, "#define {}", guard);
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 Tolnaya25ea9c2020-08-27 22:59:38 -0700378 writeln!(out, "#endif // {}", guard);
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 Tolnay591dcb62020-09-01 23:00:38 -0700382 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700383 writeln!(out, "#ifndef {}", guard);
384 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700385 for line in enm.doc.to_string().lines() {
386 writeln!(out, "//{}", line);
387 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700388 write!(out, "enum class {} : ", enm.ident);
389 write_atom(out, enm.repr);
390 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700391 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700392 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700393 }
394 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700395 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700396}
397
Joel Galenson905eb2e2020-05-04 14:58:14 -0700398fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700399 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
400 write_atom(out, enm.repr);
401 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700402 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700403 write!(out, "static_assert(static_cast<");
404 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700405 writeln!(
406 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700407 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700408 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700409 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700410 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700411}
412
Adrian Taylor405e8742020-09-25 14:01:25 -0700413fn check_trivial_extern_type(out: &mut OutFile, id: &Ident) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700414 // NOTE: The following two static assertions are just nice-to-have and not
415 // necessary for soundness. That's because triviality is always declared by
416 // the user in the form of an unsafe impl of cxx::ExternType:
417 //
418 // unsafe impl ExternType for MyType {
419 // type Id = cxx::type_id!("...");
420 // type Kind = cxx::kind::Trivial;
421 // }
422 //
423 // Since the user went on the record with their unsafe impl to unsafely
424 // claim they KNOW that the type is trivial, it's fine for that to be on
425 // them if that were wrong.
426 //
427 // There may be a legitimate reason we'll want to remove these assertions
428 // for support of types that the programmer knows are Rust-movable despite
429 // not being recognized as such by the C++ type system due to a move
430 // constructor or destructor.
431
David Tolnayf57f7562020-10-04 19:56:26 -0700432 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700433 writeln!(out, "static_assert(");
434 writeln!(
435 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700436 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700437 id,
438 );
439 writeln!(
440 out,
441 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
442 id,
443 );
444 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700445 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700446 writeln!(
447 out,
448 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
449 id,
450 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700451}
452
David Tolnayebef4a22020-03-17 15:33:47 -0700453fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
454 let mut has_cxx_throws = false;
455 for api in apis {
456 if let Api::CxxFunction(efn) = api {
457 if efn.throws {
458 has_cxx_throws = true;
459 break;
460 }
461 }
462 }
463
464 if has_cxx_throws {
465 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700466 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700467 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700468 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700469 );
470 }
471}
472
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700473fn write_cxx_function_shim(
474 out: &mut OutFile,
475 efn: &ExternFn,
476 types: &Types,
477 impl_annotations: &Option<String>,
478) {
479 if !out.header {
480 if let Some(annotation) = impl_annotations {
481 write!(out, "{} ", annotation);
482 }
483 }
David Tolnayebef4a22020-03-17 15:33:47 -0700484 if efn.throws {
485 write!(out, "::rust::Str::Repr ");
486 } else {
David Tolnay99642622020-03-25 13:07:35 -0700487 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700488 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700489 let mangled = mangle::extern_fn(&out.namespace, efn);
490 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700491 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700492 if receiver.mutability.is_none() {
493 write!(out, "const ");
494 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700495 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700496 }
David Tolnay7db73692019-10-20 14:51:12 -0400497 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700498 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400499 write!(out, ", ");
500 }
David Tolnaya46a2372020-03-06 10:03:48 -0800501 if arg.ty == RustString {
502 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700503 } else if let Type::RustVec(_) = arg.ty {
504 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800505 }
David Tolnay7db73692019-10-20 14:51:12 -0400506 write_extern_arg(out, arg, types);
507 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700508 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400509 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700510 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400511 write!(out, ", ");
512 }
David Tolnay99642622020-03-25 13:07:35 -0700513 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400514 write!(out, "*return$");
515 }
516 writeln!(out, ") noexcept {{");
517 write!(out, " ");
518 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700519 match &efn.receiver {
520 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700521 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700522 }
David Tolnay7db73692019-10-20 14:51:12 -0400523 for (i, arg) in efn.args.iter().enumerate() {
524 if i > 0 {
525 write!(out, ", ");
526 }
527 write_type(out, &arg.ty);
528 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700529 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700530 if let Some(receiver) = &efn.receiver {
531 if receiver.mutability.is_none() {
532 write!(out, " const");
533 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700534 }
535 write!(out, " = ");
536 match &efn.receiver {
537 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700538 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700539 }
540 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400541 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700542 if efn.throws {
543 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700544 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700545 writeln!(out, " [&] {{");
546 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700547 }
David Tolnay7db73692019-10-20 14:51:12 -0400548 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700549 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400550 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700551 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400552 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700553 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400554 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700555 }
556 match &efn.ret {
557 Some(Type::Ref(_)) => write!(out, "&"),
558 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700559 Some(Type::SliceRefU8(_)) if !indirect_return => {
560 write!(out, "::rust::Slice<uint8_t>::Repr(")
561 }
David Tolnay99642622020-03-25 13:07:35 -0700562 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400563 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700564 match &efn.receiver {
565 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700566 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700567 }
David Tolnay7db73692019-10-20 14:51:12 -0400568 for (i, arg) in efn.args.iter().enumerate() {
569 if i > 0 {
570 write!(out, ", ");
571 }
572 if let Type::RustBox(_) = &arg.ty {
573 write_type(out, &arg.ty);
574 write!(out, "::from_raw({})", arg.ident);
575 } else if let Type::UniquePtr(_) = &arg.ty {
576 write_type(out, &arg.ty);
577 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800578 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800579 write!(
580 out,
581 "::rust::String(::rust::unsafe_bitcopy, *{})",
582 arg.ident,
583 );
David Tolnay313b10e2020-04-25 16:30:51 -0700584 } else if let Type::RustVec(_) = arg.ty {
585 write_type(out, &arg.ty);
586 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400587 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700588 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800589 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400590 } else {
591 write!(out, "{}", arg.ident);
592 }
593 }
594 write!(out, ")");
595 match &efn.ret {
596 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
597 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700598 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400599 _ => {}
600 }
601 if indirect_return {
602 write!(out, ")");
603 }
604 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700605 if efn.throws {
606 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700607 writeln!(out, " throw$.ptr = nullptr;");
608 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700609 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700610 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700611 writeln!(
612 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700613 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700614 );
David Tolnay5d121442020-03-17 22:14:40 -0700615 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700616 writeln!(out, " return throw$;");
617 }
David Tolnay7db73692019-10-20 14:51:12 -0400618 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 for arg in &efn.args {
620 if let Type::Fn(f) = &arg.ty {
621 let var = &arg.ident;
622 write_function_pointer_trampoline(out, efn, var, f, types);
623 }
624 }
625}
626
627fn write_function_pointer_trampoline(
628 out: &mut OutFile,
629 efn: &ExternFn,
630 var: &Ident,
631 f: &Signature,
632 types: &Types,
633) {
634 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700635 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700636 let indirect_call = true;
637 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
638
639 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700640 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700641 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400642}
643
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700644fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700645 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700646 let indirect_call = false;
647 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
648}
649
650fn write_rust_function_decl_impl(
651 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700652 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 sig: &Signature,
654 types: &Types,
655 indirect_call: bool,
656) {
657 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700658 write!(out, "::rust::Str::Repr ");
659 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700660 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700661 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700662 write!(out, "{}(", link_name);
663 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700664 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700665 if receiver.mutability.is_none() {
666 write!(out, "const ");
667 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700668 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700669 needs_comma = true;
670 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700671 for arg in &sig.args {
672 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400673 write!(out, ", ");
674 }
675 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700676 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400677 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700678 if indirect_return(sig, types) {
679 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400680 write!(out, ", ");
681 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700682 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400683 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700684 needs_comma = true;
685 }
686 if indirect_call {
687 if needs_comma {
688 write!(out, ", ");
689 }
690 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400691 }
692 writeln!(out, ") noexcept;");
693}
694
695fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400696 for line in efn.doc.to_string().lines() {
697 writeln!(out, "//{}", line);
698 }
David Tolnaya73853b2020-04-20 01:19:56 -0700699 let local_name = match &efn.sig.receiver {
700 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700701 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700702 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700703 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700704 let indirect_call = false;
705 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
706}
707
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700708fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700709 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700710 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700711 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700712 indirect_call: bool,
713) {
714 write_return_type(out, &sig.ret);
715 write!(out, "{}(", local_name);
716 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400717 if i > 0 {
718 write!(out, ", ");
719 }
720 write_type_space(out, &arg.ty);
721 write!(out, "{}", arg.ident);
722 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700723 if indirect_call {
724 if !sig.args.is_empty() {
725 write!(out, ", ");
726 }
727 write!(out, "void *extern$");
728 }
David Tolnay1e548172020-03-16 13:37:09 -0700729 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700730 if let Some(receiver) = &sig.receiver {
731 if receiver.mutability.is_none() {
732 write!(out, " const");
733 }
734 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700735 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700736 write!(out, " noexcept");
737 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700738}
739
740fn write_rust_function_shim_impl(
741 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700742 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700743 sig: &Signature,
744 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700745 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700746 indirect_call: bool,
747) {
748 if out.header && sig.receiver.is_some() {
749 // We've already defined this inside the struct.
750 return;
751 }
David Tolnaya73853b2020-04-20 01:19:56 -0700752 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400753 if out.header {
754 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700755 return;
David Tolnay7db73692019-10-20 14:51:12 -0400756 }
David Tolnay439cde22020-04-20 00:46:25 -0700757 writeln!(out, " {{");
758 for arg in &sig.args {
759 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
760 out.include.utility = true;
761 write!(out, " ::rust::ManuallyDrop<");
762 write_type(out, &arg.ty);
763 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
764 }
765 }
766 write!(out, " ");
767 let indirect_return = indirect_return(sig, types);
768 if indirect_return {
769 write!(out, "::rust::MaybeUninit<");
770 write_type(out, sig.ret.as_ref().unwrap());
771 writeln!(out, "> return$;");
772 write!(out, " ");
773 } else if let Some(ret) = &sig.ret {
774 write!(out, "return ");
775 match ret {
776 Type::RustBox(_) => {
777 write_type(out, ret);
778 write!(out, "::from_raw(");
779 }
780 Type::UniquePtr(_) => {
781 write_type(out, ret);
782 write!(out, "(");
783 }
784 Type::Ref(_) => write!(out, "*"),
785 _ => {}
786 }
787 }
788 if sig.throws {
789 write!(out, "::rust::Str::Repr error$ = ");
790 }
791 write!(out, "{}(", invoke);
792 if sig.receiver.is_some() {
793 write!(out, "*this");
794 }
795 for (i, arg) in sig.args.iter().enumerate() {
796 if i > 0 || sig.receiver.is_some() {
797 write!(out, ", ");
798 }
799 match &arg.ty {
800 Type::Str(_) => write!(out, "::rust::Str::Repr("),
801 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
802 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
803 _ => {}
804 }
805 write!(out, "{}", arg.ident);
806 match &arg.ty {
807 Type::RustBox(_) => write!(out, ".into_raw()"),
808 Type::UniquePtr(_) => write!(out, ".release()"),
809 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
810 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
811 _ => {}
812 }
813 }
814 if indirect_return {
815 if !sig.args.is_empty() {
816 write!(out, ", ");
817 }
818 write!(out, "&return$.value");
819 }
820 if indirect_call {
821 if !sig.args.is_empty() || indirect_return {
822 write!(out, ", ");
823 }
824 write!(out, "extern$");
825 }
826 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400827 if !indirect_return {
828 if let Some(ret) = &sig.ret {
829 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
830 write!(out, ")");
831 }
David Tolnay439cde22020-04-20 00:46:25 -0700832 }
833 }
834 writeln!(out, ";");
835 if sig.throws {
836 writeln!(out, " if (error$.ptr) {{");
837 writeln!(out, " throw ::rust::Error(error$);");
838 writeln!(out, " }}");
839 }
840 if indirect_return {
841 out.include.utility = true;
842 writeln!(out, " return ::std::move(return$.value);");
843 }
844 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400845}
846
847fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
848 match ty {
849 None => write!(out, "void "),
850 Some(ty) => write_type_space(out, ty),
851 }
852}
853
David Tolnay75dca2e2020-03-25 20:17:52 -0700854fn indirect_return(sig: &Signature, types: &Types) -> bool {
855 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700856 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700857 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700858}
859
David Tolnay99642622020-03-25 13:07:35 -0700860fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
861 match ty {
862 Type::RustBox(ty) | Type::UniquePtr(ty) => {
863 write_type_space(out, &ty.inner);
864 write!(out, "*");
865 }
866 Type::Ref(ty) => {
867 if ty.mutability.is_none() {
868 write!(out, "const ");
869 }
870 write_type(out, &ty.inner);
871 write!(out, " *");
872 }
873 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700874 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700875 _ => write_type(out, ty),
876 }
877}
878
879fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
880 write_indirect_return_type(out, ty);
881 match ty {
882 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700883 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700884 _ => write_space_after_type(out, ty),
885 }
886}
887
888fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400889 match ty {
890 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
891 write_type_space(out, &ty.inner);
892 write!(out, "*");
893 }
David Tolnay4a441222020-01-25 16:24:27 -0800894 Some(Type::Ref(ty)) => {
895 if ty.mutability.is_none() {
896 write!(out, "const ");
897 }
898 write_type(out, &ty.inner);
899 write!(out, " *");
900 }
David Tolnay750755e2020-03-01 13:04:08 -0800901 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700902 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400903 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
904 _ => write_return_type(out, ty),
905 }
906}
907
908fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
909 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700910 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400911 write_type_space(out, &ty.inner);
912 write!(out, "*");
913 }
David Tolnay750755e2020-03-01 13:04:08 -0800914 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700915 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400916 _ => write_type_space(out, &arg.ty),
917 }
918 if types.needs_indirect_abi(&arg.ty) {
919 write!(out, "*");
920 }
921 write!(out, "{}", arg.ident);
922}
923
924fn write_type(out: &mut OutFile, ty: &Type) {
925 match ty {
926 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700927 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400928 None => write!(out, "{}", ident),
929 },
930 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800931 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400932 write_type(out, &ty.inner);
933 write!(out, ">");
934 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700935 Type::RustVec(ty) => {
936 write!(out, "::rust::Vec<");
937 write_type(out, &ty.inner);
938 write!(out, ">");
939 }
David Tolnay7db73692019-10-20 14:51:12 -0400940 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800941 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400942 write_type(out, &ptr.inner);
943 write!(out, ">");
944 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700945 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700946 write!(out, "::std::vector<");
947 write_type(out, &ty.inner);
948 write!(out, ">");
949 }
David Tolnay7db73692019-10-20 14:51:12 -0400950 Type::Ref(r) => {
951 if r.mutability.is_none() {
952 write!(out, "const ");
953 }
954 write_type(out, &r.inner);
955 write!(out, " &");
956 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700957 Type::Slice(_) => {
958 // For now, only U8 slices are supported, which are covered separately below
959 unreachable!()
960 }
David Tolnay7db73692019-10-20 14:51:12 -0400961 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800962 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400963 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700964 Type::SliceRefU8(_) => {
965 write!(out, "::rust::Slice<uint8_t>");
966 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700967 Type::Fn(f) => {
968 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
969 match &f.ret {
970 Some(ret) => write_type(out, ret),
971 None => write!(out, "void"),
972 }
973 write!(out, "(");
974 for (i, arg) in f.args.iter().enumerate() {
975 if i > 0 {
976 write!(out, ", ");
977 }
978 write_type(out, &arg.ty);
979 }
980 write!(out, ")>");
981 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700982 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400983 }
984}
985
David Tolnayf6a89f22020-05-10 23:39:27 -0700986fn write_atom(out: &mut OutFile, atom: Atom) {
987 match atom {
988 Bool => write!(out, "bool"),
989 U8 => write!(out, "uint8_t"),
990 U16 => write!(out, "uint16_t"),
991 U32 => write!(out, "uint32_t"),
992 U64 => write!(out, "uint64_t"),
993 Usize => write!(out, "size_t"),
994 I8 => write!(out, "int8_t"),
995 I16 => write!(out, "int16_t"),
996 I32 => write!(out, "int32_t"),
997 I64 => write!(out, "int64_t"),
998 Isize => write!(out, "::rust::isize"),
999 F32 => write!(out, "float"),
1000 F64 => write!(out, "double"),
1001 CxxString => write!(out, "::std::string"),
1002 RustString => write!(out, "::rust::String"),
1003 }
1004}
1005
David Tolnay7db73692019-10-20 14:51:12 -04001006fn write_type_space(out: &mut OutFile, ty: &Type) {
1007 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001008 write_space_after_type(out, ty);
1009}
1010
1011fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001012 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001013 Type::Ident(_)
1014 | Type::RustBox(_)
1015 | Type::UniquePtr(_)
1016 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001017 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001018 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001019 | Type::SliceRefU8(_)
1020 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001021 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001022 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001023 }
1024}
1025
David Tolnaycd08c442020-04-25 10:16:33 -07001026// Only called for legal referent types of unique_ptr and element types of
1027// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -07001028fn to_typename(namespace: &Namespace, ty: &Type) -> String {
1029 match ty {
1030 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -07001031 let mut path = String::new();
1032 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001033 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -07001034 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001035 }
David Tolnaycd08c442020-04-25 10:16:33 -07001036 path += &ident.to_string();
1037 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001038 }
1039 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001040 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001041 }
1042}
1043
David Tolnayacdf20a2020-04-25 12:40:53 -07001044// Only called for legal referent types of unique_ptr and element types of
1045// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001046fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1047 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001048 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001049 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001050 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001051 }
1052}
1053
David Tolnay7db73692019-10-20 14:51:12 -04001054fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001055 out.begin_block("extern \"C\"");
1056 for ty in types {
1057 if let Type::RustBox(ty) = ty {
1058 if let Type::Ident(inner) = &ty.inner {
1059 out.next_section();
1060 write_rust_box_extern(out, inner);
1061 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001062 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001063 if let Type::Ident(inner) = &ty.inner {
1064 if Atom::from(inner).is_none() {
1065 out.next_section();
1066 write_rust_vec_extern(out, inner);
1067 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001068 }
David Tolnay7db73692019-10-20 14:51:12 -04001069 } else if let Type::UniquePtr(ptr) = ty {
1070 if let Type::Ident(inner) = &ptr.inner {
David Tolnay7e69f892020-10-03 22:20:22 -07001071 if Atom::from(inner).is_none()
1072 && (!types.aliases.contains_key(inner) || types.explicit_impls.contains(ty))
1073 {
David Tolnay7db73692019-10-20 14:51:12 -04001074 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001075 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001076 }
1077 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001078 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001079 if let Type::Ident(inner) = &ptr.inner {
David Tolnay7e69f892020-10-03 22:20:22 -07001080 if Atom::from(inner).is_none()
1081 && (!types.aliases.contains_key(inner) || types.explicit_impls.contains(ty))
1082 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001083 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001084 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001085 }
1086 }
1087 }
1088 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001089 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001090
David Tolnay750755e2020-03-01 13:04:08 -08001091 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001092 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001093 for ty in types {
1094 if let Type::RustBox(ty) = ty {
1095 if let Type::Ident(inner) = &ty.inner {
1096 write_rust_box_impl(out, inner);
1097 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001098 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001099 if let Type::Ident(inner) = &ty.inner {
1100 if Atom::from(inner).is_none() {
1101 write_rust_vec_impl(out, inner);
1102 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001103 }
David Tolnay7db73692019-10-20 14:51:12 -04001104 }
1105 }
David Tolnay591dcb62020-09-01 23:00:38 -07001106 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001107 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001108}
1109
1110fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1111 let mut inner = String::new();
1112 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001113 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001114 inner += "::";
1115 }
1116 inner += &ident.to_string();
1117 let instance = inner.replace("::", "$");
1118
David Tolnay591dcb62020-09-01 23:00:38 -07001119 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1120 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001121 writeln!(
1122 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001123 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001124 instance, inner,
1125 );
1126 writeln!(
1127 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001128 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001129 instance, inner,
1130 );
David Tolnay591dcb62020-09-01 23:00:38 -07001131 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001132}
1133
David Tolnay6787be62020-04-25 11:01:02 -07001134fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1135 let element = Type::Ident(element.clone());
1136 let inner = to_typename(&out.namespace, &element);
1137 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001138
David Tolnay591dcb62020-09-01 23:00:38 -07001139 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1140 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001141 writeln!(
1142 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001143 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001144 instance, inner,
1145 );
1146 writeln!(
1147 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001148 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001149 instance, inner,
1150 );
1151 writeln!(
1152 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001153 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001154 instance, inner,
1155 );
David Tolnay219c0792020-04-24 20:31:37 -07001156 writeln!(
1157 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001158 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001159 inner, instance,
1160 );
David Tolnay503d0192020-04-24 22:18:56 -07001161 writeln!(
1162 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001163 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001164 instance,
1165 );
David Tolnay591dcb62020-09-01 23:00:38 -07001166 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001167}
1168
David Tolnay7db73692019-10-20 14:51:12 -04001169fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1170 let mut inner = String::new();
1171 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001172 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001173 inner += "::";
1174 }
1175 inner += &ident.to_string();
1176 let instance = inner.replace("::", "$");
1177
1178 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001179 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001180 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001181 writeln!(out, "}}");
1182
1183 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001184 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001185 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001186 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001187}
1188
David Tolnay6787be62020-04-25 11:01:02 -07001189fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1190 let element = Type::Ident(element.clone());
1191 let inner = to_typename(&out.namespace, &element);
1192 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001193
Myron Ahneba35cf2020-02-05 19:41:51 +07001194 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001195 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001196 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001197 writeln!(out, "}}");
1198
1199 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001200 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1201 writeln!(
1202 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001203 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001204 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001205 );
1206 writeln!(out, "}}");
1207
1208 writeln!(out, "template <>");
1209 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001210 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001211 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001212
1213 writeln!(out, "template <>");
1214 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1215 writeln!(
1216 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001217 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001218 instance,
1219 );
1220 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001221
1222 writeln!(out, "template <>");
1223 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001224 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001225 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001226}
1227
David Tolnay63da4d32020-04-25 09:41:12 -07001228fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1229 let ty = Type::Ident(ident.clone());
1230 let instance = to_mangled(&out.namespace, &ty);
1231
David Tolnay591dcb62020-09-01 23:00:38 -07001232 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1233 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001234
1235 write_unique_ptr_common(out, &ty, types);
1236
David Tolnay591dcb62020-09-01 23:00:38 -07001237 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001238}
1239
1240// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1241fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001242 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001243 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001244 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001245 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001246
David Tolnay63da4d32020-04-25 09:41:12 -07001247 let can_construct_from_value = match ty {
1248 Type::Ident(ident) => types.structs.contains_key(ident),
1249 _ => false,
1250 };
1251
David Tolnay7db73692019-10-20 14:51:12 -04001252 writeln!(
1253 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001254 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001255 inner,
1256 );
1257 writeln!(
1258 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001259 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001260 inner,
1261 );
1262 writeln!(
1263 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001264 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001265 instance, inner,
1266 );
David Tolnay7e219b82020-03-01 13:14:51 -08001267 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001268 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001269 if can_construct_from_value {
1270 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001271 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001272 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001273 instance, inner, inner,
1274 );
David Tolnay63da4d32020-04-25 09:41:12 -07001275 writeln!(
1276 out,
1277 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1278 inner, inner,
1279 );
1280 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001281 }
David Tolnay7db73692019-10-20 14:51:12 -04001282 writeln!(
1283 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001284 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001285 instance, inner, inner,
1286 );
David Tolnay7e219b82020-03-01 13:14:51 -08001287 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001288 writeln!(out, "}}");
1289 writeln!(
1290 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001291 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001292 inner, instance, inner,
1293 );
1294 writeln!(out, " return ptr.get();");
1295 writeln!(out, "}}");
1296 writeln!(
1297 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001298 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001299 inner, instance, inner,
1300 );
1301 writeln!(out, " return ptr.release();");
1302 writeln!(out, "}}");
1303 writeln!(
1304 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001305 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001306 instance, inner,
1307 );
1308 writeln!(out, " ptr->~unique_ptr();");
1309 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001310}
Myron Ahneba35cf2020-02-05 19:41:51 +07001311
David Tolnay92105da2020-04-25 11:15:31 -07001312fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001313 let element = Type::Ident(element.clone());
1314 let inner = to_typename(&out.namespace, &element);
1315 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001316
David Tolnay591dcb62020-09-01 23:00:38 -07001317 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1318 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001319 writeln!(
1320 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001321 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001322 instance, inner,
1323 );
1324 writeln!(out, " return s.size();");
1325 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001326 writeln!(
1327 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001328 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001329 inner, instance, inner,
1330 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001331 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001332 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001333
1334 write_unique_ptr_common(out, vector_ty, types);
1335
David Tolnay591dcb62020-09-01 23:00:38 -07001336 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001337}