blob: ddde8a913616fd4b5e02025fd744e569d0767d26 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Joel Galensonc03402a2020-04-23 17:31:09 -07006use crate::syntax::{mangle, Api, Enum, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -07008use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04009
David Tolnay33d30292020-03-18 18:02:02 -070010pub(super) fn gen(
David Tolnay2ec14632020-05-04 00:47:10 -070011 namespace: &Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070012 apis: &[Api],
13 types: &Types,
David Tolnaya5cca312020-08-29 23:40:04 -070014 opt: &Opt,
David Tolnay33d30292020-03-18 18:02:02 -070015 header: bool,
16) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040017 let mut out_file = OutFile::new(namespace.clone(), header);
18 let out = &mut out_file;
19
David Tolnay54702b92020-07-31 11:50:09 -070020 if header {
21 writeln!(out.front, "#pragma once");
22 }
23
David Tolnaya5cca312020-08-29 23:40:04 -070024 out.include.extend(opt.include.clone());
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070027 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070035 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040036 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070045 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040046 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
David Tolnaya593d6e2020-08-29 19:48:08 -070065 if !types.cxx.contains(&strct.ident) {
66 write_struct(out, strct);
67 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070068 }
Joel Galensonc03402a2020-04-23 17:31:09 -070069 Api::Enum(enm) => {
70 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070071 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070072 check_enum(out, enm);
73 } else {
74 write_enum(out, enm);
75 }
Joel Galensonc03402a2020-04-23 17:31:09 -070076 }
David Tolnayc1fe0052020-04-17 15:15:06 -070077 Api::RustType(ety) => {
78 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070079 out.next_section();
80 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070081 }
David Tolnayc1fe0052020-04-17 15:15:06 -070082 }
Adrian Taylor405e8742020-09-25 14:01:25 -070083 Api::TypeAlias(ety) => {
84 if types.required_trivial_aliases.contains(&ety.ident) {
85 check_trivial_extern_type(out, &ety.ident)
86 }
87 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070088 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040089 }
90 }
91
92 if !header {
93 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070094 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040095 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070096 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040097 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
98 Api::RustFunction(efn) => (efn, write_rust_function_decl),
99 _ => continue,
100 };
101 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700102 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400103 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800104 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400105 }
106
107 for api in apis {
108 if let Api::RustFunction(efn) = api {
109 out.next_section();
110 write_rust_function_shim(out, efn, types);
111 }
112 }
113
114 out.next_section();
115 for name in namespace.iter().rev() {
116 writeln!(out, "}} // namespace {}", name);
117 }
118
119 if !header {
120 out.next_section();
121 write_generic_instantiations(out, types);
122 }
123
David Tolnay54702b92020-07-31 11:50:09 -0700124 write!(out.front, "{}", out.include);
125
126 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400127}
128
129fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400130 for ty in types {
131 match ty {
David Tolnay89e386d2020-10-03 19:02:19 -0700132 Type::Ident(ident) => match Atom::from(ident) {
133 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
134 | Some(I64) => out.include.cstdint = true,
135 Some(Usize) => out.include.cstddef = true,
136 Some(CxxString) => out.include.string = true,
137 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) => {}
138 None => {
139 if types.required_trivial_aliases.contains(&ident) {
140 out.include.type_traits = true;
141 }
142 }
143 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800144 Type::RustBox(_) => out.include.type_traits = true,
145 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700146 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700147 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700148 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400149 }
150 }
David Tolnay7db73692019-10-20 14:51:12 -0400151}
152
David Tolnayf51447e2020-03-06 14:14:27 -0800153fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay16ab1462020-09-02 15:10:09 -0700154 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700155 let mut needs_rust_string = false;
156 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700157 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400158 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700159 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700160 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700161 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700162 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400163 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700164 match ty {
165 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700166 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700167 out.include.type_traits = true;
168 needs_rust_box = true;
169 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700170 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700171 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700172 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700173 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700174 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700175 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700176 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700177 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 Type::Str(_) => {
179 out.include.cstdint = true;
180 out.include.string = true;
181 needs_rust_str = true;
182 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700183 Type::Fn(_) => {
184 needs_rust_fn = true;
185 }
David Tolnay4770b472020-04-14 16:32:59 -0700186 Type::Slice(_) | Type::SliceRefU8(_) => {
187 needs_rust_slice = true;
188 }
David Tolnay7c295462020-04-25 12:45:07 -0700189 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400190 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700191 needs_rust_isize = true;
192 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 ty if ty == RustString => {
194 out.include.array = true;
195 out.include.cstdint = true;
196 out.include.string = true;
197 needs_rust_string = true;
198 }
199 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400200 }
201 }
202
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800204 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800205 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700206 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800207 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700208 match api {
209 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700210 if efn.throws {
211 needs_trycatch = true;
212 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700214 let bitcopy = match arg.ty {
215 Type::RustVec(_) => true,
216 _ => arg.ty == RustString,
217 };
218 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 needs_unsafe_bitcopy = true;
220 break;
221 }
David Tolnay09011c32020-03-06 14:40:28 -0800222 }
223 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700224 Api::RustFunction(efn) if !out.header => {
225 if efn.throws {
226 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700227 out.include.string = true;
228 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700229 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200230 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 }
232 for arg in &efn.args {
233 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
234 needs_manually_drop = true;
235 break;
236 }
237 }
238 if let Some(ret) = &efn.ret {
239 if types.needs_indirect_abi(ret) {
240 needs_maybe_uninit = true;
241 }
David Tolnayf51447e2020-03-06 14:14:27 -0800242 }
243 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700244 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800245 }
246 }
247
David Tolnay750755e2020-03-01 13:04:08 -0800248 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700249 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800250
David Tolnay16ab1462020-09-02 15:10:09 -0700251 if needs_panic
252 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700253 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700254 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700255 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700256 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700257 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700258 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700259 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700260 || needs_unsafe_bitcopy
261 || needs_manually_drop
262 || needs_maybe_uninit
263 {
David Tolnay736cbca2020-03-11 16:49:18 -0700264 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800265 }
266
David Tolnay16ab1462020-09-02 15:10:09 -0700267 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
268
David Tolnay99a95e62020-09-02 15:05:53 -0700269 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700270 out.next_section();
271 writeln!(out, "struct unsafe_bitcopy_t;");
272 }
273
David Tolnay591dcb62020-09-01 23:00:38 -0700274 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
275 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
276 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
277 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700278 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700279 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
280 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
281 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
282 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800283
284 if needs_manually_drop {
285 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700286 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800287 writeln!(out, "template <typename T>");
288 writeln!(out, "union ManuallyDrop {{");
289 writeln!(out, " T value;");
290 writeln!(
291 out,
292 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
293 );
294 writeln!(out, " ~ManuallyDrop() {{}}");
295 writeln!(out, "}};");
296 }
297
David Tolnay09011c32020-03-06 14:40:28 -0800298 if needs_maybe_uninit {
299 out.next_section();
300 writeln!(out, "template <typename T>");
301 writeln!(out, "union MaybeUninit {{");
302 writeln!(out, " T value;");
303 writeln!(out, " MaybeUninit() {{}}");
304 writeln!(out, " ~MaybeUninit() {{}}");
305 writeln!(out, "}};");
306 }
307
David Tolnay591dcb62020-09-01 23:00:38 -0700308 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700309
David Tolnay5d121442020-03-17 22:14:40 -0700310 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700311 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700312 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700313 out.include.type_traits = true;
314 out.include.utility = true;
315 writeln!(out, "class missing {{}};");
316 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700317 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700318 writeln!(out, "template <typename Try, typename Fail>");
319 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700320 writeln!(
321 out,
David Tolnay04722332020-03-18 11:31:54 -0700322 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700323 );
David Tolnay04722332020-03-18 11:31:54 -0700324 writeln!(out, " missing>::value>::type");
325 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700326 writeln!(out, " func();");
327 writeln!(out, "}} catch (const ::std::exception &e) {{");
328 writeln!(out, " fail(e.what());");
329 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700330 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700331 }
332
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800333 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400334}
335
336fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700337 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700338 writeln!(out, "#ifndef {}", guard);
339 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400340 for line in strct.doc.to_string().lines() {
341 writeln!(out, "//{}", line);
342 }
343 writeln!(out, "struct {} final {{", strct.ident);
344 for field in &strct.fields {
345 write!(out, " ");
346 write_type_space(out, &field.ty);
347 writeln!(out, "{};", field.ident);
348 }
349 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700350 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400351}
352
353fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
354 writeln!(out, "struct {};", ident);
355}
356
David Tolnay8861bee2020-01-20 18:39:24 -0800357fn write_struct_using(out: &mut OutFile, ident: &Ident) {
358 writeln!(out, "using {} = {};", ident, ident);
359}
360
David Tolnayc1fe0052020-04-17 15:15:06 -0700361fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700362 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700363 writeln!(out, "#ifndef {}", guard);
364 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700365 for line in ety.doc.to_string().lines() {
366 writeln!(out, "//{}", line);
367 }
368 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700369 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700370 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700371 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700372 write!(out, " ");
373 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700374 let local_name = method.ident.to_string();
375 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700376 writeln!(out, ";");
377 }
378 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700379 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700380}
381
Joel Galensonc03402a2020-04-23 17:31:09 -0700382fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700383 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700384 writeln!(out, "#ifndef {}", guard);
385 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700386 for line in enm.doc.to_string().lines() {
387 writeln!(out, "//{}", line);
388 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700389 write!(out, "enum class {} : ", enm.ident);
390 write_atom(out, enm.repr);
391 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700392 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700393 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700394 }
395 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700396 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700397}
398
Joel Galenson905eb2e2020-05-04 14:58:14 -0700399fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700400 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
401 write_atom(out, enm.repr);
402 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700403 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700404 write!(out, "static_assert(static_cast<");
405 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700406 writeln!(
407 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700408 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700409 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700410 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700411 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700412}
413
Adrian Taylor405e8742020-09-25 14:01:25 -0700414fn check_trivial_extern_type(out: &mut OutFile, id: &Ident) {
David Tolnay7426cc12020-10-03 19:04:04 -0700415 writeln!(out, "static_assert(");
416 writeln!(
417 out,
418 " std::is_trivially_move_constructible<{}>::value,",
419 id,
420 );
421 writeln!(
422 out,
423 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
424 id,
425 );
426 writeln!(out, "static_assert(");
427 writeln!(out, " std::is_trivially_destructible<{}>::value,", id);
428 writeln!(
429 out,
430 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
431 id,
432 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700433}
434
David Tolnayebef4a22020-03-17 15:33:47 -0700435fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
436 let mut has_cxx_throws = false;
437 for api in apis {
438 if let Api::CxxFunction(efn) = api {
439 if efn.throws {
440 has_cxx_throws = true;
441 break;
442 }
443 }
444 }
445
446 if has_cxx_throws {
447 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700448 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700449 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700450 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700451 );
452 }
453}
454
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700455fn write_cxx_function_shim(
456 out: &mut OutFile,
457 efn: &ExternFn,
458 types: &Types,
459 impl_annotations: &Option<String>,
460) {
461 if !out.header {
462 if let Some(annotation) = impl_annotations {
463 write!(out, "{} ", annotation);
464 }
465 }
David Tolnayebef4a22020-03-17 15:33:47 -0700466 if efn.throws {
467 write!(out, "::rust::Str::Repr ");
468 } else {
David Tolnay99642622020-03-25 13:07:35 -0700469 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700470 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700471 let mangled = mangle::extern_fn(&out.namespace, efn);
472 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700473 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700474 if receiver.mutability.is_none() {
475 write!(out, "const ");
476 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700477 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700478 }
David Tolnay7db73692019-10-20 14:51:12 -0400479 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700480 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400481 write!(out, ", ");
482 }
David Tolnaya46a2372020-03-06 10:03:48 -0800483 if arg.ty == RustString {
484 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700485 } else if let Type::RustVec(_) = arg.ty {
486 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800487 }
David Tolnay7db73692019-10-20 14:51:12 -0400488 write_extern_arg(out, arg, types);
489 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700490 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400491 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700492 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400493 write!(out, ", ");
494 }
David Tolnay99642622020-03-25 13:07:35 -0700495 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400496 write!(out, "*return$");
497 }
498 writeln!(out, ") noexcept {{");
499 write!(out, " ");
500 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700501 match &efn.receiver {
502 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700503 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700504 }
David Tolnay7db73692019-10-20 14:51:12 -0400505 for (i, arg) in efn.args.iter().enumerate() {
506 if i > 0 {
507 write!(out, ", ");
508 }
509 write_type(out, &arg.ty);
510 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700511 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700512 if let Some(receiver) = &efn.receiver {
513 if receiver.mutability.is_none() {
514 write!(out, " const");
515 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700516 }
517 write!(out, " = ");
518 match &efn.receiver {
519 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700520 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700521 }
522 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400523 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700524 if efn.throws {
525 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700526 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700527 writeln!(out, " [&] {{");
528 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700529 }
David Tolnay7db73692019-10-20 14:51:12 -0400530 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700531 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400532 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700533 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400534 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700535 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400536 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700537 }
538 match &efn.ret {
539 Some(Type::Ref(_)) => write!(out, "&"),
540 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700541 Some(Type::SliceRefU8(_)) if !indirect_return => {
542 write!(out, "::rust::Slice<uint8_t>::Repr(")
543 }
David Tolnay99642622020-03-25 13:07:35 -0700544 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400545 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700546 match &efn.receiver {
547 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700548 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700549 }
David Tolnay7db73692019-10-20 14:51:12 -0400550 for (i, arg) in efn.args.iter().enumerate() {
551 if i > 0 {
552 write!(out, ", ");
553 }
554 if let Type::RustBox(_) = &arg.ty {
555 write_type(out, &arg.ty);
556 write!(out, "::from_raw({})", arg.ident);
557 } else if let Type::UniquePtr(_) = &arg.ty {
558 write_type(out, &arg.ty);
559 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800560 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800561 write!(
562 out,
563 "::rust::String(::rust::unsafe_bitcopy, *{})",
564 arg.ident,
565 );
David Tolnay313b10e2020-04-25 16:30:51 -0700566 } else if let Type::RustVec(_) = arg.ty {
567 write_type(out, &arg.ty);
568 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400569 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700570 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800571 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400572 } else {
573 write!(out, "{}", arg.ident);
574 }
575 }
576 write!(out, ")");
577 match &efn.ret {
578 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
579 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700580 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400581 _ => {}
582 }
583 if indirect_return {
584 write!(out, ")");
585 }
586 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700587 if efn.throws {
588 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700589 writeln!(out, " throw$.ptr = nullptr;");
590 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700591 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700592 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700593 writeln!(
594 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700595 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700596 );
David Tolnay5d121442020-03-17 22:14:40 -0700597 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700598 writeln!(out, " return throw$;");
599 }
David Tolnay7db73692019-10-20 14:51:12 -0400600 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700601 for arg in &efn.args {
602 if let Type::Fn(f) = &arg.ty {
603 let var = &arg.ident;
604 write_function_pointer_trampoline(out, efn, var, f, types);
605 }
606 }
607}
608
609fn write_function_pointer_trampoline(
610 out: &mut OutFile,
611 efn: &ExternFn,
612 var: &Ident,
613 f: &Signature,
614 types: &Types,
615) {
616 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700617 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700618 let indirect_call = true;
619 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
620
621 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700622 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700623 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400624}
625
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700626fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700627 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 let indirect_call = false;
629 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
630}
631
632fn write_rust_function_decl_impl(
633 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700634 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700635 sig: &Signature,
636 types: &Types,
637 indirect_call: bool,
638) {
639 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700640 write!(out, "::rust::Str::Repr ");
641 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700642 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700643 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 write!(out, "{}(", link_name);
645 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700646 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700647 if receiver.mutability.is_none() {
648 write!(out, "const ");
649 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700650 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700651 needs_comma = true;
652 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 for arg in &sig.args {
654 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400655 write!(out, ", ");
656 }
657 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700658 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400659 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700660 if indirect_return(sig, types) {
661 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400662 write!(out, ", ");
663 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700664 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400665 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700666 needs_comma = true;
667 }
668 if indirect_call {
669 if needs_comma {
670 write!(out, ", ");
671 }
672 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400673 }
674 writeln!(out, ") noexcept;");
675}
676
677fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400678 for line in efn.doc.to_string().lines() {
679 writeln!(out, "//{}", line);
680 }
David Tolnaya73853b2020-04-20 01:19:56 -0700681 let local_name = match &efn.sig.receiver {
682 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700683 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700684 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700685 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700686 let indirect_call = false;
687 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
688}
689
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700690fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700691 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700692 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700693 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700694 indirect_call: bool,
695) {
696 write_return_type(out, &sig.ret);
697 write!(out, "{}(", local_name);
698 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400699 if i > 0 {
700 write!(out, ", ");
701 }
702 write_type_space(out, &arg.ty);
703 write!(out, "{}", arg.ident);
704 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700705 if indirect_call {
706 if !sig.args.is_empty() {
707 write!(out, ", ");
708 }
709 write!(out, "void *extern$");
710 }
David Tolnay1e548172020-03-16 13:37:09 -0700711 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700712 if let Some(receiver) = &sig.receiver {
713 if receiver.mutability.is_none() {
714 write!(out, " const");
715 }
716 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700717 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700718 write!(out, " noexcept");
719 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700720}
721
722fn write_rust_function_shim_impl(
723 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700724 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700725 sig: &Signature,
726 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700727 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700728 indirect_call: bool,
729) {
730 if out.header && sig.receiver.is_some() {
731 // We've already defined this inside the struct.
732 return;
733 }
David Tolnaya73853b2020-04-20 01:19:56 -0700734 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400735 if out.header {
736 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700737 return;
David Tolnay7db73692019-10-20 14:51:12 -0400738 }
David Tolnay439cde22020-04-20 00:46:25 -0700739 writeln!(out, " {{");
740 for arg in &sig.args {
741 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
742 out.include.utility = true;
743 write!(out, " ::rust::ManuallyDrop<");
744 write_type(out, &arg.ty);
745 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
746 }
747 }
748 write!(out, " ");
749 let indirect_return = indirect_return(sig, types);
750 if indirect_return {
751 write!(out, "::rust::MaybeUninit<");
752 write_type(out, sig.ret.as_ref().unwrap());
753 writeln!(out, "> return$;");
754 write!(out, " ");
755 } else if let Some(ret) = &sig.ret {
756 write!(out, "return ");
757 match ret {
758 Type::RustBox(_) => {
759 write_type(out, ret);
760 write!(out, "::from_raw(");
761 }
762 Type::UniquePtr(_) => {
763 write_type(out, ret);
764 write!(out, "(");
765 }
766 Type::Ref(_) => write!(out, "*"),
767 _ => {}
768 }
769 }
770 if sig.throws {
771 write!(out, "::rust::Str::Repr error$ = ");
772 }
773 write!(out, "{}(", invoke);
774 if sig.receiver.is_some() {
775 write!(out, "*this");
776 }
777 for (i, arg) in sig.args.iter().enumerate() {
778 if i > 0 || sig.receiver.is_some() {
779 write!(out, ", ");
780 }
781 match &arg.ty {
782 Type::Str(_) => write!(out, "::rust::Str::Repr("),
783 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
784 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
785 _ => {}
786 }
787 write!(out, "{}", arg.ident);
788 match &arg.ty {
789 Type::RustBox(_) => write!(out, ".into_raw()"),
790 Type::UniquePtr(_) => write!(out, ".release()"),
791 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
792 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
793 _ => {}
794 }
795 }
796 if indirect_return {
797 if !sig.args.is_empty() {
798 write!(out, ", ");
799 }
800 write!(out, "&return$.value");
801 }
802 if indirect_call {
803 if !sig.args.is_empty() || indirect_return {
804 write!(out, ", ");
805 }
806 write!(out, "extern$");
807 }
808 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400809 if !indirect_return {
810 if let Some(ret) = &sig.ret {
811 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
812 write!(out, ")");
813 }
David Tolnay439cde22020-04-20 00:46:25 -0700814 }
815 }
816 writeln!(out, ";");
817 if sig.throws {
818 writeln!(out, " if (error$.ptr) {{");
819 writeln!(out, " throw ::rust::Error(error$);");
820 writeln!(out, " }}");
821 }
822 if indirect_return {
823 out.include.utility = true;
824 writeln!(out, " return ::std::move(return$.value);");
825 }
826 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400827}
828
829fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
830 match ty {
831 None => write!(out, "void "),
832 Some(ty) => write_type_space(out, ty),
833 }
834}
835
David Tolnay75dca2e2020-03-25 20:17:52 -0700836fn indirect_return(sig: &Signature, types: &Types) -> bool {
837 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700838 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700839 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700840}
841
David Tolnay99642622020-03-25 13:07:35 -0700842fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
843 match ty {
844 Type::RustBox(ty) | Type::UniquePtr(ty) => {
845 write_type_space(out, &ty.inner);
846 write!(out, "*");
847 }
848 Type::Ref(ty) => {
849 if ty.mutability.is_none() {
850 write!(out, "const ");
851 }
852 write_type(out, &ty.inner);
853 write!(out, " *");
854 }
855 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700856 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700857 _ => write_type(out, ty),
858 }
859}
860
861fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
862 write_indirect_return_type(out, ty);
863 match ty {
864 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700865 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700866 _ => write_space_after_type(out, ty),
867 }
868}
869
870fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400871 match ty {
872 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
873 write_type_space(out, &ty.inner);
874 write!(out, "*");
875 }
David Tolnay4a441222020-01-25 16:24:27 -0800876 Some(Type::Ref(ty)) => {
877 if ty.mutability.is_none() {
878 write!(out, "const ");
879 }
880 write_type(out, &ty.inner);
881 write!(out, " *");
882 }
David Tolnay750755e2020-03-01 13:04:08 -0800883 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700884 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400885 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
886 _ => write_return_type(out, ty),
887 }
888}
889
890fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
891 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700892 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400893 write_type_space(out, &ty.inner);
894 write!(out, "*");
895 }
David Tolnay750755e2020-03-01 13:04:08 -0800896 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700897 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400898 _ => write_type_space(out, &arg.ty),
899 }
900 if types.needs_indirect_abi(&arg.ty) {
901 write!(out, "*");
902 }
903 write!(out, "{}", arg.ident);
904}
905
906fn write_type(out: &mut OutFile, ty: &Type) {
907 match ty {
908 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700909 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400910 None => write!(out, "{}", ident),
911 },
912 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800913 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400914 write_type(out, &ty.inner);
915 write!(out, ">");
916 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700917 Type::RustVec(ty) => {
918 write!(out, "::rust::Vec<");
919 write_type(out, &ty.inner);
920 write!(out, ">");
921 }
David Tolnay7db73692019-10-20 14:51:12 -0400922 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800923 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400924 write_type(out, &ptr.inner);
925 write!(out, ">");
926 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700927 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700928 write!(out, "::std::vector<");
929 write_type(out, &ty.inner);
930 write!(out, ">");
931 }
David Tolnay7db73692019-10-20 14:51:12 -0400932 Type::Ref(r) => {
933 if r.mutability.is_none() {
934 write!(out, "const ");
935 }
936 write_type(out, &r.inner);
937 write!(out, " &");
938 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700939 Type::Slice(_) => {
940 // For now, only U8 slices are supported, which are covered separately below
941 unreachable!()
942 }
David Tolnay7db73692019-10-20 14:51:12 -0400943 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800944 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400945 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700946 Type::SliceRefU8(_) => {
947 write!(out, "::rust::Slice<uint8_t>");
948 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700949 Type::Fn(f) => {
950 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
951 match &f.ret {
952 Some(ret) => write_type(out, ret),
953 None => write!(out, "void"),
954 }
955 write!(out, "(");
956 for (i, arg) in f.args.iter().enumerate() {
957 if i > 0 {
958 write!(out, ", ");
959 }
960 write_type(out, &arg.ty);
961 }
962 write!(out, ")>");
963 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700964 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400965 }
966}
967
David Tolnayf6a89f22020-05-10 23:39:27 -0700968fn write_atom(out: &mut OutFile, atom: Atom) {
969 match atom {
970 Bool => write!(out, "bool"),
971 U8 => write!(out, "uint8_t"),
972 U16 => write!(out, "uint16_t"),
973 U32 => write!(out, "uint32_t"),
974 U64 => write!(out, "uint64_t"),
975 Usize => write!(out, "size_t"),
976 I8 => write!(out, "int8_t"),
977 I16 => write!(out, "int16_t"),
978 I32 => write!(out, "int32_t"),
979 I64 => write!(out, "int64_t"),
980 Isize => write!(out, "::rust::isize"),
981 F32 => write!(out, "float"),
982 F64 => write!(out, "double"),
983 CxxString => write!(out, "::std::string"),
984 RustString => write!(out, "::rust::String"),
985 }
986}
987
David Tolnay7db73692019-10-20 14:51:12 -0400988fn write_type_space(out: &mut OutFile, ty: &Type) {
989 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700990 write_space_after_type(out, ty);
991}
992
993fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400994 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700995 Type::Ident(_)
996 | Type::RustBox(_)
997 | Type::UniquePtr(_)
998 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700999 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001000 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001001 | Type::SliceRefU8(_)
1002 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001003 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001004 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001005 }
1006}
1007
David Tolnaycd08c442020-04-25 10:16:33 -07001008// Only called for legal referent types of unique_ptr and element types of
1009// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -07001010fn to_typename(namespace: &Namespace, ty: &Type) -> String {
1011 match ty {
1012 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -07001013 let mut path = String::new();
1014 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001015 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -07001016 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001017 }
David Tolnaycd08c442020-04-25 10:16:33 -07001018 path += &ident.to_string();
1019 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001020 }
1021 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001022 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001023 }
1024}
1025
David Tolnayacdf20a2020-04-25 12:40:53 -07001026// Only called for legal referent types of unique_ptr and element types of
1027// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001028fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1029 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001030 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001031 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001032 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001033 }
1034}
1035
David Tolnay7db73692019-10-20 14:51:12 -04001036fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001037 out.begin_block("extern \"C\"");
1038 for ty in types {
1039 if let Type::RustBox(ty) = ty {
1040 if let Type::Ident(inner) = &ty.inner {
1041 out.next_section();
1042 write_rust_box_extern(out, inner);
1043 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001044 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001045 if let Type::Ident(inner) = &ty.inner {
1046 if Atom::from(inner).is_none() {
1047 out.next_section();
1048 write_rust_vec_extern(out, inner);
1049 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001050 }
David Tolnay7db73692019-10-20 14:51:12 -04001051 } else if let Type::UniquePtr(ptr) = ty {
1052 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001053 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001054 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001055 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001056 }
1057 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001058 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001059 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001060 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001061 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001062 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001063 }
1064 }
1065 }
1066 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001067 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001068
David Tolnay750755e2020-03-01 13:04:08 -08001069 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001070 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001071 for ty in types {
1072 if let Type::RustBox(ty) = ty {
1073 if let Type::Ident(inner) = &ty.inner {
1074 write_rust_box_impl(out, inner);
1075 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001076 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001077 if let Type::Ident(inner) = &ty.inner {
1078 if Atom::from(inner).is_none() {
1079 write_rust_vec_impl(out, inner);
1080 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001081 }
David Tolnay7db73692019-10-20 14:51:12 -04001082 }
1083 }
David Tolnay591dcb62020-09-01 23:00:38 -07001084 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001085 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001086}
1087
1088fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1089 let mut inner = String::new();
1090 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001091 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001092 inner += "::";
1093 }
1094 inner += &ident.to_string();
1095 let instance = inner.replace("::", "$");
1096
David Tolnay591dcb62020-09-01 23:00:38 -07001097 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1098 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001099 writeln!(
1100 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001101 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001102 instance, inner,
1103 );
1104 writeln!(
1105 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001106 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001107 instance, inner,
1108 );
David Tolnay591dcb62020-09-01 23:00:38 -07001109 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001110}
1111
David Tolnay6787be62020-04-25 11:01:02 -07001112fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1113 let element = Type::Ident(element.clone());
1114 let inner = to_typename(&out.namespace, &element);
1115 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001116
David Tolnay591dcb62020-09-01 23:00:38 -07001117 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1118 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001119 writeln!(
1120 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001121 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001122 instance, inner,
1123 );
1124 writeln!(
1125 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001126 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001127 instance, inner,
1128 );
1129 writeln!(
1130 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001131 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001132 instance, inner,
1133 );
David Tolnay219c0792020-04-24 20:31:37 -07001134 writeln!(
1135 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001136 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001137 inner, instance,
1138 );
David Tolnay503d0192020-04-24 22:18:56 -07001139 writeln!(
1140 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001141 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001142 instance,
1143 );
David Tolnay591dcb62020-09-01 23:00:38 -07001144 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001145}
1146
David Tolnay7db73692019-10-20 14:51:12 -04001147fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1148 let mut inner = String::new();
1149 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001150 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001151 inner += "::";
1152 }
1153 inner += &ident.to_string();
1154 let instance = inner.replace("::", "$");
1155
1156 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001157 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001158 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001159 writeln!(out, "}}");
1160
1161 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001162 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001163 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001164 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001165}
1166
David Tolnay6787be62020-04-25 11:01:02 -07001167fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1168 let element = Type::Ident(element.clone());
1169 let inner = to_typename(&out.namespace, &element);
1170 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001171
Myron Ahneba35cf2020-02-05 19:41:51 +07001172 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001173 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001174 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001175 writeln!(out, "}}");
1176
1177 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001178 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1179 writeln!(
1180 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001181 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001182 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001183 );
1184 writeln!(out, "}}");
1185
1186 writeln!(out, "template <>");
1187 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001188 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001189 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001190
1191 writeln!(out, "template <>");
1192 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1193 writeln!(
1194 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001195 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001196 instance,
1197 );
1198 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001199
1200 writeln!(out, "template <>");
1201 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001202 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001203 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001204}
1205
David Tolnay63da4d32020-04-25 09:41:12 -07001206fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1207 let ty = Type::Ident(ident.clone());
1208 let instance = to_mangled(&out.namespace, &ty);
1209
David Tolnay591dcb62020-09-01 23:00:38 -07001210 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1211 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001212
1213 write_unique_ptr_common(out, &ty, types);
1214
David Tolnay591dcb62020-09-01 23:00:38 -07001215 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001216}
1217
1218// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1219fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001220 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001221 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001222 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001223 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001224
David Tolnay63da4d32020-04-25 09:41:12 -07001225 let can_construct_from_value = match ty {
1226 Type::Ident(ident) => types.structs.contains_key(ident),
1227 _ => false,
1228 };
1229
David Tolnay7db73692019-10-20 14:51:12 -04001230 writeln!(
1231 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001232 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001233 inner,
1234 );
1235 writeln!(
1236 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001237 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001238 inner,
1239 );
1240 writeln!(
1241 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001242 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001243 instance, inner,
1244 );
David Tolnay7e219b82020-03-01 13:14:51 -08001245 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001246 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001247 if can_construct_from_value {
1248 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001249 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001250 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001251 instance, inner, inner,
1252 );
David Tolnay63da4d32020-04-25 09:41:12 -07001253 writeln!(
1254 out,
1255 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1256 inner, inner,
1257 );
1258 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001259 }
David Tolnay7db73692019-10-20 14:51:12 -04001260 writeln!(
1261 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001262 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001263 instance, inner, inner,
1264 );
David Tolnay7e219b82020-03-01 13:14:51 -08001265 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001266 writeln!(out, "}}");
1267 writeln!(
1268 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001269 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001270 inner, instance, inner,
1271 );
1272 writeln!(out, " return ptr.get();");
1273 writeln!(out, "}}");
1274 writeln!(
1275 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001276 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001277 inner, instance, inner,
1278 );
1279 writeln!(out, " return ptr.release();");
1280 writeln!(out, "}}");
1281 writeln!(
1282 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001283 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001284 instance, inner,
1285 );
1286 writeln!(out, " ptr->~unique_ptr();");
1287 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001288}
Myron Ahneba35cf2020-02-05 19:41:51 +07001289
David Tolnay92105da2020-04-25 11:15:31 -07001290fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001291 let element = Type::Ident(element.clone());
1292 let inner = to_typename(&out.namespace, &element);
1293 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001294
David Tolnay591dcb62020-09-01 23:00:38 -07001295 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1296 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001297 writeln!(
1298 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001299 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001300 instance, inner,
1301 );
1302 writeln!(out, " return s.size();");
1303 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001304 writeln!(
1305 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001306 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001307 inner, instance, inner,
1308 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001309 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001310 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001311
1312 write_unique_ptr_common(out, vector_ty, types);
1313
David Tolnay591dcb62020-09-01 23:00:38 -07001314 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001315}