blob: e9e22393a8f1aec7cc792b40a7b07694cc2ef01c [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) {
415 writeln!(out, "static_assert(std::is_trivially_move_constructible<{}>::value,\"type {} marked as Trivial in Rust is not trivially move constructible in C++\");", id, id);
416 writeln!(out, "static_assert(std::is_trivially_destructible<{}>::value,\"type {} marked as Trivial in Rust is not trivially destructible in C++\");", id, id);
417}
418
David Tolnayebef4a22020-03-17 15:33:47 -0700419fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
420 let mut has_cxx_throws = false;
421 for api in apis {
422 if let Api::CxxFunction(efn) = api {
423 if efn.throws {
424 has_cxx_throws = true;
425 break;
426 }
427 }
428 }
429
430 if has_cxx_throws {
431 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700432 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700433 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700434 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700435 );
436 }
437}
438
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700439fn write_cxx_function_shim(
440 out: &mut OutFile,
441 efn: &ExternFn,
442 types: &Types,
443 impl_annotations: &Option<String>,
444) {
445 if !out.header {
446 if let Some(annotation) = impl_annotations {
447 write!(out, "{} ", annotation);
448 }
449 }
David Tolnayebef4a22020-03-17 15:33:47 -0700450 if efn.throws {
451 write!(out, "::rust::Str::Repr ");
452 } else {
David Tolnay99642622020-03-25 13:07:35 -0700453 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700454 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700455 let mangled = mangle::extern_fn(&out.namespace, efn);
456 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700457 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700458 if receiver.mutability.is_none() {
459 write!(out, "const ");
460 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700461 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700462 }
David Tolnay7db73692019-10-20 14:51:12 -0400463 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700464 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400465 write!(out, ", ");
466 }
David Tolnaya46a2372020-03-06 10:03:48 -0800467 if arg.ty == RustString {
468 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700469 } else if let Type::RustVec(_) = arg.ty {
470 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800471 }
David Tolnay7db73692019-10-20 14:51:12 -0400472 write_extern_arg(out, arg, types);
473 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700474 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400475 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700476 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400477 write!(out, ", ");
478 }
David Tolnay99642622020-03-25 13:07:35 -0700479 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400480 write!(out, "*return$");
481 }
482 writeln!(out, ") noexcept {{");
483 write!(out, " ");
484 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700485 match &efn.receiver {
486 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700487 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700488 }
David Tolnay7db73692019-10-20 14:51:12 -0400489 for (i, arg) in efn.args.iter().enumerate() {
490 if i > 0 {
491 write!(out, ", ");
492 }
493 write_type(out, &arg.ty);
494 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700495 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700496 if let Some(receiver) = &efn.receiver {
497 if receiver.mutability.is_none() {
498 write!(out, " const");
499 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700500 }
501 write!(out, " = ");
502 match &efn.receiver {
503 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700504 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700505 }
506 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400507 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700508 if efn.throws {
509 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700510 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700511 writeln!(out, " [&] {{");
512 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700513 }
David Tolnay7db73692019-10-20 14:51:12 -0400514 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700515 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400516 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700517 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400518 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700519 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400520 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700521 }
522 match &efn.ret {
523 Some(Type::Ref(_)) => write!(out, "&"),
524 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700525 Some(Type::SliceRefU8(_)) if !indirect_return => {
526 write!(out, "::rust::Slice<uint8_t>::Repr(")
527 }
David Tolnay99642622020-03-25 13:07:35 -0700528 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400529 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700530 match &efn.receiver {
531 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700532 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700533 }
David Tolnay7db73692019-10-20 14:51:12 -0400534 for (i, arg) in efn.args.iter().enumerate() {
535 if i > 0 {
536 write!(out, ", ");
537 }
538 if let Type::RustBox(_) = &arg.ty {
539 write_type(out, &arg.ty);
540 write!(out, "::from_raw({})", arg.ident);
541 } else if let Type::UniquePtr(_) = &arg.ty {
542 write_type(out, &arg.ty);
543 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800544 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800545 write!(
546 out,
547 "::rust::String(::rust::unsafe_bitcopy, *{})",
548 arg.ident,
549 );
David Tolnay313b10e2020-04-25 16:30:51 -0700550 } else if let Type::RustVec(_) = arg.ty {
551 write_type(out, &arg.ty);
552 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400553 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700554 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800555 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400556 } else {
557 write!(out, "{}", arg.ident);
558 }
559 }
560 write!(out, ")");
561 match &efn.ret {
562 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
563 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700564 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400565 _ => {}
566 }
567 if indirect_return {
568 write!(out, ")");
569 }
570 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700571 if efn.throws {
572 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700573 writeln!(out, " throw$.ptr = nullptr;");
574 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700575 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700576 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700577 writeln!(
578 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700579 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700580 );
David Tolnay5d121442020-03-17 22:14:40 -0700581 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700582 writeln!(out, " return throw$;");
583 }
David Tolnay7db73692019-10-20 14:51:12 -0400584 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700585 for arg in &efn.args {
586 if let Type::Fn(f) = &arg.ty {
587 let var = &arg.ident;
588 write_function_pointer_trampoline(out, efn, var, f, types);
589 }
590 }
591}
592
593fn write_function_pointer_trampoline(
594 out: &mut OutFile,
595 efn: &ExternFn,
596 var: &Ident,
597 f: &Signature,
598 types: &Types,
599) {
600 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700601 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700602 let indirect_call = true;
603 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
604
605 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700606 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700607 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400608}
609
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700610fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700611 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 let indirect_call = false;
613 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
614}
615
616fn write_rust_function_decl_impl(
617 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700618 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 sig: &Signature,
620 types: &Types,
621 indirect_call: bool,
622) {
623 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700624 write!(out, "::rust::Str::Repr ");
625 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700626 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700627 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 write!(out, "{}(", link_name);
629 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700630 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700631 if receiver.mutability.is_none() {
632 write!(out, "const ");
633 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700634 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700635 needs_comma = true;
636 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700637 for arg in &sig.args {
638 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400639 write!(out, ", ");
640 }
641 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700642 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400643 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 if indirect_return(sig, types) {
645 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400646 write!(out, ", ");
647 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700648 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400649 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700650 needs_comma = true;
651 }
652 if indirect_call {
653 if needs_comma {
654 write!(out, ", ");
655 }
656 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400657 }
658 writeln!(out, ") noexcept;");
659}
660
661fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400662 for line in efn.doc.to_string().lines() {
663 writeln!(out, "//{}", line);
664 }
David Tolnaya73853b2020-04-20 01:19:56 -0700665 let local_name = match &efn.sig.receiver {
666 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700667 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700668 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700669 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700670 let indirect_call = false;
671 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
672}
673
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700674fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700675 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700676 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700677 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700678 indirect_call: bool,
679) {
680 write_return_type(out, &sig.ret);
681 write!(out, "{}(", local_name);
682 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400683 if i > 0 {
684 write!(out, ", ");
685 }
686 write_type_space(out, &arg.ty);
687 write!(out, "{}", arg.ident);
688 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700689 if indirect_call {
690 if !sig.args.is_empty() {
691 write!(out, ", ");
692 }
693 write!(out, "void *extern$");
694 }
David Tolnay1e548172020-03-16 13:37:09 -0700695 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700696 if let Some(receiver) = &sig.receiver {
697 if receiver.mutability.is_none() {
698 write!(out, " const");
699 }
700 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700701 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700702 write!(out, " noexcept");
703 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700704}
705
706fn write_rust_function_shim_impl(
707 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700708 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700709 sig: &Signature,
710 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700711 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700712 indirect_call: bool,
713) {
714 if out.header && sig.receiver.is_some() {
715 // We've already defined this inside the struct.
716 return;
717 }
David Tolnaya73853b2020-04-20 01:19:56 -0700718 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400719 if out.header {
720 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700721 return;
David Tolnay7db73692019-10-20 14:51:12 -0400722 }
David Tolnay439cde22020-04-20 00:46:25 -0700723 writeln!(out, " {{");
724 for arg in &sig.args {
725 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
726 out.include.utility = true;
727 write!(out, " ::rust::ManuallyDrop<");
728 write_type(out, &arg.ty);
729 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
730 }
731 }
732 write!(out, " ");
733 let indirect_return = indirect_return(sig, types);
734 if indirect_return {
735 write!(out, "::rust::MaybeUninit<");
736 write_type(out, sig.ret.as_ref().unwrap());
737 writeln!(out, "> return$;");
738 write!(out, " ");
739 } else if let Some(ret) = &sig.ret {
740 write!(out, "return ");
741 match ret {
742 Type::RustBox(_) => {
743 write_type(out, ret);
744 write!(out, "::from_raw(");
745 }
746 Type::UniquePtr(_) => {
747 write_type(out, ret);
748 write!(out, "(");
749 }
750 Type::Ref(_) => write!(out, "*"),
751 _ => {}
752 }
753 }
754 if sig.throws {
755 write!(out, "::rust::Str::Repr error$ = ");
756 }
757 write!(out, "{}(", invoke);
758 if sig.receiver.is_some() {
759 write!(out, "*this");
760 }
761 for (i, arg) in sig.args.iter().enumerate() {
762 if i > 0 || sig.receiver.is_some() {
763 write!(out, ", ");
764 }
765 match &arg.ty {
766 Type::Str(_) => write!(out, "::rust::Str::Repr("),
767 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
768 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
769 _ => {}
770 }
771 write!(out, "{}", arg.ident);
772 match &arg.ty {
773 Type::RustBox(_) => write!(out, ".into_raw()"),
774 Type::UniquePtr(_) => write!(out, ".release()"),
775 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
776 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
777 _ => {}
778 }
779 }
780 if indirect_return {
781 if !sig.args.is_empty() {
782 write!(out, ", ");
783 }
784 write!(out, "&return$.value");
785 }
786 if indirect_call {
787 if !sig.args.is_empty() || indirect_return {
788 write!(out, ", ");
789 }
790 write!(out, "extern$");
791 }
792 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400793 if !indirect_return {
794 if let Some(ret) = &sig.ret {
795 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
796 write!(out, ")");
797 }
David Tolnay439cde22020-04-20 00:46:25 -0700798 }
799 }
800 writeln!(out, ";");
801 if sig.throws {
802 writeln!(out, " if (error$.ptr) {{");
803 writeln!(out, " throw ::rust::Error(error$);");
804 writeln!(out, " }}");
805 }
806 if indirect_return {
807 out.include.utility = true;
808 writeln!(out, " return ::std::move(return$.value);");
809 }
810 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400811}
812
813fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
814 match ty {
815 None => write!(out, "void "),
816 Some(ty) => write_type_space(out, ty),
817 }
818}
819
David Tolnay75dca2e2020-03-25 20:17:52 -0700820fn indirect_return(sig: &Signature, types: &Types) -> bool {
821 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700822 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700823 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700824}
825
David Tolnay99642622020-03-25 13:07:35 -0700826fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
827 match ty {
828 Type::RustBox(ty) | Type::UniquePtr(ty) => {
829 write_type_space(out, &ty.inner);
830 write!(out, "*");
831 }
832 Type::Ref(ty) => {
833 if ty.mutability.is_none() {
834 write!(out, "const ");
835 }
836 write_type(out, &ty.inner);
837 write!(out, " *");
838 }
839 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700840 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700841 _ => write_type(out, ty),
842 }
843}
844
845fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
846 write_indirect_return_type(out, ty);
847 match ty {
848 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700849 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700850 _ => write_space_after_type(out, ty),
851 }
852}
853
854fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400855 match ty {
856 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
857 write_type_space(out, &ty.inner);
858 write!(out, "*");
859 }
David Tolnay4a441222020-01-25 16:24:27 -0800860 Some(Type::Ref(ty)) => {
861 if ty.mutability.is_none() {
862 write!(out, "const ");
863 }
864 write_type(out, &ty.inner);
865 write!(out, " *");
866 }
David Tolnay750755e2020-03-01 13:04:08 -0800867 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700868 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400869 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
870 _ => write_return_type(out, ty),
871 }
872}
873
874fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
875 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700876 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400877 write_type_space(out, &ty.inner);
878 write!(out, "*");
879 }
David Tolnay750755e2020-03-01 13:04:08 -0800880 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700881 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400882 _ => write_type_space(out, &arg.ty),
883 }
884 if types.needs_indirect_abi(&arg.ty) {
885 write!(out, "*");
886 }
887 write!(out, "{}", arg.ident);
888}
889
890fn write_type(out: &mut OutFile, ty: &Type) {
891 match ty {
892 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700893 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400894 None => write!(out, "{}", ident),
895 },
896 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800897 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400898 write_type(out, &ty.inner);
899 write!(out, ">");
900 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700901 Type::RustVec(ty) => {
902 write!(out, "::rust::Vec<");
903 write_type(out, &ty.inner);
904 write!(out, ">");
905 }
David Tolnay7db73692019-10-20 14:51:12 -0400906 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800907 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400908 write_type(out, &ptr.inner);
909 write!(out, ">");
910 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700911 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700912 write!(out, "::std::vector<");
913 write_type(out, &ty.inner);
914 write!(out, ">");
915 }
David Tolnay7db73692019-10-20 14:51:12 -0400916 Type::Ref(r) => {
917 if r.mutability.is_none() {
918 write!(out, "const ");
919 }
920 write_type(out, &r.inner);
921 write!(out, " &");
922 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700923 Type::Slice(_) => {
924 // For now, only U8 slices are supported, which are covered separately below
925 unreachable!()
926 }
David Tolnay7db73692019-10-20 14:51:12 -0400927 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800928 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400929 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700930 Type::SliceRefU8(_) => {
931 write!(out, "::rust::Slice<uint8_t>");
932 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700933 Type::Fn(f) => {
934 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
935 match &f.ret {
936 Some(ret) => write_type(out, ret),
937 None => write!(out, "void"),
938 }
939 write!(out, "(");
940 for (i, arg) in f.args.iter().enumerate() {
941 if i > 0 {
942 write!(out, ", ");
943 }
944 write_type(out, &arg.ty);
945 }
946 write!(out, ")>");
947 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700948 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400949 }
950}
951
David Tolnayf6a89f22020-05-10 23:39:27 -0700952fn write_atom(out: &mut OutFile, atom: Atom) {
953 match atom {
954 Bool => write!(out, "bool"),
955 U8 => write!(out, "uint8_t"),
956 U16 => write!(out, "uint16_t"),
957 U32 => write!(out, "uint32_t"),
958 U64 => write!(out, "uint64_t"),
959 Usize => write!(out, "size_t"),
960 I8 => write!(out, "int8_t"),
961 I16 => write!(out, "int16_t"),
962 I32 => write!(out, "int32_t"),
963 I64 => write!(out, "int64_t"),
964 Isize => write!(out, "::rust::isize"),
965 F32 => write!(out, "float"),
966 F64 => write!(out, "double"),
967 CxxString => write!(out, "::std::string"),
968 RustString => write!(out, "::rust::String"),
969 }
970}
971
David Tolnay7db73692019-10-20 14:51:12 -0400972fn write_type_space(out: &mut OutFile, ty: &Type) {
973 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700974 write_space_after_type(out, ty);
975}
976
977fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400978 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700979 Type::Ident(_)
980 | Type::RustBox(_)
981 | Type::UniquePtr(_)
982 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700983 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700984 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700985 | Type::SliceRefU8(_)
986 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400987 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700988 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400989 }
990}
991
David Tolnaycd08c442020-04-25 10:16:33 -0700992// Only called for legal referent types of unique_ptr and element types of
993// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700994fn to_typename(namespace: &Namespace, ty: &Type) -> String {
995 match ty {
996 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700997 let mut path = String::new();
998 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700999 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -07001000 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001001 }
David Tolnaycd08c442020-04-25 10:16:33 -07001002 path += &ident.to_string();
1003 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001004 }
1005 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001006 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001007 }
1008}
1009
David Tolnayacdf20a2020-04-25 12:40:53 -07001010// Only called for legal referent types of unique_ptr and element types of
1011// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001012fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1013 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001014 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001015 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001016 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001017 }
1018}
1019
David Tolnay7db73692019-10-20 14:51:12 -04001020fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001021 out.begin_block("extern \"C\"");
1022 for ty in types {
1023 if let Type::RustBox(ty) = ty {
1024 if let Type::Ident(inner) = &ty.inner {
1025 out.next_section();
1026 write_rust_box_extern(out, inner);
1027 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001028 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001029 if let Type::Ident(inner) = &ty.inner {
1030 if Atom::from(inner).is_none() {
1031 out.next_section();
1032 write_rust_vec_extern(out, inner);
1033 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001034 }
David Tolnay7db73692019-10-20 14:51:12 -04001035 } else if let Type::UniquePtr(ptr) = ty {
1036 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001037 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001038 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001039 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001040 }
1041 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001042 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001043 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001044 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001045 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001046 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001047 }
1048 }
1049 }
1050 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001051 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001052
David Tolnay750755e2020-03-01 13:04:08 -08001053 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001054 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001055 for ty in types {
1056 if let Type::RustBox(ty) = ty {
1057 if let Type::Ident(inner) = &ty.inner {
1058 write_rust_box_impl(out, inner);
1059 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001060 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001061 if let Type::Ident(inner) = &ty.inner {
1062 if Atom::from(inner).is_none() {
1063 write_rust_vec_impl(out, inner);
1064 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001065 }
David Tolnay7db73692019-10-20 14:51:12 -04001066 }
1067 }
David Tolnay591dcb62020-09-01 23:00:38 -07001068 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001069 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001070}
1071
1072fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1073 let mut inner = String::new();
1074 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001075 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001076 inner += "::";
1077 }
1078 inner += &ident.to_string();
1079 let instance = inner.replace("::", "$");
1080
David Tolnay591dcb62020-09-01 23:00:38 -07001081 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1082 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001083 writeln!(
1084 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001085 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001086 instance, inner,
1087 );
1088 writeln!(
1089 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001090 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001091 instance, inner,
1092 );
David Tolnay591dcb62020-09-01 23:00:38 -07001093 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001094}
1095
David Tolnay6787be62020-04-25 11:01:02 -07001096fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1097 let element = Type::Ident(element.clone());
1098 let inner = to_typename(&out.namespace, &element);
1099 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001100
David Tolnay591dcb62020-09-01 23:00:38 -07001101 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1102 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001103 writeln!(
1104 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001105 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001106 instance, inner,
1107 );
1108 writeln!(
1109 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001110 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001111 instance, inner,
1112 );
1113 writeln!(
1114 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001115 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001116 instance, inner,
1117 );
David Tolnay219c0792020-04-24 20:31:37 -07001118 writeln!(
1119 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001120 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001121 inner, instance,
1122 );
David Tolnay503d0192020-04-24 22:18:56 -07001123 writeln!(
1124 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001125 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001126 instance,
1127 );
David Tolnay591dcb62020-09-01 23:00:38 -07001128 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001129}
1130
David Tolnay7db73692019-10-20 14:51:12 -04001131fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1132 let mut inner = String::new();
1133 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001134 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001135 inner += "::";
1136 }
1137 inner += &ident.to_string();
1138 let instance = inner.replace("::", "$");
1139
1140 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001141 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001142 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001143 writeln!(out, "}}");
1144
1145 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001146 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001147 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001148 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001149}
1150
David Tolnay6787be62020-04-25 11:01:02 -07001151fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1152 let element = Type::Ident(element.clone());
1153 let inner = to_typename(&out.namespace, &element);
1154 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001155
Myron Ahneba35cf2020-02-05 19:41:51 +07001156 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001157 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001158 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001159 writeln!(out, "}}");
1160
1161 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001162 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1163 writeln!(
1164 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001165 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001166 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001167 );
1168 writeln!(out, "}}");
1169
1170 writeln!(out, "template <>");
1171 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001172 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001173 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001174
1175 writeln!(out, "template <>");
1176 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1177 writeln!(
1178 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001179 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001180 instance,
1181 );
1182 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001183
1184 writeln!(out, "template <>");
1185 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001186 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001187 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001188}
1189
David Tolnay63da4d32020-04-25 09:41:12 -07001190fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1191 let ty = Type::Ident(ident.clone());
1192 let instance = to_mangled(&out.namespace, &ty);
1193
David Tolnay591dcb62020-09-01 23:00:38 -07001194 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1195 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001196
1197 write_unique_ptr_common(out, &ty, types);
1198
David Tolnay591dcb62020-09-01 23:00:38 -07001199 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001200}
1201
1202// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1203fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001204 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001205 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001206 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001207 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001208
David Tolnay63da4d32020-04-25 09:41:12 -07001209 let can_construct_from_value = match ty {
1210 Type::Ident(ident) => types.structs.contains_key(ident),
1211 _ => false,
1212 };
1213
David Tolnay7db73692019-10-20 14:51:12 -04001214 writeln!(
1215 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001216 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001217 inner,
1218 );
1219 writeln!(
1220 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001221 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001222 inner,
1223 );
1224 writeln!(
1225 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001226 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001227 instance, inner,
1228 );
David Tolnay7e219b82020-03-01 13:14:51 -08001229 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001230 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001231 if can_construct_from_value {
1232 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001233 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001234 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001235 instance, inner, inner,
1236 );
David Tolnay63da4d32020-04-25 09:41:12 -07001237 writeln!(
1238 out,
1239 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1240 inner, inner,
1241 );
1242 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001243 }
David Tolnay7db73692019-10-20 14:51:12 -04001244 writeln!(
1245 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001246 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001247 instance, inner, inner,
1248 );
David Tolnay7e219b82020-03-01 13:14:51 -08001249 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001250 writeln!(out, "}}");
1251 writeln!(
1252 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001253 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001254 inner, instance, inner,
1255 );
1256 writeln!(out, " return ptr.get();");
1257 writeln!(out, "}}");
1258 writeln!(
1259 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001260 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001261 inner, instance, inner,
1262 );
1263 writeln!(out, " return ptr.release();");
1264 writeln!(out, "}}");
1265 writeln!(
1266 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001267 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001268 instance, inner,
1269 );
1270 writeln!(out, " ptr->~unique_ptr();");
1271 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001272}
Myron Ahneba35cf2020-02-05 19:41:51 +07001273
David Tolnay92105da2020-04-25 11:15:31 -07001274fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001275 let element = Type::Ident(element.clone());
1276 let inner = to_typename(&out.namespace, &element);
1277 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001278
David Tolnay591dcb62020-09-01 23:00:38 -07001279 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1280 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001281 writeln!(
1282 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001283 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001284 instance, inner,
1285 );
1286 writeln!(out, " return s.size();");
1287 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001288 writeln!(
1289 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001290 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001291 inner, instance, inner,
1292 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001293 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001294 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001295
1296 write_unique_ptr_common(out, vector_ty, types);
1297
David Tolnay591dcb62020-09-01 23:00:38 -07001298 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001299}