blob: 32ccbf6efedff45966ba84d011780983f6ff45b7 [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,
14 opt: Opt,
15 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 Tolnay33d30292020-03-18 18:02:02 -070024 out.include.extend(opt.include);
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 Tolnay7db73692019-10-20 14:51:12 -0400151 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700152 match ty {
153 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700154 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700155 out.include.type_traits = true;
156 needs_rust_box = true;
157 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700158 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700159 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700160 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700161 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700162 needs_rust_vec = true;
163 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700164 Type::Str(_) => {
165 out.include.cstdint = true;
166 out.include.string = true;
167 needs_rust_str = true;
168 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700169 Type::Fn(_) => {
170 needs_rust_fn = true;
171 }
David Tolnay4770b472020-04-14 16:32:59 -0700172 Type::Slice(_) | Type::SliceRefU8(_) => {
173 needs_rust_slice = true;
174 }
David Tolnay7c295462020-04-25 12:45:07 -0700175 ty if ty == Isize => {
176 out.include.base_tsd = true;
177 needs_rust_isize = true;
178 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700179 ty if ty == RustString => {
180 out.include.array = true;
181 out.include.cstdint = true;
182 out.include.string = true;
183 needs_rust_string = true;
184 }
185 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400186 }
187 }
188
David Tolnayb7a7cb62020-03-17 21:18:40 -0700189 let mut needs_rust_error = false;
190 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800191 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800192 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700193 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800194 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700195 match api {
196 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700197 if efn.throws {
198 needs_trycatch = true;
199 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700200 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700201 let bitcopy = match arg.ty {
202 Type::RustVec(_) => true,
203 _ => arg.ty == RustString,
204 };
205 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700206 needs_unsafe_bitcopy = true;
207 break;
208 }
David Tolnay09011c32020-03-06 14:40:28 -0800209 }
210 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700211 Api::RustFunction(efn) if !out.header => {
212 if efn.throws {
213 out.include.exception = true;
214 needs_rust_error = true;
215 }
216 for arg in &efn.args {
217 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
218 needs_manually_drop = true;
219 break;
220 }
221 }
222 if let Some(ret) = &efn.ret {
223 if types.needs_indirect_abi(ret) {
224 needs_maybe_uninit = true;
225 }
David Tolnayf51447e2020-03-06 14:14:27 -0800226 }
227 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800229 }
230 }
231
David Tolnay750755e2020-03-01 13:04:08 -0800232 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700233 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800234
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 if needs_rust_string
236 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700237 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700238 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700239 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700240 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700242 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 || needs_unsafe_bitcopy
244 || needs_manually_drop
245 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700246 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700247 {
David Tolnay736cbca2020-03-11 16:49:18 -0700248 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800249 }
250
David Tolnay32ee9d32020-05-12 20:01:09 -0700251 if needs_rust_string || needs_rust_vec {
David Tolnayd1402742020-03-25 22:21:42 -0700252 out.next_section();
253 writeln!(out, "struct unsafe_bitcopy_t;");
254 }
255
David Tolnaye54f3382020-05-12 19:58:36 -0700256 include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
257 include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
258 include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
259 include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
260 include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
261 include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
262 include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
263 include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
264 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800265
266 if needs_manually_drop {
267 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700268 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800269 writeln!(out, "template <typename T>");
270 writeln!(out, "union ManuallyDrop {{");
271 writeln!(out, " T value;");
272 writeln!(
273 out,
274 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
275 );
276 writeln!(out, " ~ManuallyDrop() {{}}");
277 writeln!(out, "}};");
278 }
279
David Tolnay09011c32020-03-06 14:40:28 -0800280 if needs_maybe_uninit {
281 out.next_section();
282 writeln!(out, "template <typename T>");
283 writeln!(out, "union MaybeUninit {{");
284 writeln!(out, " T value;");
285 writeln!(out, " MaybeUninit() {{}}");
286 writeln!(out, " ~MaybeUninit() {{}}");
287 writeln!(out, "}};");
288 }
289
David Tolnay69601622020-04-29 18:48:36 -0700290 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700291
David Tolnay5d121442020-03-17 22:14:40 -0700292 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700293 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700294 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700295 out.include.type_traits = true;
296 out.include.utility = true;
297 writeln!(out, "class missing {{}};");
298 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700299 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700300 writeln!(out, "template <typename Try, typename Fail>");
301 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700302 writeln!(
303 out,
David Tolnay04722332020-03-18 11:31:54 -0700304 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700305 );
David Tolnay04722332020-03-18 11:31:54 -0700306 writeln!(out, " missing>::value>::type");
307 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700308 writeln!(out, " func();");
309 writeln!(out, "}} catch (const ::std::exception &e) {{");
310 writeln!(out, " fail(e.what());");
311 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700312 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700313 }
314
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800315 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400316}
317
318fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnaya25ea9c2020-08-27 22:59:38 -0700319 let guard = format!("CXXBRIDGE03_STRUCT_{}{}", out.namespace, strct.ident);
320 writeln!(out, "#ifndef {}", guard);
321 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400322 for line in strct.doc.to_string().lines() {
323 writeln!(out, "//{}", line);
324 }
325 writeln!(out, "struct {} final {{", strct.ident);
326 for field in &strct.fields {
327 write!(out, " ");
328 write_type_space(out, &field.ty);
329 writeln!(out, "{};", field.ident);
330 }
331 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700332 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400333}
334
335fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
336 writeln!(out, "struct {};", ident);
337}
338
David Tolnay8861bee2020-01-20 18:39:24 -0800339fn write_struct_using(out: &mut OutFile, ident: &Ident) {
340 writeln!(out, "using {} = {};", ident, ident);
341}
342
David Tolnayc1fe0052020-04-17 15:15:06 -0700343fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnaya25ea9c2020-08-27 22:59:38 -0700344 let guard = format!("CXXBRIDGE03_STRUCT_{}{}", out.namespace, ety.ident);
345 writeln!(out, "#ifndef {}", guard);
346 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700347 for line in ety.doc.to_string().lines() {
348 writeln!(out, "//{}", line);
349 }
350 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700351 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700352 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700353 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700354 write!(out, " ");
355 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700356 let local_name = method.ident.to_string();
357 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700358 writeln!(out, ";");
359 }
360 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700361 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700362}
363
Joel Galensonc03402a2020-04-23 17:31:09 -0700364fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnaya25ea9c2020-08-27 22:59:38 -0700365 let guard = format!("CXXBRIDGE03_ENUM_{}{}", out.namespace, enm.ident);
366 writeln!(out, "#ifndef {}", guard);
367 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700368 for line in enm.doc.to_string().lines() {
369 writeln!(out, "//{}", line);
370 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700371 write!(out, "enum class {} : ", enm.ident);
372 write_atom(out, enm.repr);
373 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700374 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700375 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700376 }
377 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700378 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700379}
380
Joel Galenson905eb2e2020-05-04 14:58:14 -0700381fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700382 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
383 write_atom(out, enm.repr);
384 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700385 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700386 write!(out, "static_assert(static_cast<");
387 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700388 writeln!(
389 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700390 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700391 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700392 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700393 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700394}
395
David Tolnayebef4a22020-03-17 15:33:47 -0700396fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
397 let mut has_cxx_throws = false;
398 for api in apis {
399 if let Api::CxxFunction(efn) = api {
400 if efn.throws {
401 has_cxx_throws = true;
402 break;
403 }
404 }
405 }
406
407 if has_cxx_throws {
408 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700409 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700410 out,
David Tolnay69601622020-04-29 18:48:36 -0700411 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700412 );
413 }
414}
415
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700416fn write_cxx_function_shim(
417 out: &mut OutFile,
418 efn: &ExternFn,
419 types: &Types,
420 impl_annotations: &Option<String>,
421) {
422 if !out.header {
423 if let Some(annotation) = impl_annotations {
424 write!(out, "{} ", annotation);
425 }
426 }
David Tolnayebef4a22020-03-17 15:33:47 -0700427 if efn.throws {
428 write!(out, "::rust::Str::Repr ");
429 } else {
David Tolnay99642622020-03-25 13:07:35 -0700430 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700431 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700432 let mangled = mangle::extern_fn(&out.namespace, efn);
433 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700434 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700435 if receiver.mutability.is_none() {
436 write!(out, "const ");
437 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700438 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700439 }
David Tolnay7db73692019-10-20 14:51:12 -0400440 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700441 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400442 write!(out, ", ");
443 }
David Tolnaya46a2372020-03-06 10:03:48 -0800444 if arg.ty == RustString {
445 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700446 } else if let Type::RustVec(_) = arg.ty {
447 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800448 }
David Tolnay7db73692019-10-20 14:51:12 -0400449 write_extern_arg(out, arg, types);
450 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700451 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400452 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700453 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400454 write!(out, ", ");
455 }
David Tolnay99642622020-03-25 13:07:35 -0700456 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400457 write!(out, "*return$");
458 }
459 writeln!(out, ") noexcept {{");
460 write!(out, " ");
461 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700462 match &efn.receiver {
463 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700464 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700465 }
David Tolnay7db73692019-10-20 14:51:12 -0400466 for (i, arg) in efn.args.iter().enumerate() {
467 if i > 0 {
468 write!(out, ", ");
469 }
470 write_type(out, &arg.ty);
471 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700472 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700473 if let Some(receiver) = &efn.receiver {
474 if receiver.mutability.is_none() {
475 write!(out, " const");
476 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700477 }
478 write!(out, " = ");
479 match &efn.receiver {
480 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700481 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700482 }
483 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400484 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700485 if efn.throws {
486 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700487 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700488 writeln!(out, " [&] {{");
489 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700490 }
David Tolnay7db73692019-10-20 14:51:12 -0400491 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700492 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400493 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700494 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400495 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700496 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400497 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700498 }
499 match &efn.ret {
500 Some(Type::Ref(_)) => write!(out, "&"),
501 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700502 Some(Type::SliceRefU8(_)) if !indirect_return => {
503 write!(out, "::rust::Slice<uint8_t>::Repr(")
504 }
David Tolnay99642622020-03-25 13:07:35 -0700505 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400506 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700507 match &efn.receiver {
508 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700509 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700510 }
David Tolnay7db73692019-10-20 14:51:12 -0400511 for (i, arg) in efn.args.iter().enumerate() {
512 if i > 0 {
513 write!(out, ", ");
514 }
515 if let Type::RustBox(_) = &arg.ty {
516 write_type(out, &arg.ty);
517 write!(out, "::from_raw({})", arg.ident);
518 } else if let Type::UniquePtr(_) = &arg.ty {
519 write_type(out, &arg.ty);
520 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800521 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800522 write!(
523 out,
524 "::rust::String(::rust::unsafe_bitcopy, *{})",
525 arg.ident,
526 );
David Tolnay313b10e2020-04-25 16:30:51 -0700527 } else if let Type::RustVec(_) = arg.ty {
528 write_type(out, &arg.ty);
529 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400530 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700531 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800532 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400533 } else {
534 write!(out, "{}", arg.ident);
535 }
536 }
537 write!(out, ")");
538 match &efn.ret {
539 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
540 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700541 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400542 _ => {}
543 }
544 if indirect_return {
545 write!(out, ")");
546 }
547 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700548 if efn.throws {
549 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700550 writeln!(out, " throw$.ptr = nullptr;");
551 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700552 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700553 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700554 writeln!(
555 out,
David Tolnay69601622020-04-29 18:48:36 -0700556 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700557 );
David Tolnay5d121442020-03-17 22:14:40 -0700558 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700559 writeln!(out, " return throw$;");
560 }
David Tolnay7db73692019-10-20 14:51:12 -0400561 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700562 for arg in &efn.args {
563 if let Type::Fn(f) = &arg.ty {
564 let var = &arg.ident;
565 write_function_pointer_trampoline(out, efn, var, f, types);
566 }
567 }
568}
569
570fn write_function_pointer_trampoline(
571 out: &mut OutFile,
572 efn: &ExternFn,
573 var: &Ident,
574 f: &Signature,
575 types: &Types,
576) {
577 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700578 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700579 let indirect_call = true;
580 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
581
582 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700583 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700584 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400585}
586
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700587fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700588 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700589 let indirect_call = false;
590 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
591}
592
593fn write_rust_function_decl_impl(
594 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700595 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700596 sig: &Signature,
597 types: &Types,
598 indirect_call: bool,
599) {
600 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700601 write!(out, "::rust::Str::Repr ");
602 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700604 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700605 write!(out, "{}(", link_name);
606 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700607 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700608 if receiver.mutability.is_none() {
609 write!(out, "const ");
610 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700611 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700612 needs_comma = true;
613 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700614 for arg in &sig.args {
615 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400616 write!(out, ", ");
617 }
618 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400620 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700621 if indirect_return(sig, types) {
622 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400623 write!(out, ", ");
624 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700625 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400626 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 needs_comma = true;
628 }
629 if indirect_call {
630 if needs_comma {
631 write!(out, ", ");
632 }
633 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400634 }
635 writeln!(out, ") noexcept;");
636}
637
638fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400639 for line in efn.doc.to_string().lines() {
640 writeln!(out, "//{}", line);
641 }
David Tolnaya73853b2020-04-20 01:19:56 -0700642 let local_name = match &efn.sig.receiver {
643 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700644 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700645 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700646 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700647 let indirect_call = false;
648 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
649}
650
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700651fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700653 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700654 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700655 indirect_call: bool,
656) {
657 write_return_type(out, &sig.ret);
658 write!(out, "{}(", local_name);
659 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400660 if i > 0 {
661 write!(out, ", ");
662 }
663 write_type_space(out, &arg.ty);
664 write!(out, "{}", arg.ident);
665 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700666 if indirect_call {
667 if !sig.args.is_empty() {
668 write!(out, ", ");
669 }
670 write!(out, "void *extern$");
671 }
David Tolnay1e548172020-03-16 13:37:09 -0700672 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700673 if let Some(receiver) = &sig.receiver {
674 if receiver.mutability.is_none() {
675 write!(out, " const");
676 }
677 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700678 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700679 write!(out, " noexcept");
680 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700681}
682
683fn write_rust_function_shim_impl(
684 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700685 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700686 sig: &Signature,
687 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700688 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700689 indirect_call: bool,
690) {
691 if out.header && sig.receiver.is_some() {
692 // We've already defined this inside the struct.
693 return;
694 }
David Tolnaya73853b2020-04-20 01:19:56 -0700695 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400696 if out.header {
697 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700698 return;
David Tolnay7db73692019-10-20 14:51:12 -0400699 }
David Tolnay439cde22020-04-20 00:46:25 -0700700 writeln!(out, " {{");
701 for arg in &sig.args {
702 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
703 out.include.utility = true;
704 write!(out, " ::rust::ManuallyDrop<");
705 write_type(out, &arg.ty);
706 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
707 }
708 }
709 write!(out, " ");
710 let indirect_return = indirect_return(sig, types);
711 if indirect_return {
712 write!(out, "::rust::MaybeUninit<");
713 write_type(out, sig.ret.as_ref().unwrap());
714 writeln!(out, "> return$;");
715 write!(out, " ");
716 } else if let Some(ret) = &sig.ret {
717 write!(out, "return ");
718 match ret {
719 Type::RustBox(_) => {
720 write_type(out, ret);
721 write!(out, "::from_raw(");
722 }
723 Type::UniquePtr(_) => {
724 write_type(out, ret);
725 write!(out, "(");
726 }
727 Type::Ref(_) => write!(out, "*"),
728 _ => {}
729 }
730 }
731 if sig.throws {
732 write!(out, "::rust::Str::Repr error$ = ");
733 }
734 write!(out, "{}(", invoke);
735 if sig.receiver.is_some() {
736 write!(out, "*this");
737 }
738 for (i, arg) in sig.args.iter().enumerate() {
739 if i > 0 || sig.receiver.is_some() {
740 write!(out, ", ");
741 }
742 match &arg.ty {
743 Type::Str(_) => write!(out, "::rust::Str::Repr("),
744 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
745 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
746 _ => {}
747 }
748 write!(out, "{}", arg.ident);
749 match &arg.ty {
750 Type::RustBox(_) => write!(out, ".into_raw()"),
751 Type::UniquePtr(_) => write!(out, ".release()"),
752 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
753 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
754 _ => {}
755 }
756 }
757 if indirect_return {
758 if !sig.args.is_empty() {
759 write!(out, ", ");
760 }
761 write!(out, "&return$.value");
762 }
763 if indirect_call {
764 if !sig.args.is_empty() || indirect_return {
765 write!(out, ", ");
766 }
767 write!(out, "extern$");
768 }
769 write!(out, ")");
770 if let Some(ret) = &sig.ret {
771 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
772 write!(out, ")");
773 }
774 }
775 writeln!(out, ";");
776 if sig.throws {
777 writeln!(out, " if (error$.ptr) {{");
778 writeln!(out, " throw ::rust::Error(error$);");
779 writeln!(out, " }}");
780 }
781 if indirect_return {
782 out.include.utility = true;
783 writeln!(out, " return ::std::move(return$.value);");
784 }
785 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400786}
787
788fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
789 match ty {
790 None => write!(out, "void "),
791 Some(ty) => write_type_space(out, ty),
792 }
793}
794
David Tolnay75dca2e2020-03-25 20:17:52 -0700795fn indirect_return(sig: &Signature, types: &Types) -> bool {
796 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700797 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700798 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700799}
800
David Tolnay99642622020-03-25 13:07:35 -0700801fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
802 match ty {
803 Type::RustBox(ty) | Type::UniquePtr(ty) => {
804 write_type_space(out, &ty.inner);
805 write!(out, "*");
806 }
807 Type::Ref(ty) => {
808 if ty.mutability.is_none() {
809 write!(out, "const ");
810 }
811 write_type(out, &ty.inner);
812 write!(out, " *");
813 }
814 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700815 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700816 _ => write_type(out, ty),
817 }
818}
819
820fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
821 write_indirect_return_type(out, ty);
822 match ty {
823 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700824 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700825 _ => write_space_after_type(out, ty),
826 }
827}
828
829fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400830 match ty {
831 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
832 write_type_space(out, &ty.inner);
833 write!(out, "*");
834 }
David Tolnay4a441222020-01-25 16:24:27 -0800835 Some(Type::Ref(ty)) => {
836 if ty.mutability.is_none() {
837 write!(out, "const ");
838 }
839 write_type(out, &ty.inner);
840 write!(out, " *");
841 }
David Tolnay750755e2020-03-01 13:04:08 -0800842 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700843 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400844 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
845 _ => write_return_type(out, ty),
846 }
847}
848
849fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
850 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700851 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400852 write_type_space(out, &ty.inner);
853 write!(out, "*");
854 }
David Tolnay750755e2020-03-01 13:04:08 -0800855 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700856 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400857 _ => write_type_space(out, &arg.ty),
858 }
859 if types.needs_indirect_abi(&arg.ty) {
860 write!(out, "*");
861 }
862 write!(out, "{}", arg.ident);
863}
864
865fn write_type(out: &mut OutFile, ty: &Type) {
866 match ty {
867 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700868 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400869 None => write!(out, "{}", ident),
870 },
871 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800872 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400873 write_type(out, &ty.inner);
874 write!(out, ">");
875 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700876 Type::RustVec(ty) => {
877 write!(out, "::rust::Vec<");
878 write_type(out, &ty.inner);
879 write!(out, ">");
880 }
David Tolnay7db73692019-10-20 14:51:12 -0400881 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800882 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400883 write_type(out, &ptr.inner);
884 write!(out, ">");
885 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700886 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700887 write!(out, "::std::vector<");
888 write_type(out, &ty.inner);
889 write!(out, ">");
890 }
David Tolnay7db73692019-10-20 14:51:12 -0400891 Type::Ref(r) => {
892 if r.mutability.is_none() {
893 write!(out, "const ");
894 }
895 write_type(out, &r.inner);
896 write!(out, " &");
897 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700898 Type::Slice(_) => {
899 // For now, only U8 slices are supported, which are covered separately below
900 unreachable!()
901 }
David Tolnay7db73692019-10-20 14:51:12 -0400902 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800903 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400904 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700905 Type::SliceRefU8(_) => {
906 write!(out, "::rust::Slice<uint8_t>");
907 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700908 Type::Fn(f) => {
909 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
910 match &f.ret {
911 Some(ret) => write_type(out, ret),
912 None => write!(out, "void"),
913 }
914 write!(out, "(");
915 for (i, arg) in f.args.iter().enumerate() {
916 if i > 0 {
917 write!(out, ", ");
918 }
919 write_type(out, &arg.ty);
920 }
921 write!(out, ")>");
922 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700923 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400924 }
925}
926
David Tolnayf6a89f22020-05-10 23:39:27 -0700927fn write_atom(out: &mut OutFile, atom: Atom) {
928 match atom {
929 Bool => write!(out, "bool"),
930 U8 => write!(out, "uint8_t"),
931 U16 => write!(out, "uint16_t"),
932 U32 => write!(out, "uint32_t"),
933 U64 => write!(out, "uint64_t"),
934 Usize => write!(out, "size_t"),
935 I8 => write!(out, "int8_t"),
936 I16 => write!(out, "int16_t"),
937 I32 => write!(out, "int32_t"),
938 I64 => write!(out, "int64_t"),
939 Isize => write!(out, "::rust::isize"),
940 F32 => write!(out, "float"),
941 F64 => write!(out, "double"),
942 CxxString => write!(out, "::std::string"),
943 RustString => write!(out, "::rust::String"),
944 }
945}
946
David Tolnay7db73692019-10-20 14:51:12 -0400947fn write_type_space(out: &mut OutFile, ty: &Type) {
948 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700949 write_space_after_type(out, ty);
950}
951
952fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400953 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700954 Type::Ident(_)
955 | Type::RustBox(_)
956 | Type::UniquePtr(_)
957 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700958 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700959 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700960 | Type::SliceRefU8(_)
961 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400962 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700963 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400964 }
965}
966
David Tolnaycd08c442020-04-25 10:16:33 -0700967// Only called for legal referent types of unique_ptr and element types of
968// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700969fn to_typename(namespace: &Namespace, ty: &Type) -> String {
970 match ty {
971 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700972 let mut path = String::new();
973 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700974 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700975 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700976 }
David Tolnaycd08c442020-04-25 10:16:33 -0700977 path += &ident.to_string();
978 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700979 }
980 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700981 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700982 }
983}
984
David Tolnayacdf20a2020-04-25 12:40:53 -0700985// Only called for legal referent types of unique_ptr and element types of
986// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700987fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
988 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700989 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700990 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700991 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700992 }
993}
994
David Tolnay7db73692019-10-20 14:51:12 -0400995fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400996 out.begin_block("extern \"C\"");
997 for ty in types {
998 if let Type::RustBox(ty) = ty {
999 if let Type::Ident(inner) = &ty.inner {
1000 out.next_section();
1001 write_rust_box_extern(out, inner);
1002 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001003 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001004 if let Type::Ident(inner) = &ty.inner {
1005 if Atom::from(inner).is_none() {
1006 out.next_section();
1007 write_rust_vec_extern(out, inner);
1008 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001009 }
David Tolnay7db73692019-10-20 14:51:12 -04001010 } else if let Type::UniquePtr(ptr) = ty {
1011 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001012 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001013 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001014 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001015 }
1016 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001017 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001018 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001019 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001020 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001021 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001022 }
1023 }
1024 }
1025 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001026 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001027
David Tolnay750755e2020-03-01 13:04:08 -08001028 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001029 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001030 for ty in types {
1031 if let Type::RustBox(ty) = ty {
1032 if let Type::Ident(inner) = &ty.inner {
1033 write_rust_box_impl(out, inner);
1034 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001035 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001036 if let Type::Ident(inner) = &ty.inner {
1037 if Atom::from(inner).is_none() {
1038 write_rust_vec_impl(out, inner);
1039 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001040 }
David Tolnay7db73692019-10-20 14:51:12 -04001041 }
1042 }
David Tolnay69601622020-04-29 18:48:36 -07001043 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001044 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001045}
1046
1047fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1048 let mut inner = String::new();
1049 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001050 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001051 inner += "::";
1052 }
1053 inner += &ident.to_string();
1054 let instance = inner.replace("::", "$");
1055
David Tolnay69601622020-04-29 18:48:36 -07001056 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1057 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001058 writeln!(
1059 out,
David Tolnay69601622020-04-29 18:48:36 -07001060 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001061 instance, inner,
1062 );
1063 writeln!(
1064 out,
David Tolnay69601622020-04-29 18:48:36 -07001065 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001066 instance, inner,
1067 );
David Tolnay69601622020-04-29 18:48:36 -07001068 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001069}
1070
David Tolnay6787be62020-04-25 11:01:02 -07001071fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1072 let element = Type::Ident(element.clone());
1073 let inner = to_typename(&out.namespace, &element);
1074 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001075
David Tolnay69601622020-04-29 18:48:36 -07001076 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1077 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001078 writeln!(
1079 out,
David Tolnay69601622020-04-29 18:48:36 -07001080 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001081 instance, inner,
1082 );
1083 writeln!(
1084 out,
David Tolnay69601622020-04-29 18:48:36 -07001085 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001086 instance, inner,
1087 );
1088 writeln!(
1089 out,
David Tolnay69601622020-04-29 18:48:36 -07001090 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001091 instance, inner,
1092 );
David Tolnay219c0792020-04-24 20:31:37 -07001093 writeln!(
1094 out,
David Tolnay69601622020-04-29 18:48:36 -07001095 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001096 inner, instance,
1097 );
David Tolnay503d0192020-04-24 22:18:56 -07001098 writeln!(
1099 out,
David Tolnay69601622020-04-29 18:48:36 -07001100 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001101 instance,
1102 );
David Tolnay69601622020-04-29 18:48:36 -07001103 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001104}
1105
David Tolnay7db73692019-10-20 14:51:12 -04001106fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1107 let mut inner = String::new();
1108 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001109 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001110 inner += "::";
1111 }
1112 inner += &ident.to_string();
1113 let instance = inner.replace("::", "$");
1114
1115 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001116 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001117 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001118 writeln!(out, "}}");
1119
1120 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001121 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001122 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001123 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001124}
1125
David Tolnay6787be62020-04-25 11:01:02 -07001126fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1127 let element = Type::Ident(element.clone());
1128 let inner = to_typename(&out.namespace, &element);
1129 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001130
Myron Ahneba35cf2020-02-05 19:41:51 +07001131 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001132 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001133 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001134 writeln!(out, "}}");
1135
1136 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001137 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1138 writeln!(
1139 out,
David Tolnay69601622020-04-29 18:48:36 -07001140 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001141 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001142 );
1143 writeln!(out, "}}");
1144
1145 writeln!(out, "template <>");
1146 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001147 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001148 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001149
1150 writeln!(out, "template <>");
1151 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1152 writeln!(
1153 out,
David Tolnay69601622020-04-29 18:48:36 -07001154 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001155 instance,
1156 );
1157 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001158
1159 writeln!(out, "template <>");
1160 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001161 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001162 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001163}
1164
David Tolnay63da4d32020-04-25 09:41:12 -07001165fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1166 let ty = Type::Ident(ident.clone());
1167 let instance = to_mangled(&out.namespace, &ty);
1168
David Tolnay69601622020-04-29 18:48:36 -07001169 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1170 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001171
1172 write_unique_ptr_common(out, &ty, types);
1173
David Tolnay69601622020-04-29 18:48:36 -07001174 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001175}
1176
1177// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1178fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001179 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001180 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001181 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001182 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001183
David Tolnay63da4d32020-04-25 09:41:12 -07001184 let can_construct_from_value = match ty {
1185 Type::Ident(ident) => types.structs.contains_key(ident),
1186 _ => false,
1187 };
1188
David Tolnay7db73692019-10-20 14:51:12 -04001189 writeln!(
1190 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001191 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001192 inner,
1193 );
1194 writeln!(
1195 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001196 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001197 inner,
1198 );
1199 writeln!(
1200 out,
David Tolnay69601622020-04-29 18:48:36 -07001201 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001202 instance, inner,
1203 );
David Tolnay7e219b82020-03-01 13:14:51 -08001204 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001205 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001206 if can_construct_from_value {
1207 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001208 out,
David Tolnay69601622020-04-29 18:48:36 -07001209 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001210 instance, inner, inner,
1211 );
David Tolnay63da4d32020-04-25 09:41:12 -07001212 writeln!(
1213 out,
1214 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1215 inner, inner,
1216 );
1217 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001218 }
David Tolnay7db73692019-10-20 14:51:12 -04001219 writeln!(
1220 out,
David Tolnay69601622020-04-29 18:48:36 -07001221 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001222 instance, inner, inner,
1223 );
David Tolnay7e219b82020-03-01 13:14:51 -08001224 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001225 writeln!(out, "}}");
1226 writeln!(
1227 out,
David Tolnay69601622020-04-29 18:48:36 -07001228 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001229 inner, instance, inner,
1230 );
1231 writeln!(out, " return ptr.get();");
1232 writeln!(out, "}}");
1233 writeln!(
1234 out,
David Tolnay69601622020-04-29 18:48:36 -07001235 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001236 inner, instance, inner,
1237 );
1238 writeln!(out, " return ptr.release();");
1239 writeln!(out, "}}");
1240 writeln!(
1241 out,
David Tolnay69601622020-04-29 18:48:36 -07001242 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001243 instance, inner,
1244 );
1245 writeln!(out, " ptr->~unique_ptr();");
1246 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001247}
Myron Ahneba35cf2020-02-05 19:41:51 +07001248
David Tolnay92105da2020-04-25 11:15:31 -07001249fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001250 let element = Type::Ident(element.clone());
1251 let inner = to_typename(&out.namespace, &element);
1252 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001253
David Tolnay69601622020-04-29 18:48:36 -07001254 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1255 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001256 writeln!(
1257 out,
David Tolnay69601622020-04-29 18:48:36 -07001258 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001259 instance, inner,
1260 );
1261 writeln!(out, " return s.size();");
1262 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001263 writeln!(
1264 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001265 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001266 inner, instance, inner,
1267 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001268 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001269 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001270
1271 write_unique_ptr_common(out, vector_ty, types);
1272
David Tolnay69601622020-04-29 18:48:36 -07001273 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001274}