blob: b31a239c2e3f2645c7d97c0a10f8ad75255afb55 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Joel Galensonc03402a2020-04-23 17:31:09 -07006use crate::syntax::{mangle, Api, Enum, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -07008use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04009
David Tolnay33d30292020-03-18 18:02:02 -070010pub(super) fn gen(
David Tolnay2ec14632020-05-04 00:47:10 -070011 namespace: &Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070012 apis: &[Api],
13 types: &Types,
David Tolnaya5cca312020-08-29 23:40:04 -070014 opt: &Opt,
David Tolnay33d30292020-03-18 18:02:02 -070015 header: bool,
16) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040017 let mut out_file = OutFile::new(namespace.clone(), header);
18 let out = &mut out_file;
19
David Tolnay54702b92020-07-31 11:50:09 -070020 if header {
21 writeln!(out.front, "#pragma once");
22 }
23
David Tolnaya5cca312020-08-29 23:40:04 -070024 out.include.extend(opt.include.clone());
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070027 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070035 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040036 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070045 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040046 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
David Tolnaya593d6e2020-08-29 19:48:08 -070065 if !types.cxx.contains(&strct.ident) {
66 write_struct(out, strct);
67 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070068 }
Joel Galensonc03402a2020-04-23 17:31:09 -070069 Api::Enum(enm) => {
70 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070071 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070072 check_enum(out, enm);
73 } else {
74 write_enum(out, enm);
75 }
Joel Galensonc03402a2020-04-23 17:31:09 -070076 }
David Tolnayc1fe0052020-04-17 15:15:06 -070077 Api::RustType(ety) => {
78 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070079 out.next_section();
80 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070081 }
David Tolnayc1fe0052020-04-17 15:15:06 -070082 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070083 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040084 }
85 }
86
87 if !header {
88 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070089 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040090 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070091 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040092 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
93 Api::RustFunction(efn) => (efn, write_rust_function_decl),
94 _ => continue,
95 };
96 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -070097 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -040098 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080099 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400100 }
101
102 for api in apis {
103 if let Api::RustFunction(efn) = api {
104 out.next_section();
105 write_rust_function_shim(out, efn, types);
106 }
107 }
108
109 out.next_section();
110 for name in namespace.iter().rev() {
111 writeln!(out, "}} // namespace {}", name);
112 }
113
114 if !header {
115 out.next_section();
116 write_generic_instantiations(out, types);
117 }
118
David Tolnay54702b92020-07-31 11:50:09 -0700119 write!(out.front, "{}", out.include);
120
121 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400122}
123
124fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400125 for ty in types {
126 match ty {
127 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700128 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
129 | Some(I64) => out.include.cstdint = true,
130 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800131 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700132 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400133 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800134 Type::RustBox(_) => out.include.type_traits = true,
135 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700136 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700137 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700138 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400139 }
140 }
David Tolnay7db73692019-10-20 14:51:12 -0400141}
142
David Tolnayf51447e2020-03-06 14:14:27 -0800143fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700144 let mut needs_rust_string = false;
145 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700146 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400147 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700148 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700149 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700150 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700151 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400152 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700153 match ty {
154 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700155 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700156 out.include.type_traits = true;
157 needs_rust_box = true;
158 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700159 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700160 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700161 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700162 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700163 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700164 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700165 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 Type::Str(_) => {
167 out.include.cstdint = true;
168 out.include.string = true;
169 needs_rust_str = true;
170 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700171 Type::Fn(_) => {
172 needs_rust_fn = true;
173 }
David Tolnay4770b472020-04-14 16:32:59 -0700174 Type::Slice(_) | Type::SliceRefU8(_) => {
175 needs_rust_slice = true;
176 }
David Tolnay7c295462020-04-25 12:45:07 -0700177 ty if ty == Isize => {
178 out.include.base_tsd = true;
179 needs_rust_isize = true;
180 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700181 ty if ty == RustString => {
182 out.include.array = true;
183 out.include.cstdint = true;
184 out.include.string = true;
185 needs_rust_string = true;
186 }
187 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400188 }
189 }
190
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800192 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800193 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700194 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800195 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700196 match api {
197 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700198 if efn.throws {
199 needs_trycatch = true;
200 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700201 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700202 let bitcopy = match arg.ty {
203 Type::RustVec(_) => true,
204 _ => arg.ty == RustString,
205 };
206 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 needs_unsafe_bitcopy = true;
208 break;
209 }
David Tolnay09011c32020-03-06 14:40:28 -0800210 }
211 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700212 Api::RustFunction(efn) if !out.header => {
213 if efn.throws {
214 out.include.exception = true;
215 needs_rust_error = true;
216 }
217 for arg in &efn.args {
218 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
219 needs_manually_drop = true;
220 break;
221 }
222 }
223 if let Some(ret) = &efn.ret {
224 if types.needs_indirect_abi(ret) {
225 needs_maybe_uninit = true;
226 }
David Tolnayf51447e2020-03-06 14:14:27 -0800227 }
228 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700229 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800230 }
231 }
232
David Tolnay750755e2020-03-01 13:04:08 -0800233 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700234 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800235
David Tolnayb7a7cb62020-03-17 21:18:40 -0700236 if needs_rust_string
237 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700238 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700240 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700241 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700242 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700243 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700244 || needs_unsafe_bitcopy
245 || needs_manually_drop
246 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700247 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700248 {
David Tolnay736cbca2020-03-11 16:49:18 -0700249 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800250 }
251
David Tolnay99a95e62020-09-02 15:05:53 -0700252 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700253 out.next_section();
254 writeln!(out, "struct unsafe_bitcopy_t;");
255 }
256
David Tolnay591dcb62020-09-01 23:00:38 -0700257 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
258 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
259 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
260 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700261 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700262 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
263 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
264 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
265 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800266
267 if needs_manually_drop {
268 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700269 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800270 writeln!(out, "template <typename T>");
271 writeln!(out, "union ManuallyDrop {{");
272 writeln!(out, " T value;");
273 writeln!(
274 out,
275 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
276 );
277 writeln!(out, " ~ManuallyDrop() {{}}");
278 writeln!(out, "}};");
279 }
280
David Tolnay09011c32020-03-06 14:40:28 -0800281 if needs_maybe_uninit {
282 out.next_section();
283 writeln!(out, "template <typename T>");
284 writeln!(out, "union MaybeUninit {{");
285 writeln!(out, " T value;");
286 writeln!(out, " MaybeUninit() {{}}");
287 writeln!(out, " ~MaybeUninit() {{}}");
288 writeln!(out, "}};");
289 }
290
David Tolnay591dcb62020-09-01 23:00:38 -0700291 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700292
David Tolnay5d121442020-03-17 22:14:40 -0700293 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700294 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700295 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700296 out.include.type_traits = true;
297 out.include.utility = true;
298 writeln!(out, "class missing {{}};");
299 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700300 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700301 writeln!(out, "template <typename Try, typename Fail>");
302 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700303 writeln!(
304 out,
David Tolnay04722332020-03-18 11:31:54 -0700305 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700306 );
David Tolnay04722332020-03-18 11:31:54 -0700307 writeln!(out, " missing>::value>::type");
308 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700309 writeln!(out, " func();");
310 writeln!(out, "}} catch (const ::std::exception &e) {{");
311 writeln!(out, " fail(e.what());");
312 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700313 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700314 }
315
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800316 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400317}
318
319fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700320 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700321 writeln!(out, "#ifndef {}", guard);
322 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400323 for line in strct.doc.to_string().lines() {
324 writeln!(out, "//{}", line);
325 }
326 writeln!(out, "struct {} final {{", strct.ident);
327 for field in &strct.fields {
328 write!(out, " ");
329 write_type_space(out, &field.ty);
330 writeln!(out, "{};", field.ident);
331 }
332 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700333 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400334}
335
336fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
337 writeln!(out, "struct {};", ident);
338}
339
David Tolnay8861bee2020-01-20 18:39:24 -0800340fn write_struct_using(out: &mut OutFile, ident: &Ident) {
341 writeln!(out, "using {} = {};", ident, ident);
342}
343
David Tolnayc1fe0052020-04-17 15:15:06 -0700344fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700345 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700346 writeln!(out, "#ifndef {}", guard);
347 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700348 for line in ety.doc.to_string().lines() {
349 writeln!(out, "//{}", line);
350 }
351 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700352 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700353 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700354 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700355 write!(out, " ");
356 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700357 let local_name = method.ident.to_string();
358 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700359 writeln!(out, ";");
360 }
361 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700362 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700363}
364
Joel Galensonc03402a2020-04-23 17:31:09 -0700365fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700366 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700367 writeln!(out, "#ifndef {}", guard);
368 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700369 for line in enm.doc.to_string().lines() {
370 writeln!(out, "//{}", line);
371 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700372 write!(out, "enum class {} : ", enm.ident);
373 write_atom(out, enm.repr);
374 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700375 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700376 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700377 }
378 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700379 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700380}
381
Joel Galenson905eb2e2020-05-04 14:58:14 -0700382fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700383 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
384 write_atom(out, enm.repr);
385 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700386 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700387 write!(out, "static_assert(static_cast<");
388 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700389 writeln!(
390 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700391 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700392 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700393 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700394 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700395}
396
David Tolnayebef4a22020-03-17 15:33:47 -0700397fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
398 let mut has_cxx_throws = false;
399 for api in apis {
400 if let Api::CxxFunction(efn) = api {
401 if efn.throws {
402 has_cxx_throws = true;
403 break;
404 }
405 }
406 }
407
408 if has_cxx_throws {
409 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700410 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700411 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700412 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700413 );
414 }
415}
416
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700417fn write_cxx_function_shim(
418 out: &mut OutFile,
419 efn: &ExternFn,
420 types: &Types,
421 impl_annotations: &Option<String>,
422) {
423 if !out.header {
424 if let Some(annotation) = impl_annotations {
425 write!(out, "{} ", annotation);
426 }
427 }
David Tolnayebef4a22020-03-17 15:33:47 -0700428 if efn.throws {
429 write!(out, "::rust::Str::Repr ");
430 } else {
David Tolnay99642622020-03-25 13:07:35 -0700431 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700432 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700433 let mangled = mangle::extern_fn(&out.namespace, efn);
434 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700435 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700436 if receiver.mutability.is_none() {
437 write!(out, "const ");
438 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700439 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700440 }
David Tolnay7db73692019-10-20 14:51:12 -0400441 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700442 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400443 write!(out, ", ");
444 }
David Tolnaya46a2372020-03-06 10:03:48 -0800445 if arg.ty == RustString {
446 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700447 } else if let Type::RustVec(_) = arg.ty {
448 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800449 }
David Tolnay7db73692019-10-20 14:51:12 -0400450 write_extern_arg(out, arg, types);
451 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700452 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400453 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700454 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400455 write!(out, ", ");
456 }
David Tolnay99642622020-03-25 13:07:35 -0700457 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400458 write!(out, "*return$");
459 }
460 writeln!(out, ") noexcept {{");
461 write!(out, " ");
462 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700463 match &efn.receiver {
464 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700465 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700466 }
David Tolnay7db73692019-10-20 14:51:12 -0400467 for (i, arg) in efn.args.iter().enumerate() {
468 if i > 0 {
469 write!(out, ", ");
470 }
471 write_type(out, &arg.ty);
472 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700473 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700474 if let Some(receiver) = &efn.receiver {
475 if receiver.mutability.is_none() {
476 write!(out, " const");
477 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700478 }
479 write!(out, " = ");
480 match &efn.receiver {
481 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700482 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700483 }
484 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400485 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700486 if efn.throws {
487 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700488 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700489 writeln!(out, " [&] {{");
490 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700491 }
David Tolnay7db73692019-10-20 14:51:12 -0400492 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700493 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400494 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700495 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400496 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700497 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400498 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700499 }
500 match &efn.ret {
501 Some(Type::Ref(_)) => write!(out, "&"),
502 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700503 Some(Type::SliceRefU8(_)) if !indirect_return => {
504 write!(out, "::rust::Slice<uint8_t>::Repr(")
505 }
David Tolnay99642622020-03-25 13:07:35 -0700506 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400507 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700508 match &efn.receiver {
509 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700510 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700511 }
David Tolnay7db73692019-10-20 14:51:12 -0400512 for (i, arg) in efn.args.iter().enumerate() {
513 if i > 0 {
514 write!(out, ", ");
515 }
516 if let Type::RustBox(_) = &arg.ty {
517 write_type(out, &arg.ty);
518 write!(out, "::from_raw({})", arg.ident);
519 } else if let Type::UniquePtr(_) = &arg.ty {
520 write_type(out, &arg.ty);
521 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800522 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800523 write!(
524 out,
525 "::rust::String(::rust::unsafe_bitcopy, *{})",
526 arg.ident,
527 );
David Tolnay313b10e2020-04-25 16:30:51 -0700528 } else if let Type::RustVec(_) = arg.ty {
529 write_type(out, &arg.ty);
530 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400531 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700532 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800533 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400534 } else {
535 write!(out, "{}", arg.ident);
536 }
537 }
538 write!(out, ")");
539 match &efn.ret {
540 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
541 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700542 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400543 _ => {}
544 }
545 if indirect_return {
546 write!(out, ")");
547 }
548 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700549 if efn.throws {
550 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700551 writeln!(out, " throw$.ptr = nullptr;");
552 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700553 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700554 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700555 writeln!(
556 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700557 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700558 );
David Tolnay5d121442020-03-17 22:14:40 -0700559 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700560 writeln!(out, " return throw$;");
561 }
David Tolnay7db73692019-10-20 14:51:12 -0400562 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700563 for arg in &efn.args {
564 if let Type::Fn(f) = &arg.ty {
565 let var = &arg.ident;
566 write_function_pointer_trampoline(out, efn, var, f, types);
567 }
568 }
569}
570
571fn write_function_pointer_trampoline(
572 out: &mut OutFile,
573 efn: &ExternFn,
574 var: &Ident,
575 f: &Signature,
576 types: &Types,
577) {
578 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700579 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700580 let indirect_call = true;
581 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
582
583 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700584 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700585 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400586}
587
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700588fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700589 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700590 let indirect_call = false;
591 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
592}
593
594fn write_rust_function_decl_impl(
595 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700596 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700597 sig: &Signature,
598 types: &Types,
599 indirect_call: bool,
600) {
601 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700602 write!(out, "::rust::Str::Repr ");
603 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700604 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700605 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700606 write!(out, "{}(", link_name);
607 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700608 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700609 if receiver.mutability.is_none() {
610 write!(out, "const ");
611 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700612 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700613 needs_comma = true;
614 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700615 for arg in &sig.args {
616 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400617 write!(out, ", ");
618 }
619 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700620 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400621 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700622 if indirect_return(sig, types) {
623 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400624 write!(out, ", ");
625 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700626 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400627 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 needs_comma = true;
629 }
630 if indirect_call {
631 if needs_comma {
632 write!(out, ", ");
633 }
634 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400635 }
636 writeln!(out, ") noexcept;");
637}
638
639fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400640 for line in efn.doc.to_string().lines() {
641 writeln!(out, "//{}", line);
642 }
David Tolnaya73853b2020-04-20 01:19:56 -0700643 let local_name = match &efn.sig.receiver {
644 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700645 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700646 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700647 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700648 let indirect_call = false;
649 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
650}
651
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700652fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700654 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700655 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 indirect_call: bool,
657) {
658 write_return_type(out, &sig.ret);
659 write!(out, "{}(", local_name);
660 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400661 if i > 0 {
662 write!(out, ", ");
663 }
664 write_type_space(out, &arg.ty);
665 write!(out, "{}", arg.ident);
666 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700667 if indirect_call {
668 if !sig.args.is_empty() {
669 write!(out, ", ");
670 }
671 write!(out, "void *extern$");
672 }
David Tolnay1e548172020-03-16 13:37:09 -0700673 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700674 if let Some(receiver) = &sig.receiver {
675 if receiver.mutability.is_none() {
676 write!(out, " const");
677 }
678 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700679 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700680 write!(out, " noexcept");
681 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700682}
683
684fn write_rust_function_shim_impl(
685 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700686 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700687 sig: &Signature,
688 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700689 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700690 indirect_call: bool,
691) {
692 if out.header && sig.receiver.is_some() {
693 // We've already defined this inside the struct.
694 return;
695 }
David Tolnaya73853b2020-04-20 01:19:56 -0700696 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400697 if out.header {
698 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700699 return;
David Tolnay7db73692019-10-20 14:51:12 -0400700 }
David Tolnay439cde22020-04-20 00:46:25 -0700701 writeln!(out, " {{");
702 for arg in &sig.args {
703 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
704 out.include.utility = true;
705 write!(out, " ::rust::ManuallyDrop<");
706 write_type(out, &arg.ty);
707 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
708 }
709 }
710 write!(out, " ");
711 let indirect_return = indirect_return(sig, types);
712 if indirect_return {
713 write!(out, "::rust::MaybeUninit<");
714 write_type(out, sig.ret.as_ref().unwrap());
715 writeln!(out, "> return$;");
716 write!(out, " ");
717 } else if let Some(ret) = &sig.ret {
718 write!(out, "return ");
719 match ret {
720 Type::RustBox(_) => {
721 write_type(out, ret);
722 write!(out, "::from_raw(");
723 }
724 Type::UniquePtr(_) => {
725 write_type(out, ret);
726 write!(out, "(");
727 }
728 Type::Ref(_) => write!(out, "*"),
729 _ => {}
730 }
731 }
732 if sig.throws {
733 write!(out, "::rust::Str::Repr error$ = ");
734 }
735 write!(out, "{}(", invoke);
736 if sig.receiver.is_some() {
737 write!(out, "*this");
738 }
739 for (i, arg) in sig.args.iter().enumerate() {
740 if i > 0 || sig.receiver.is_some() {
741 write!(out, ", ");
742 }
743 match &arg.ty {
744 Type::Str(_) => write!(out, "::rust::Str::Repr("),
745 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
746 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
747 _ => {}
748 }
749 write!(out, "{}", arg.ident);
750 match &arg.ty {
751 Type::RustBox(_) => write!(out, ".into_raw()"),
752 Type::UniquePtr(_) => write!(out, ".release()"),
753 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
754 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
755 _ => {}
756 }
757 }
758 if indirect_return {
759 if !sig.args.is_empty() {
760 write!(out, ", ");
761 }
762 write!(out, "&return$.value");
763 }
764 if indirect_call {
765 if !sig.args.is_empty() || indirect_return {
766 write!(out, ", ");
767 }
768 write!(out, "extern$");
769 }
770 write!(out, ")");
771 if let Some(ret) = &sig.ret {
772 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
773 write!(out, ")");
774 }
775 }
776 writeln!(out, ";");
777 if sig.throws {
778 writeln!(out, " if (error$.ptr) {{");
779 writeln!(out, " throw ::rust::Error(error$);");
780 writeln!(out, " }}");
781 }
782 if indirect_return {
783 out.include.utility = true;
784 writeln!(out, " return ::std::move(return$.value);");
785 }
786 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400787}
788
789fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
790 match ty {
791 None => write!(out, "void "),
792 Some(ty) => write_type_space(out, ty),
793 }
794}
795
David Tolnay75dca2e2020-03-25 20:17:52 -0700796fn indirect_return(sig: &Signature, types: &Types) -> bool {
797 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700798 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700799 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700800}
801
David Tolnay99642622020-03-25 13:07:35 -0700802fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
803 match ty {
804 Type::RustBox(ty) | Type::UniquePtr(ty) => {
805 write_type_space(out, &ty.inner);
806 write!(out, "*");
807 }
808 Type::Ref(ty) => {
809 if ty.mutability.is_none() {
810 write!(out, "const ");
811 }
812 write_type(out, &ty.inner);
813 write!(out, " *");
814 }
815 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700816 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700817 _ => write_type(out, ty),
818 }
819}
820
821fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
822 write_indirect_return_type(out, ty);
823 match ty {
824 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700825 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700826 _ => write_space_after_type(out, ty),
827 }
828}
829
830fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400831 match ty {
832 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
833 write_type_space(out, &ty.inner);
834 write!(out, "*");
835 }
David Tolnay4a441222020-01-25 16:24:27 -0800836 Some(Type::Ref(ty)) => {
837 if ty.mutability.is_none() {
838 write!(out, "const ");
839 }
840 write_type(out, &ty.inner);
841 write!(out, " *");
842 }
David Tolnay750755e2020-03-01 13:04:08 -0800843 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700844 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400845 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
846 _ => write_return_type(out, ty),
847 }
848}
849
850fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
851 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700852 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400853 write_type_space(out, &ty.inner);
854 write!(out, "*");
855 }
David Tolnay750755e2020-03-01 13:04:08 -0800856 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700857 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400858 _ => write_type_space(out, &arg.ty),
859 }
860 if types.needs_indirect_abi(&arg.ty) {
861 write!(out, "*");
862 }
863 write!(out, "{}", arg.ident);
864}
865
866fn write_type(out: &mut OutFile, ty: &Type) {
867 match ty {
868 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700869 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400870 None => write!(out, "{}", ident),
871 },
872 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800873 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400874 write_type(out, &ty.inner);
875 write!(out, ">");
876 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700877 Type::RustVec(ty) => {
878 write!(out, "::rust::Vec<");
879 write_type(out, &ty.inner);
880 write!(out, ">");
881 }
David Tolnay7db73692019-10-20 14:51:12 -0400882 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800883 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400884 write_type(out, &ptr.inner);
885 write!(out, ">");
886 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700887 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700888 write!(out, "::std::vector<");
889 write_type(out, &ty.inner);
890 write!(out, ">");
891 }
David Tolnay7db73692019-10-20 14:51:12 -0400892 Type::Ref(r) => {
893 if r.mutability.is_none() {
894 write!(out, "const ");
895 }
896 write_type(out, &r.inner);
897 write!(out, " &");
898 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700899 Type::Slice(_) => {
900 // For now, only U8 slices are supported, which are covered separately below
901 unreachable!()
902 }
David Tolnay7db73692019-10-20 14:51:12 -0400903 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800904 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400905 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700906 Type::SliceRefU8(_) => {
907 write!(out, "::rust::Slice<uint8_t>");
908 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700909 Type::Fn(f) => {
910 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
911 match &f.ret {
912 Some(ret) => write_type(out, ret),
913 None => write!(out, "void"),
914 }
915 write!(out, "(");
916 for (i, arg) in f.args.iter().enumerate() {
917 if i > 0 {
918 write!(out, ", ");
919 }
920 write_type(out, &arg.ty);
921 }
922 write!(out, ")>");
923 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700924 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400925 }
926}
927
David Tolnayf6a89f22020-05-10 23:39:27 -0700928fn write_atom(out: &mut OutFile, atom: Atom) {
929 match atom {
930 Bool => write!(out, "bool"),
931 U8 => write!(out, "uint8_t"),
932 U16 => write!(out, "uint16_t"),
933 U32 => write!(out, "uint32_t"),
934 U64 => write!(out, "uint64_t"),
935 Usize => write!(out, "size_t"),
936 I8 => write!(out, "int8_t"),
937 I16 => write!(out, "int16_t"),
938 I32 => write!(out, "int32_t"),
939 I64 => write!(out, "int64_t"),
940 Isize => write!(out, "::rust::isize"),
941 F32 => write!(out, "float"),
942 F64 => write!(out, "double"),
943 CxxString => write!(out, "::std::string"),
944 RustString => write!(out, "::rust::String"),
945 }
946}
947
David Tolnay7db73692019-10-20 14:51:12 -0400948fn write_type_space(out: &mut OutFile, ty: &Type) {
949 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700950 write_space_after_type(out, ty);
951}
952
953fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400954 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700955 Type::Ident(_)
956 | Type::RustBox(_)
957 | Type::UniquePtr(_)
958 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700959 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700960 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700961 | Type::SliceRefU8(_)
962 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400963 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700964 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400965 }
966}
967
David Tolnaycd08c442020-04-25 10:16:33 -0700968// Only called for legal referent types of unique_ptr and element types of
969// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700970fn to_typename(namespace: &Namespace, ty: &Type) -> String {
971 match ty {
972 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700973 let mut path = String::new();
974 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700975 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700976 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700977 }
David Tolnaycd08c442020-04-25 10:16:33 -0700978 path += &ident.to_string();
979 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700980 }
981 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700982 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700983 }
984}
985
David Tolnayacdf20a2020-04-25 12:40:53 -0700986// Only called for legal referent types of unique_ptr and element types of
987// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700988fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
989 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700990 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700991 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700992 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700993 }
994}
995
David Tolnay7db73692019-10-20 14:51:12 -0400996fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400997 out.begin_block("extern \"C\"");
998 for ty in types {
999 if let Type::RustBox(ty) = ty {
1000 if let Type::Ident(inner) = &ty.inner {
1001 out.next_section();
1002 write_rust_box_extern(out, inner);
1003 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001004 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001005 if let Type::Ident(inner) = &ty.inner {
1006 if Atom::from(inner).is_none() {
1007 out.next_section();
1008 write_rust_vec_extern(out, inner);
1009 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001010 }
David Tolnay7db73692019-10-20 14:51:12 -04001011 } else if let Type::UniquePtr(ptr) = ty {
1012 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001013 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001014 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001015 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001016 }
1017 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001018 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001019 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001020 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001021 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001022 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001023 }
1024 }
1025 }
1026 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001027 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001028
David Tolnay750755e2020-03-01 13:04:08 -08001029 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001030 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001031 for ty in types {
1032 if let Type::RustBox(ty) = ty {
1033 if let Type::Ident(inner) = &ty.inner {
1034 write_rust_box_impl(out, inner);
1035 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001036 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001037 if let Type::Ident(inner) = &ty.inner {
1038 if Atom::from(inner).is_none() {
1039 write_rust_vec_impl(out, inner);
1040 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001041 }
David Tolnay7db73692019-10-20 14:51:12 -04001042 }
1043 }
David Tolnay591dcb62020-09-01 23:00:38 -07001044 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001045 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001046}
1047
1048fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1049 let mut inner = String::new();
1050 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001051 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001052 inner += "::";
1053 }
1054 inner += &ident.to_string();
1055 let instance = inner.replace("::", "$");
1056
David Tolnay591dcb62020-09-01 23:00:38 -07001057 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1058 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001059 writeln!(
1060 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001061 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001062 instance, inner,
1063 );
1064 writeln!(
1065 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001066 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001067 instance, inner,
1068 );
David Tolnay591dcb62020-09-01 23:00:38 -07001069 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001070}
1071
David Tolnay6787be62020-04-25 11:01:02 -07001072fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1073 let element = Type::Ident(element.clone());
1074 let inner = to_typename(&out.namespace, &element);
1075 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001076
David Tolnay591dcb62020-09-01 23:00:38 -07001077 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1078 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001079 writeln!(
1080 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001081 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001082 instance, inner,
1083 );
1084 writeln!(
1085 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001086 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001087 instance, inner,
1088 );
1089 writeln!(
1090 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001091 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001092 instance, inner,
1093 );
David Tolnay219c0792020-04-24 20:31:37 -07001094 writeln!(
1095 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001096 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001097 inner, instance,
1098 );
David Tolnay503d0192020-04-24 22:18:56 -07001099 writeln!(
1100 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001101 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001102 instance,
1103 );
David Tolnay591dcb62020-09-01 23:00:38 -07001104 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001105}
1106
David Tolnay7db73692019-10-20 14:51:12 -04001107fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1108 let mut inner = String::new();
1109 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001110 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001111 inner += "::";
1112 }
1113 inner += &ident.to_string();
1114 let instance = inner.replace("::", "$");
1115
1116 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001117 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001118 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001119 writeln!(out, "}}");
1120
1121 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001122 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001123 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001124 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001125}
1126
David Tolnay6787be62020-04-25 11:01:02 -07001127fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1128 let element = Type::Ident(element.clone());
1129 let inner = to_typename(&out.namespace, &element);
1130 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001131
Myron Ahneba35cf2020-02-05 19:41:51 +07001132 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001133 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001134 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001135 writeln!(out, "}}");
1136
1137 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001138 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1139 writeln!(
1140 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001141 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001142 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001143 );
1144 writeln!(out, "}}");
1145
1146 writeln!(out, "template <>");
1147 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001148 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001149 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001150
1151 writeln!(out, "template <>");
1152 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1153 writeln!(
1154 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001155 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001156 instance,
1157 );
1158 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001159
1160 writeln!(out, "template <>");
1161 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001162 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001163 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001164}
1165
David Tolnay63da4d32020-04-25 09:41:12 -07001166fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1167 let ty = Type::Ident(ident.clone());
1168 let instance = to_mangled(&out.namespace, &ty);
1169
David Tolnay591dcb62020-09-01 23:00:38 -07001170 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1171 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001172
1173 write_unique_ptr_common(out, &ty, types);
1174
David Tolnay591dcb62020-09-01 23:00:38 -07001175 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001176}
1177
1178// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1179fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001180 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001181 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001182 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001183 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001184
David Tolnay63da4d32020-04-25 09:41:12 -07001185 let can_construct_from_value = match ty {
1186 Type::Ident(ident) => types.structs.contains_key(ident),
1187 _ => false,
1188 };
1189
David Tolnay7db73692019-10-20 14:51:12 -04001190 writeln!(
1191 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001192 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001193 inner,
1194 );
1195 writeln!(
1196 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001197 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001198 inner,
1199 );
1200 writeln!(
1201 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001202 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001203 instance, inner,
1204 );
David Tolnay7e219b82020-03-01 13:14:51 -08001205 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001206 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001207 if can_construct_from_value {
1208 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001209 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001210 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001211 instance, inner, inner,
1212 );
David Tolnay63da4d32020-04-25 09:41:12 -07001213 writeln!(
1214 out,
1215 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1216 inner, inner,
1217 );
1218 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001219 }
David Tolnay7db73692019-10-20 14:51:12 -04001220 writeln!(
1221 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001222 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001223 instance, inner, inner,
1224 );
David Tolnay7e219b82020-03-01 13:14:51 -08001225 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001226 writeln!(out, "}}");
1227 writeln!(
1228 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001229 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001230 inner, instance, inner,
1231 );
1232 writeln!(out, " return ptr.get();");
1233 writeln!(out, "}}");
1234 writeln!(
1235 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001236 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001237 inner, instance, inner,
1238 );
1239 writeln!(out, " return ptr.release();");
1240 writeln!(out, "}}");
1241 writeln!(
1242 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001243 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001244 instance, inner,
1245 );
1246 writeln!(out, " ptr->~unique_ptr();");
1247 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001248}
Myron Ahneba35cf2020-02-05 19:41:51 +07001249
David Tolnay92105da2020-04-25 11:15:31 -07001250fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001251 let element = Type::Ident(element.clone());
1252 let inner = to_typename(&out.namespace, &element);
1253 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001254
David Tolnay591dcb62020-09-01 23:00:38 -07001255 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1256 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001257 writeln!(
1258 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001259 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001260 instance, inner,
1261 );
1262 writeln!(out, " return s.size();");
1263 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001264 writeln!(
1265 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001266 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001267 inner, instance, inner,
1268 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001269 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001270 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001271
1272 write_unique_ptr_common(out, vector_ty, types);
1273
David Tolnay591dcb62020-09-01 23:00:38 -07001274 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001275}