blob: 1a071a26bec66c8f4b62e73c66c75959bb59959d [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,
141 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) => {}
142 None => {
David Tolnay3208fd72020-10-03 20:19:40 -0700143 if types.aliases.contains_key(ident)
144 && types.required_trivial.contains_key(ident)
145 {
David Tolnay89e386d2020-10-03 19:02:19 -0700146 out.include.type_traits = true;
147 }
148 }
149 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800150 Type::RustBox(_) => out.include.type_traits = true,
151 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700152 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700153 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700154 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400155 }
156 }
David Tolnay7db73692019-10-20 14:51:12 -0400157}
158
David Tolnayf51447e2020-03-06 14:14:27 -0800159fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay16ab1462020-09-02 15:10:09 -0700160 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700161 let mut needs_rust_string = false;
162 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700163 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400164 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700165 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700166 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700167 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700168 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400169 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700170 match ty {
171 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700172 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700173 out.include.type_traits = true;
174 needs_rust_box = true;
175 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700176 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700177 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700178 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700179 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700180 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700181 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700182 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700183 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700184 Type::Str(_) => {
185 out.include.cstdint = true;
186 out.include.string = true;
187 needs_rust_str = true;
188 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700189 Type::Fn(_) => {
190 needs_rust_fn = true;
191 }
David Tolnay4770b472020-04-14 16:32:59 -0700192 Type::Slice(_) | Type::SliceRefU8(_) => {
193 needs_rust_slice = true;
194 }
David Tolnay7c295462020-04-25 12:45:07 -0700195 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400196 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700197 needs_rust_isize = true;
198 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700199 ty if ty == RustString => {
200 out.include.array = true;
201 out.include.cstdint = true;
202 out.include.string = true;
203 needs_rust_string = true;
204 }
205 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400206 }
207 }
208
David Tolnayb7a7cb62020-03-17 21:18:40 -0700209 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800210 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800211 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700212 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800213 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 match api {
215 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700216 if efn.throws {
217 needs_trycatch = true;
218 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700220 let bitcopy = match arg.ty {
221 Type::RustVec(_) => true,
222 _ => arg.ty == RustString,
223 };
224 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700225 needs_unsafe_bitcopy = true;
226 break;
227 }
David Tolnay09011c32020-03-06 14:40:28 -0800228 }
229 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700230 Api::RustFunction(efn) if !out.header => {
231 if efn.throws {
232 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700233 out.include.string = true;
234 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200236 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 }
238 for arg in &efn.args {
239 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
240 needs_manually_drop = true;
241 break;
242 }
243 }
244 if let Some(ret) = &efn.ret {
245 if types.needs_indirect_abi(ret) {
246 needs_maybe_uninit = true;
247 }
David Tolnayf51447e2020-03-06 14:14:27 -0800248 }
249 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700250 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800251 }
252 }
253
David Tolnay750755e2020-03-01 13:04:08 -0800254 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700255 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800256
David Tolnay16ab1462020-09-02 15:10:09 -0700257 if needs_panic
258 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700259 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700260 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700261 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700262 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700263 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700264 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700265 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700266 || needs_unsafe_bitcopy
267 || needs_manually_drop
268 || needs_maybe_uninit
269 {
David Tolnay736cbca2020-03-11 16:49:18 -0700270 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800271 }
272
David Tolnay16ab1462020-09-02 15:10:09 -0700273 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
274
David Tolnay99a95e62020-09-02 15:05:53 -0700275 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700276 out.next_section();
277 writeln!(out, "struct unsafe_bitcopy_t;");
278 }
279
David Tolnay591dcb62020-09-01 23:00:38 -0700280 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
281 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
282 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
283 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700284 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700285 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
286 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
287 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
288 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800289
290 if needs_manually_drop {
291 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700292 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800293 writeln!(out, "template <typename T>");
294 writeln!(out, "union ManuallyDrop {{");
295 writeln!(out, " T value;");
296 writeln!(
297 out,
298 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
299 );
300 writeln!(out, " ~ManuallyDrop() {{}}");
301 writeln!(out, "}};");
302 }
303
David Tolnay09011c32020-03-06 14:40:28 -0800304 if needs_maybe_uninit {
305 out.next_section();
306 writeln!(out, "template <typename T>");
307 writeln!(out, "union MaybeUninit {{");
308 writeln!(out, " T value;");
309 writeln!(out, " MaybeUninit() {{}}");
310 writeln!(out, " ~MaybeUninit() {{}}");
311 writeln!(out, "}};");
312 }
313
David Tolnay591dcb62020-09-01 23:00:38 -0700314 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700315
David Tolnay5d121442020-03-17 22:14:40 -0700316 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700317 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700318 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700319 out.include.type_traits = true;
320 out.include.utility = true;
321 writeln!(out, "class missing {{}};");
322 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700323 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700324 writeln!(out, "template <typename Try, typename Fail>");
325 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700326 writeln!(
327 out,
David Tolnay04722332020-03-18 11:31:54 -0700328 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700329 );
David Tolnay04722332020-03-18 11:31:54 -0700330 writeln!(out, " missing>::value>::type");
331 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700332 writeln!(out, " func();");
333 writeln!(out, "}} catch (const ::std::exception &e) {{");
334 writeln!(out, " fail(e.what());");
335 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700336 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700337 }
338
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800339 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400340}
341
342fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700343 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700344 writeln!(out, "#ifndef {}", guard);
345 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400346 for line in strct.doc.to_string().lines() {
347 writeln!(out, "//{}", line);
348 }
349 writeln!(out, "struct {} final {{", strct.ident);
350 for field in &strct.fields {
351 write!(out, " ");
352 write_type_space(out, &field.ty);
353 writeln!(out, "{};", field.ident);
354 }
355 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700356 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400357}
358
359fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
360 writeln!(out, "struct {};", ident);
361}
362
David Tolnay8861bee2020-01-20 18:39:24 -0800363fn write_struct_using(out: &mut OutFile, ident: &Ident) {
364 writeln!(out, "using {} = {};", ident, ident);
365}
366
David Tolnayc1fe0052020-04-17 15:15:06 -0700367fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700368 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700369 writeln!(out, "#ifndef {}", guard);
370 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700371 for line in ety.doc.to_string().lines() {
372 writeln!(out, "//{}", line);
373 }
374 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700375 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700376 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700377 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700378 write!(out, " ");
379 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700380 let local_name = method.ident.to_string();
381 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700382 writeln!(out, ";");
383 }
384 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700385 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700386}
387
Joel Galensonc03402a2020-04-23 17:31:09 -0700388fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700389 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700390 writeln!(out, "#ifndef {}", guard);
391 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700392 for line in enm.doc.to_string().lines() {
393 writeln!(out, "//{}", line);
394 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700395 write!(out, "enum class {} : ", enm.ident);
396 write_atom(out, enm.repr);
397 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700398 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700399 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700400 }
401 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700402 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700403}
404
Joel Galenson905eb2e2020-05-04 14:58:14 -0700405fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700406 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
407 write_atom(out, enm.repr);
408 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700409 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700410 write!(out, "static_assert(static_cast<");
411 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700412 writeln!(
413 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700414 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700415 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700416 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700417 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700418}
419
Adrian Taylor405e8742020-09-25 14:01:25 -0700420fn check_trivial_extern_type(out: &mut OutFile, id: &Ident) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700421 // NOTE: The following two static assertions are just nice-to-have and not
422 // necessary for soundness. That's because triviality is always declared by
423 // the user in the form of an unsafe impl of cxx::ExternType:
424 //
425 // unsafe impl ExternType for MyType {
426 // type Id = cxx::type_id!("...");
427 // type Kind = cxx::kind::Trivial;
428 // }
429 //
430 // Since the user went on the record with their unsafe impl to unsafely
431 // claim they KNOW that the type is trivial, it's fine for that to be on
432 // them if that were wrong.
433 //
434 // There may be a legitimate reason we'll want to remove these assertions
435 // for support of types that the programmer knows are Rust-movable despite
436 // not being recognized as such by the C++ type system due to a move
437 // constructor or destructor.
438
David Tolnay7426cc12020-10-03 19:04:04 -0700439 writeln!(out, "static_assert(");
440 writeln!(
441 out,
442 " std::is_trivially_move_constructible<{}>::value,",
443 id,
444 );
445 writeln!(
446 out,
447 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
448 id,
449 );
450 writeln!(out, "static_assert(");
451 writeln!(out, " std::is_trivially_destructible<{}>::value,", id);
452 writeln!(
453 out,
454 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
455 id,
456 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700457}
458
David Tolnayebef4a22020-03-17 15:33:47 -0700459fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
460 let mut has_cxx_throws = false;
461 for api in apis {
462 if let Api::CxxFunction(efn) = api {
463 if efn.throws {
464 has_cxx_throws = true;
465 break;
466 }
467 }
468 }
469
470 if has_cxx_throws {
471 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700472 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700473 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700474 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700475 );
476 }
477}
478
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700479fn write_cxx_function_shim(
480 out: &mut OutFile,
481 efn: &ExternFn,
482 types: &Types,
483 impl_annotations: &Option<String>,
484) {
485 if !out.header {
486 if let Some(annotation) = impl_annotations {
487 write!(out, "{} ", annotation);
488 }
489 }
David Tolnayebef4a22020-03-17 15:33:47 -0700490 if efn.throws {
491 write!(out, "::rust::Str::Repr ");
492 } else {
David Tolnay99642622020-03-25 13:07:35 -0700493 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700494 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700495 let mangled = mangle::extern_fn(&out.namespace, efn);
496 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700497 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700498 if receiver.mutability.is_none() {
499 write!(out, "const ");
500 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700501 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700502 }
David Tolnay7db73692019-10-20 14:51:12 -0400503 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700504 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400505 write!(out, ", ");
506 }
David Tolnaya46a2372020-03-06 10:03:48 -0800507 if arg.ty == RustString {
508 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700509 } else if let Type::RustVec(_) = arg.ty {
510 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800511 }
David Tolnay7db73692019-10-20 14:51:12 -0400512 write_extern_arg(out, arg, types);
513 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700514 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400515 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700516 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400517 write!(out, ", ");
518 }
David Tolnay99642622020-03-25 13:07:35 -0700519 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400520 write!(out, "*return$");
521 }
522 writeln!(out, ") noexcept {{");
523 write!(out, " ");
524 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700525 match &efn.receiver {
526 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700527 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700528 }
David Tolnay7db73692019-10-20 14:51:12 -0400529 for (i, arg) in efn.args.iter().enumerate() {
530 if i > 0 {
531 write!(out, ", ");
532 }
533 write_type(out, &arg.ty);
534 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700535 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700536 if let Some(receiver) = &efn.receiver {
537 if receiver.mutability.is_none() {
538 write!(out, " const");
539 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700540 }
541 write!(out, " = ");
542 match &efn.receiver {
543 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700544 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700545 }
546 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400547 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700548 if efn.throws {
549 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700550 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700551 writeln!(out, " [&] {{");
552 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700553 }
David Tolnay7db73692019-10-20 14:51:12 -0400554 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700555 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400556 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700557 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400558 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700559 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400560 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700561 }
562 match &efn.ret {
563 Some(Type::Ref(_)) => write!(out, "&"),
564 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700565 Some(Type::SliceRefU8(_)) if !indirect_return => {
566 write!(out, "::rust::Slice<uint8_t>::Repr(")
567 }
David Tolnay99642622020-03-25 13:07:35 -0700568 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400569 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700570 match &efn.receiver {
571 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700572 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700573 }
David Tolnay7db73692019-10-20 14:51:12 -0400574 for (i, arg) in efn.args.iter().enumerate() {
575 if i > 0 {
576 write!(out, ", ");
577 }
578 if let Type::RustBox(_) = &arg.ty {
579 write_type(out, &arg.ty);
580 write!(out, "::from_raw({})", arg.ident);
581 } else if let Type::UniquePtr(_) = &arg.ty {
582 write_type(out, &arg.ty);
583 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800584 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800585 write!(
586 out,
587 "::rust::String(::rust::unsafe_bitcopy, *{})",
588 arg.ident,
589 );
David Tolnay313b10e2020-04-25 16:30:51 -0700590 } else if let Type::RustVec(_) = arg.ty {
591 write_type(out, &arg.ty);
592 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400593 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700594 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800595 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400596 } else {
597 write!(out, "{}", arg.ident);
598 }
599 }
600 write!(out, ")");
601 match &efn.ret {
602 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
603 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700604 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400605 _ => {}
606 }
607 if indirect_return {
608 write!(out, ")");
609 }
610 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700611 if efn.throws {
612 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700613 writeln!(out, " throw$.ptr = nullptr;");
614 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700615 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700616 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700617 writeln!(
618 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700619 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700620 );
David Tolnay5d121442020-03-17 22:14:40 -0700621 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700622 writeln!(out, " return throw$;");
623 }
David Tolnay7db73692019-10-20 14:51:12 -0400624 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700625 for arg in &efn.args {
626 if let Type::Fn(f) = &arg.ty {
627 let var = &arg.ident;
628 write_function_pointer_trampoline(out, efn, var, f, types);
629 }
630 }
631}
632
633fn write_function_pointer_trampoline(
634 out: &mut OutFile,
635 efn: &ExternFn,
636 var: &Ident,
637 f: &Signature,
638 types: &Types,
639) {
640 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700641 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700642 let indirect_call = true;
643 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
644
645 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700646 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700647 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400648}
649
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700650fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700651 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 let indirect_call = false;
653 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
654}
655
656fn write_rust_function_decl_impl(
657 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700658 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700659 sig: &Signature,
660 types: &Types,
661 indirect_call: bool,
662) {
663 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700664 write!(out, "::rust::Str::Repr ");
665 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700666 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700667 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700668 write!(out, "{}(", link_name);
669 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700670 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700671 if receiver.mutability.is_none() {
672 write!(out, "const ");
673 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700674 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700675 needs_comma = true;
676 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700677 for arg in &sig.args {
678 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400679 write!(out, ", ");
680 }
681 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700682 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400683 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700684 if indirect_return(sig, types) {
685 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400686 write!(out, ", ");
687 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700688 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400689 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700690 needs_comma = true;
691 }
692 if indirect_call {
693 if needs_comma {
694 write!(out, ", ");
695 }
696 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400697 }
698 writeln!(out, ") noexcept;");
699}
700
701fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400702 for line in efn.doc.to_string().lines() {
703 writeln!(out, "//{}", line);
704 }
David Tolnaya73853b2020-04-20 01:19:56 -0700705 let local_name = match &efn.sig.receiver {
706 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700707 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700708 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700709 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700710 let indirect_call = false;
711 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
712}
713
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700714fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700715 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700716 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700717 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700718 indirect_call: bool,
719) {
720 write_return_type(out, &sig.ret);
721 write!(out, "{}(", local_name);
722 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400723 if i > 0 {
724 write!(out, ", ");
725 }
726 write_type_space(out, &arg.ty);
727 write!(out, "{}", arg.ident);
728 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700729 if indirect_call {
730 if !sig.args.is_empty() {
731 write!(out, ", ");
732 }
733 write!(out, "void *extern$");
734 }
David Tolnay1e548172020-03-16 13:37:09 -0700735 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700736 if let Some(receiver) = &sig.receiver {
737 if receiver.mutability.is_none() {
738 write!(out, " const");
739 }
740 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700741 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700742 write!(out, " noexcept");
743 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700744}
745
746fn write_rust_function_shim_impl(
747 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700748 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700749 sig: &Signature,
750 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700751 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700752 indirect_call: bool,
753) {
754 if out.header && sig.receiver.is_some() {
755 // We've already defined this inside the struct.
756 return;
757 }
David Tolnaya73853b2020-04-20 01:19:56 -0700758 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400759 if out.header {
760 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700761 return;
David Tolnay7db73692019-10-20 14:51:12 -0400762 }
David Tolnay439cde22020-04-20 00:46:25 -0700763 writeln!(out, " {{");
764 for arg in &sig.args {
765 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
766 out.include.utility = true;
767 write!(out, " ::rust::ManuallyDrop<");
768 write_type(out, &arg.ty);
769 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
770 }
771 }
772 write!(out, " ");
773 let indirect_return = indirect_return(sig, types);
774 if indirect_return {
775 write!(out, "::rust::MaybeUninit<");
776 write_type(out, sig.ret.as_ref().unwrap());
777 writeln!(out, "> return$;");
778 write!(out, " ");
779 } else if let Some(ret) = &sig.ret {
780 write!(out, "return ");
781 match ret {
782 Type::RustBox(_) => {
783 write_type(out, ret);
784 write!(out, "::from_raw(");
785 }
786 Type::UniquePtr(_) => {
787 write_type(out, ret);
788 write!(out, "(");
789 }
790 Type::Ref(_) => write!(out, "*"),
791 _ => {}
792 }
793 }
794 if sig.throws {
795 write!(out, "::rust::Str::Repr error$ = ");
796 }
797 write!(out, "{}(", invoke);
798 if sig.receiver.is_some() {
799 write!(out, "*this");
800 }
801 for (i, arg) in sig.args.iter().enumerate() {
802 if i > 0 || sig.receiver.is_some() {
803 write!(out, ", ");
804 }
805 match &arg.ty {
806 Type::Str(_) => write!(out, "::rust::Str::Repr("),
807 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
808 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
809 _ => {}
810 }
811 write!(out, "{}", arg.ident);
812 match &arg.ty {
813 Type::RustBox(_) => write!(out, ".into_raw()"),
814 Type::UniquePtr(_) => write!(out, ".release()"),
815 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
816 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
817 _ => {}
818 }
819 }
820 if indirect_return {
821 if !sig.args.is_empty() {
822 write!(out, ", ");
823 }
824 write!(out, "&return$.value");
825 }
826 if indirect_call {
827 if !sig.args.is_empty() || indirect_return {
828 write!(out, ", ");
829 }
830 write!(out, "extern$");
831 }
832 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400833 if !indirect_return {
834 if let Some(ret) = &sig.ret {
835 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
836 write!(out, ")");
837 }
David Tolnay439cde22020-04-20 00:46:25 -0700838 }
839 }
840 writeln!(out, ";");
841 if sig.throws {
842 writeln!(out, " if (error$.ptr) {{");
843 writeln!(out, " throw ::rust::Error(error$);");
844 writeln!(out, " }}");
845 }
846 if indirect_return {
847 out.include.utility = true;
848 writeln!(out, " return ::std::move(return$.value);");
849 }
850 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400851}
852
853fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
854 match ty {
855 None => write!(out, "void "),
856 Some(ty) => write_type_space(out, ty),
857 }
858}
859
David Tolnay75dca2e2020-03-25 20:17:52 -0700860fn indirect_return(sig: &Signature, types: &Types) -> bool {
861 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700862 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700863 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700864}
865
David Tolnay99642622020-03-25 13:07:35 -0700866fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
867 match ty {
868 Type::RustBox(ty) | Type::UniquePtr(ty) => {
869 write_type_space(out, &ty.inner);
870 write!(out, "*");
871 }
872 Type::Ref(ty) => {
873 if ty.mutability.is_none() {
874 write!(out, "const ");
875 }
876 write_type(out, &ty.inner);
877 write!(out, " *");
878 }
879 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700880 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700881 _ => write_type(out, ty),
882 }
883}
884
885fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
886 write_indirect_return_type(out, ty);
887 match ty {
888 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700889 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700890 _ => write_space_after_type(out, ty),
891 }
892}
893
894fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400895 match ty {
896 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
897 write_type_space(out, &ty.inner);
898 write!(out, "*");
899 }
David Tolnay4a441222020-01-25 16:24:27 -0800900 Some(Type::Ref(ty)) => {
901 if ty.mutability.is_none() {
902 write!(out, "const ");
903 }
904 write_type(out, &ty.inner);
905 write!(out, " *");
906 }
David Tolnay750755e2020-03-01 13:04:08 -0800907 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700908 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400909 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
910 _ => write_return_type(out, ty),
911 }
912}
913
914fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
915 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700916 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400917 write_type_space(out, &ty.inner);
918 write!(out, "*");
919 }
David Tolnay750755e2020-03-01 13:04:08 -0800920 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700921 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400922 _ => write_type_space(out, &arg.ty),
923 }
924 if types.needs_indirect_abi(&arg.ty) {
925 write!(out, "*");
926 }
927 write!(out, "{}", arg.ident);
928}
929
930fn write_type(out: &mut OutFile, ty: &Type) {
931 match ty {
932 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700933 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400934 None => write!(out, "{}", ident),
935 },
936 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800937 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400938 write_type(out, &ty.inner);
939 write!(out, ">");
940 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700941 Type::RustVec(ty) => {
942 write!(out, "::rust::Vec<");
943 write_type(out, &ty.inner);
944 write!(out, ">");
945 }
David Tolnay7db73692019-10-20 14:51:12 -0400946 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800947 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400948 write_type(out, &ptr.inner);
949 write!(out, ">");
950 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700951 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700952 write!(out, "::std::vector<");
953 write_type(out, &ty.inner);
954 write!(out, ">");
955 }
David Tolnay7db73692019-10-20 14:51:12 -0400956 Type::Ref(r) => {
957 if r.mutability.is_none() {
958 write!(out, "const ");
959 }
960 write_type(out, &r.inner);
961 write!(out, " &");
962 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700963 Type::Slice(_) => {
964 // For now, only U8 slices are supported, which are covered separately below
965 unreachable!()
966 }
David Tolnay7db73692019-10-20 14:51:12 -0400967 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800968 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400969 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700970 Type::SliceRefU8(_) => {
971 write!(out, "::rust::Slice<uint8_t>");
972 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700973 Type::Fn(f) => {
974 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
975 match &f.ret {
976 Some(ret) => write_type(out, ret),
977 None => write!(out, "void"),
978 }
979 write!(out, "(");
980 for (i, arg) in f.args.iter().enumerate() {
981 if i > 0 {
982 write!(out, ", ");
983 }
984 write_type(out, &arg.ty);
985 }
986 write!(out, ")>");
987 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700988 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400989 }
990}
991
David Tolnayf6a89f22020-05-10 23:39:27 -0700992fn write_atom(out: &mut OutFile, atom: Atom) {
993 match atom {
994 Bool => write!(out, "bool"),
995 U8 => write!(out, "uint8_t"),
996 U16 => write!(out, "uint16_t"),
997 U32 => write!(out, "uint32_t"),
998 U64 => write!(out, "uint64_t"),
999 Usize => write!(out, "size_t"),
1000 I8 => write!(out, "int8_t"),
1001 I16 => write!(out, "int16_t"),
1002 I32 => write!(out, "int32_t"),
1003 I64 => write!(out, "int64_t"),
1004 Isize => write!(out, "::rust::isize"),
1005 F32 => write!(out, "float"),
1006 F64 => write!(out, "double"),
1007 CxxString => write!(out, "::std::string"),
1008 RustString => write!(out, "::rust::String"),
1009 }
1010}
1011
David Tolnay7db73692019-10-20 14:51:12 -04001012fn write_type_space(out: &mut OutFile, ty: &Type) {
1013 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001014 write_space_after_type(out, ty);
1015}
1016
1017fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001018 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001019 Type::Ident(_)
1020 | Type::RustBox(_)
1021 | Type::UniquePtr(_)
1022 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001023 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001024 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001025 | Type::SliceRefU8(_)
1026 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001027 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001028 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001029 }
1030}
1031
David Tolnaycd08c442020-04-25 10:16:33 -07001032// Only called for legal referent types of unique_ptr and element types of
1033// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -07001034fn to_typename(namespace: &Namespace, ty: &Type) -> String {
1035 match ty {
1036 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -07001037 let mut path = String::new();
1038 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001039 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -07001040 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001041 }
David Tolnaycd08c442020-04-25 10:16:33 -07001042 path += &ident.to_string();
1043 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001044 }
1045 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001046 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001047 }
1048}
1049
David Tolnayacdf20a2020-04-25 12:40:53 -07001050// Only called for legal referent types of unique_ptr and element types of
1051// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001052fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1053 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001054 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001055 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001056 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001057 }
1058}
1059
David Tolnay7db73692019-10-20 14:51:12 -04001060fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001061 out.begin_block("extern \"C\"");
1062 for ty in types {
1063 if let Type::RustBox(ty) = ty {
1064 if let Type::Ident(inner) = &ty.inner {
1065 out.next_section();
1066 write_rust_box_extern(out, inner);
1067 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001068 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001069 if let Type::Ident(inner) = &ty.inner {
1070 if Atom::from(inner).is_none() {
1071 out.next_section();
1072 write_rust_vec_extern(out, inner);
1073 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001074 }
David Tolnay7db73692019-10-20 14:51:12 -04001075 } else if let Type::UniquePtr(ptr) = ty {
1076 if let Type::Ident(inner) = &ptr.inner {
David Tolnay7e69f892020-10-03 22:20:22 -07001077 if Atom::from(inner).is_none()
1078 && (!types.aliases.contains_key(inner) || types.explicit_impls.contains(ty))
1079 {
David Tolnay7db73692019-10-20 14:51:12 -04001080 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001081 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001082 }
1083 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001084 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001085 if let Type::Ident(inner) = &ptr.inner {
David Tolnay7e69f892020-10-03 22:20:22 -07001086 if Atom::from(inner).is_none()
1087 && (!types.aliases.contains_key(inner) || types.explicit_impls.contains(ty))
1088 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001089 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001090 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001091 }
1092 }
1093 }
1094 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001095 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001096
David Tolnay750755e2020-03-01 13:04:08 -08001097 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001098 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001099 for ty in types {
1100 if let Type::RustBox(ty) = ty {
1101 if let Type::Ident(inner) = &ty.inner {
1102 write_rust_box_impl(out, inner);
1103 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001104 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001105 if let Type::Ident(inner) = &ty.inner {
1106 if Atom::from(inner).is_none() {
1107 write_rust_vec_impl(out, inner);
1108 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001109 }
David Tolnay7db73692019-10-20 14:51:12 -04001110 }
1111 }
David Tolnay591dcb62020-09-01 23:00:38 -07001112 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001113 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001114}
1115
1116fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1117 let mut inner = String::new();
1118 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001119 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001120 inner += "::";
1121 }
1122 inner += &ident.to_string();
1123 let instance = inner.replace("::", "$");
1124
David Tolnay591dcb62020-09-01 23:00:38 -07001125 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1126 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001127 writeln!(
1128 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001129 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001130 instance, inner,
1131 );
1132 writeln!(
1133 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001134 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001135 instance, inner,
1136 );
David Tolnay591dcb62020-09-01 23:00:38 -07001137 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001138}
1139
David Tolnay6787be62020-04-25 11:01:02 -07001140fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1141 let element = Type::Ident(element.clone());
1142 let inner = to_typename(&out.namespace, &element);
1143 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001144
David Tolnay591dcb62020-09-01 23:00:38 -07001145 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1146 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001147 writeln!(
1148 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001149 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001150 instance, inner,
1151 );
1152 writeln!(
1153 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001154 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001155 instance, inner,
1156 );
1157 writeln!(
1158 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001159 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001160 instance, inner,
1161 );
David Tolnay219c0792020-04-24 20:31:37 -07001162 writeln!(
1163 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001164 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001165 inner, instance,
1166 );
David Tolnay503d0192020-04-24 22:18:56 -07001167 writeln!(
1168 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001169 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001170 instance,
1171 );
David Tolnay591dcb62020-09-01 23:00:38 -07001172 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001173}
1174
David Tolnay7db73692019-10-20 14:51:12 -04001175fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1176 let mut inner = String::new();
1177 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001178 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001179 inner += "::";
1180 }
1181 inner += &ident.to_string();
1182 let instance = inner.replace("::", "$");
1183
1184 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001185 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001186 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001187 writeln!(out, "}}");
1188
1189 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001190 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001191 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001192 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001193}
1194
David Tolnay6787be62020-04-25 11:01:02 -07001195fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1196 let element = Type::Ident(element.clone());
1197 let inner = to_typename(&out.namespace, &element);
1198 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001199
Myron Ahneba35cf2020-02-05 19:41:51 +07001200 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001201 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001202 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001203 writeln!(out, "}}");
1204
1205 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001206 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1207 writeln!(
1208 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001209 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001210 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001211 );
1212 writeln!(out, "}}");
1213
1214 writeln!(out, "template <>");
1215 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001216 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001217 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001218
1219 writeln!(out, "template <>");
1220 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1221 writeln!(
1222 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001223 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001224 instance,
1225 );
1226 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001227
1228 writeln!(out, "template <>");
1229 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001230 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001231 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001232}
1233
David Tolnay63da4d32020-04-25 09:41:12 -07001234fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1235 let ty = Type::Ident(ident.clone());
1236 let instance = to_mangled(&out.namespace, &ty);
1237
David Tolnay591dcb62020-09-01 23:00:38 -07001238 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1239 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001240
1241 write_unique_ptr_common(out, &ty, types);
1242
David Tolnay591dcb62020-09-01 23:00:38 -07001243 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001244}
1245
1246// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1247fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001248 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001249 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001250 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001251 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001252
David Tolnay63da4d32020-04-25 09:41:12 -07001253 let can_construct_from_value = match ty {
1254 Type::Ident(ident) => types.structs.contains_key(ident),
1255 _ => false,
1256 };
1257
David Tolnay7db73692019-10-20 14:51:12 -04001258 writeln!(
1259 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001260 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001261 inner,
1262 );
1263 writeln!(
1264 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001265 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001266 inner,
1267 );
1268 writeln!(
1269 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001270 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001271 instance, inner,
1272 );
David Tolnay7e219b82020-03-01 13:14:51 -08001273 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001274 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001275 if can_construct_from_value {
1276 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001277 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001278 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001279 instance, inner, inner,
1280 );
David Tolnay63da4d32020-04-25 09:41:12 -07001281 writeln!(
1282 out,
1283 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1284 inner, inner,
1285 );
1286 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001287 }
David Tolnay7db73692019-10-20 14:51:12 -04001288 writeln!(
1289 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001290 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001291 instance, inner, inner,
1292 );
David Tolnay7e219b82020-03-01 13:14:51 -08001293 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001294 writeln!(out, "}}");
1295 writeln!(
1296 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001297 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001298 inner, instance, inner,
1299 );
1300 writeln!(out, " return ptr.get();");
1301 writeln!(out, "}}");
1302 writeln!(
1303 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001304 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001305 inner, instance, inner,
1306 );
1307 writeln!(out, " return ptr.release();");
1308 writeln!(out, "}}");
1309 writeln!(
1310 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001311 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001312 instance, inner,
1313 );
1314 writeln!(out, " ptr->~unique_ptr();");
1315 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001316}
Myron Ahneba35cf2020-02-05 19:41:51 +07001317
David Tolnay92105da2020-04-25 11:15:31 -07001318fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001319 let element = Type::Ident(element.clone());
1320 let inner = to_typename(&out.namespace, &element);
1321 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001322
David Tolnay591dcb62020-09-01 23:00:38 -07001323 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1324 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001325 writeln!(
1326 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001327 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001328 instance, inner,
1329 );
1330 writeln!(out, " return s.size();");
1331 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001332 writeln!(
1333 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001334 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001335 inner, instance, inner,
1336 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001337 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001338 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001339
1340 write_unique_ptr_common(out, vector_ty, types);
1341
David Tolnay591dcb62020-09-01 23:00:38 -07001342 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001343}