blob: e088e2c0f0726d090373deed7a9eb87f4cfeec0a [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 Tolnay33d30292020-03-18 18:02:02 -070020 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040021 for api in apis {
22 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070023 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040024 }
25 }
26
27 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080028 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040029
David Tolnay7db73692019-10-20 14:51:12 -040030 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070031 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040032 writeln!(out, "namespace {} {{", name);
33 }
34
David Tolnay7db73692019-10-20 14:51:12 -040035 out.next_section();
36 for api in apis {
37 match api {
38 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080039 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
40 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070041 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040042 }
43 }
44
David Tolnayf94bef12020-04-17 14:46:42 -070045 let mut methods_for_type = HashMap::new();
46 for api in apis {
47 if let Api::RustFunction(efn) = api {
48 if let Some(receiver) = &efn.sig.receiver {
49 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070050 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070051 .or_insert_with(Vec::new)
52 .push(efn);
53 }
54 }
55 }
Joel Galenson968738f2020-04-15 14:19:33 -070056
David Tolnay7db73692019-10-20 14:51:12 -040057 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070058 match api {
59 Api::Struct(strct) => {
60 out.next_section();
David Tolnaydf4ca022020-07-31 18:33:44 -070061 write_struct(out, strct);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 }
Joel Galensonc03402a2020-04-23 17:31:09 -070063 Api::Enum(enm) => {
64 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070065 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070066 check_enum(out, enm);
67 } else {
David Tolnaydf4ca022020-07-31 18:33:44 -070068 write_enum(out, enm);
Joel Galenson905eb2e2020-05-04 14:58:14 -070069 }
Joel Galensonc03402a2020-04-23 17:31:09 -070070 }
David Tolnayc1fe0052020-04-17 15:15:06 -070071 Api::RustType(ety) => {
72 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070073 out.next_section();
David Tolnaydf4ca022020-07-31 18:33:44 -070074 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070075 }
David Tolnayc1fe0052020-04-17 15:15:06 -070076 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070077 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040078 }
79 }
80
81 if !header {
82 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070083 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040084 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070085 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040086 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
87 Api::RustFunction(efn) => (efn, write_rust_function_decl),
88 _ => continue,
89 };
90 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -070091 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -040092 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080093 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040094 }
95
96 for api in apis {
97 if let Api::RustFunction(efn) = api {
98 out.next_section();
99 write_rust_function_shim(out, efn, types);
100 }
101 }
102
103 out.next_section();
104 for name in namespace.iter().rev() {
105 writeln!(out, "}} // namespace {}", name);
106 }
107
108 if !header {
109 out.next_section();
110 write_generic_instantiations(out, types);
111 }
112
David Tolnayf4632de2020-07-31 10:46:55 -0700113 // We collected necessary includes lazily while generating the above. Now
114 // put it all together.
115 let mut full_file = OutFile::new(namespace.clone(), header);
116 let full = &mut full_file;
117 if header {
118 writeln!(full, "#pragma once");
119 }
120 write!(full, "{}", out.include);
121 full.next_section();
122 full.extend(out);
123 full_file
David Tolnay7db73692019-10-20 14:51:12 -0400124}
125
126fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400127 for ty in types {
128 match ty {
129 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700130 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
131 | Some(I64) => out.include.cstdint = true,
132 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800133 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700134 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400135 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800136 Type::RustBox(_) => out.include.type_traits = true,
137 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700138 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700139 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700140 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400141 }
142 }
David Tolnay7db73692019-10-20 14:51:12 -0400143}
144
David Tolnayf51447e2020-03-06 14:14:27 -0800145fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700146 let mut needs_rust_string = false;
147 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700148 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400149 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700150 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700151 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700152 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400153 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700154 match ty {
155 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700156 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700157 out.include.type_traits = true;
158 needs_rust_box = true;
159 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700160 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700161 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700162 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700163 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700164 needs_rust_vec = true;
165 }
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;
192 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800193 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800194 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700195 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800196 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700197 match api {
198 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700199 if efn.throws {
200 needs_trycatch = true;
201 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700202 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700203 let bitcopy = match arg.ty {
204 Type::RustVec(_) => true,
205 _ => arg.ty == RustString,
206 };
207 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700208 needs_unsafe_bitcopy = true;
209 break;
210 }
David Tolnay09011c32020-03-06 14:40:28 -0800211 }
212 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 Api::RustFunction(efn) if !out.header => {
214 if efn.throws {
215 out.include.exception = true;
216 needs_rust_error = true;
217 }
218 for arg in &efn.args {
219 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
220 needs_manually_drop = true;
221 break;
222 }
223 }
224 if let Some(ret) = &efn.ret {
225 if types.needs_indirect_abi(ret) {
226 needs_maybe_uninit = true;
227 }
David Tolnayf51447e2020-03-06 14:14:27 -0800228 }
229 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700230 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800231 }
232 }
233
David Tolnay750755e2020-03-01 13:04:08 -0800234 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700235 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800236
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 if needs_rust_string
238 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700239 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700240 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700241 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700242 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700244 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 || needs_unsafe_bitcopy
246 || needs_manually_drop
247 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700248 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 {
David Tolnay736cbca2020-03-11 16:49:18 -0700250 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800251 }
252
David Tolnay32ee9d32020-05-12 20:01:09 -0700253 if needs_rust_string || needs_rust_vec {
David Tolnayd1402742020-03-25 22:21:42 -0700254 out.next_section();
255 writeln!(out, "struct unsafe_bitcopy_t;");
256 }
257
David Tolnaye54f3382020-05-12 19:58:36 -0700258 include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
259 include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
260 include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
261 include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
262 include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
263 include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
264 include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
265 include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
266 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800267
268 if needs_manually_drop {
269 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700270 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800271 writeln!(out, "template <typename T>");
272 writeln!(out, "union ManuallyDrop {{");
273 writeln!(out, " T value;");
274 writeln!(
275 out,
276 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
277 );
278 writeln!(out, " ~ManuallyDrop() {{}}");
279 writeln!(out, "}};");
280 }
281
David Tolnay09011c32020-03-06 14:40:28 -0800282 if needs_maybe_uninit {
283 out.next_section();
284 writeln!(out, "template <typename T>");
285 writeln!(out, "union MaybeUninit {{");
286 writeln!(out, " T value;");
287 writeln!(out, " MaybeUninit() {{}}");
288 writeln!(out, " ~MaybeUninit() {{}}");
289 writeln!(out, "}};");
290 }
291
David Tolnay69601622020-04-29 18:48:36 -0700292 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700293
David Tolnay5d121442020-03-17 22:14:40 -0700294 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700295 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700296 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700297 out.include.type_traits = true;
298 out.include.utility = true;
299 writeln!(out, "class missing {{}};");
300 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700301 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700302 writeln!(out, "template <typename Try, typename Fail>");
303 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700304 writeln!(
305 out,
David Tolnay04722332020-03-18 11:31:54 -0700306 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700307 );
David Tolnay04722332020-03-18 11:31:54 -0700308 writeln!(out, " missing>::value>::type");
309 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700310 writeln!(out, " func();");
311 writeln!(out, "}} catch (const ::std::exception &e) {{");
312 writeln!(out, " fail(e.what());");
313 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700314 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700315 }
316
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800317 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400318}
319
David Tolnaydf4ca022020-07-31 18:33:44 -0700320fn write_struct(out: &mut OutFile, strct: &Struct) {
321 write_include_guard_start(out, &strct.ident);
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 Tolnaydf4ca022020-07-31 18:33:44 -0700332 write_include_guard_end(out, &strct.ident);
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
Adrian Taylora1f890b2020-07-31 15:35:34 -0700343const INCLUDE_GUARD_PREFIX: &'static str = "CXXBRIDGE03_TYPE_";
344
David Tolnaydf4ca022020-07-31 18:33:44 -0700345fn write_include_guard_start(out: &mut OutFile, ident: &Ident) {
Adrian Taylora1f890b2020-07-31 15:35:34 -0700346 writeln!(
347 out,
348 "#ifndef {}{}{}",
David Tolnaydf4ca022020-07-31 18:33:44 -0700349 INCLUDE_GUARD_PREFIX, out.namespace, ident
Adrian Taylora1f890b2020-07-31 15:35:34 -0700350 );
351 writeln!(
352 out,
353 "#define {}{}{}",
David Tolnaydf4ca022020-07-31 18:33:44 -0700354 INCLUDE_GUARD_PREFIX, out.namespace, ident
Adrian Taylora1f890b2020-07-31 15:35:34 -0700355 );
356}
357
David Tolnaydf4ca022020-07-31 18:33:44 -0700358fn write_include_guard_end(out: &mut OutFile, ident: &Ident) {
Adrian Taylora1f890b2020-07-31 15:35:34 -0700359 writeln!(
360 out,
361 "#endif // {}{}{}",
David Tolnaydf4ca022020-07-31 18:33:44 -0700362 INCLUDE_GUARD_PREFIX, out.namespace, ident
Adrian Taylora1f890b2020-07-31 15:35:34 -0700363 );
364}
365
David Tolnaydf4ca022020-07-31 18:33:44 -0700366fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
367 write_include_guard_start(out, &ety.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700368 for line in ety.doc.to_string().lines() {
369 writeln!(out, "//{}", line);
370 }
371 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700372 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700373 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700374 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700375 write!(out, " ");
376 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700377 let local_name = method.ident.to_string();
378 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700379 writeln!(out, ";");
380 }
381 writeln!(out, "}};");
David Tolnaydf4ca022020-07-31 18:33:44 -0700382 write_include_guard_end(out, &ety.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700383}
384
David Tolnaydf4ca022020-07-31 18:33:44 -0700385fn write_enum(out: &mut OutFile, enm: &Enum) {
386 write_include_guard_start(out, &enm.ident);
Joel Galensonc03402a2020-04-23 17:31:09 -0700387 for line in enm.doc.to_string().lines() {
388 writeln!(out, "//{}", line);
389 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700390 write!(out, "enum class {} : ", enm.ident);
391 write_atom(out, enm.repr);
392 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700393 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700394 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700395 }
396 writeln!(out, "}};");
David Tolnaydf4ca022020-07-31 18:33:44 -0700397 write_include_guard_end(out, &enm.ident);
Joel Galensonc03402a2020-04-23 17:31:09 -0700398}
399
Joel Galenson905eb2e2020-05-04 14:58:14 -0700400fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700401 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
402 write_atom(out, enm.repr);
403 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700404 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700405 write!(out, "static_assert(static_cast<");
406 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700407 writeln!(
408 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700409 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700410 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700411 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700412 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700413}
414
David Tolnayebef4a22020-03-17 15:33:47 -0700415fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
416 let mut has_cxx_throws = false;
417 for api in apis {
418 if let Api::CxxFunction(efn) = api {
419 if efn.throws {
420 has_cxx_throws = true;
421 break;
422 }
423 }
424 }
425
426 if has_cxx_throws {
427 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700428 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700429 out,
David Tolnay69601622020-04-29 18:48:36 -0700430 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700431 );
432 }
433}
434
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700435fn write_cxx_function_shim(
436 out: &mut OutFile,
437 efn: &ExternFn,
438 types: &Types,
439 impl_annotations: &Option<String>,
440) {
441 if !out.header {
442 if let Some(annotation) = impl_annotations {
443 write!(out, "{} ", annotation);
444 }
445 }
David Tolnayebef4a22020-03-17 15:33:47 -0700446 if efn.throws {
447 write!(out, "::rust::Str::Repr ");
448 } else {
David Tolnay99642622020-03-25 13:07:35 -0700449 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700450 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700451 let mangled = mangle::extern_fn(&out.namespace, efn);
452 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700453 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700454 if receiver.mutability.is_none() {
455 write!(out, "const ");
456 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700457 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700458 }
David Tolnay7db73692019-10-20 14:51:12 -0400459 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700460 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400461 write!(out, ", ");
462 }
David Tolnaya46a2372020-03-06 10:03:48 -0800463 if arg.ty == RustString {
464 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700465 } else if let Type::RustVec(_) = arg.ty {
466 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800467 }
David Tolnay7db73692019-10-20 14:51:12 -0400468 write_extern_arg(out, arg, types);
469 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700470 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400471 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700472 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400473 write!(out, ", ");
474 }
David Tolnay99642622020-03-25 13:07:35 -0700475 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400476 write!(out, "*return$");
477 }
478 writeln!(out, ") noexcept {{");
479 write!(out, " ");
480 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700481 match &efn.receiver {
482 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700483 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700484 }
David Tolnay7db73692019-10-20 14:51:12 -0400485 for (i, arg) in efn.args.iter().enumerate() {
486 if i > 0 {
487 write!(out, ", ");
488 }
489 write_type(out, &arg.ty);
490 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700491 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700492 if let Some(receiver) = &efn.receiver {
493 if receiver.mutability.is_none() {
494 write!(out, " const");
495 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700496 }
497 write!(out, " = ");
498 match &efn.receiver {
499 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700500 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700501 }
502 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400503 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700504 if efn.throws {
505 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700506 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700507 writeln!(out, " [&] {{");
508 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700509 }
David Tolnay7db73692019-10-20 14:51:12 -0400510 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700511 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400512 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700513 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400514 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700515 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400516 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700517 }
518 match &efn.ret {
519 Some(Type::Ref(_)) => write!(out, "&"),
520 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700521 Some(Type::SliceRefU8(_)) if !indirect_return => {
522 write!(out, "::rust::Slice<uint8_t>::Repr(")
523 }
David Tolnay99642622020-03-25 13:07:35 -0700524 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400525 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700526 match &efn.receiver {
527 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700528 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700529 }
David Tolnay7db73692019-10-20 14:51:12 -0400530 for (i, arg) in efn.args.iter().enumerate() {
531 if i > 0 {
532 write!(out, ", ");
533 }
534 if let Type::RustBox(_) = &arg.ty {
535 write_type(out, &arg.ty);
536 write!(out, "::from_raw({})", arg.ident);
537 } else if let Type::UniquePtr(_) = &arg.ty {
538 write_type(out, &arg.ty);
539 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800540 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800541 write!(
542 out,
543 "::rust::String(::rust::unsafe_bitcopy, *{})",
544 arg.ident,
545 );
David Tolnay313b10e2020-04-25 16:30:51 -0700546 } else if let Type::RustVec(_) = arg.ty {
547 write_type(out, &arg.ty);
548 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400549 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700550 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800551 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400552 } else {
553 write!(out, "{}", arg.ident);
554 }
555 }
556 write!(out, ")");
557 match &efn.ret {
558 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
559 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700560 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400561 _ => {}
562 }
563 if indirect_return {
564 write!(out, ")");
565 }
566 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700567 if efn.throws {
568 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700569 writeln!(out, " throw$.ptr = nullptr;");
570 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700571 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700572 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700573 writeln!(
574 out,
David Tolnay69601622020-04-29 18:48:36 -0700575 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700576 );
David Tolnay5d121442020-03-17 22:14:40 -0700577 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700578 writeln!(out, " return throw$;");
579 }
David Tolnay7db73692019-10-20 14:51:12 -0400580 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700581 for arg in &efn.args {
582 if let Type::Fn(f) = &arg.ty {
583 let var = &arg.ident;
584 write_function_pointer_trampoline(out, efn, var, f, types);
585 }
586 }
587}
588
589fn write_function_pointer_trampoline(
590 out: &mut OutFile,
591 efn: &ExternFn,
592 var: &Ident,
593 f: &Signature,
594 types: &Types,
595) {
596 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700597 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 let indirect_call = true;
599 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
600
601 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700602 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400604}
605
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700606fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700607 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700608 let indirect_call = false;
609 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
610}
611
612fn write_rust_function_decl_impl(
613 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700614 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700615 sig: &Signature,
616 types: &Types,
617 indirect_call: bool,
618) {
619 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700620 write!(out, "::rust::Str::Repr ");
621 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700622 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700623 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700624 write!(out, "{}(", link_name);
625 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700626 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700627 if receiver.mutability.is_none() {
628 write!(out, "const ");
629 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700630 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700631 needs_comma = true;
632 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700633 for arg in &sig.args {
634 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400635 write!(out, ", ");
636 }
637 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700638 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400639 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700640 if indirect_return(sig, types) {
641 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400642 write!(out, ", ");
643 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400645 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700646 needs_comma = true;
647 }
648 if indirect_call {
649 if needs_comma {
650 write!(out, ", ");
651 }
652 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400653 }
654 writeln!(out, ") noexcept;");
655}
656
657fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400658 for line in efn.doc.to_string().lines() {
659 writeln!(out, "//{}", line);
660 }
David Tolnaya73853b2020-04-20 01:19:56 -0700661 let local_name = match &efn.sig.receiver {
662 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700663 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700664 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700665 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700666 let indirect_call = false;
667 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
668}
669
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700670fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700671 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700672 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700673 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700674 indirect_call: bool,
675) {
676 write_return_type(out, &sig.ret);
677 write!(out, "{}(", local_name);
678 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400679 if i > 0 {
680 write!(out, ", ");
681 }
682 write_type_space(out, &arg.ty);
683 write!(out, "{}", arg.ident);
684 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700685 if indirect_call {
686 if !sig.args.is_empty() {
687 write!(out, ", ");
688 }
689 write!(out, "void *extern$");
690 }
David Tolnay1e548172020-03-16 13:37:09 -0700691 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700692 if let Some(receiver) = &sig.receiver {
693 if receiver.mutability.is_none() {
694 write!(out, " const");
695 }
696 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700697 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700698 write!(out, " noexcept");
699 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700700}
701
702fn write_rust_function_shim_impl(
703 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700704 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700705 sig: &Signature,
706 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700707 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700708 indirect_call: bool,
709) {
710 if out.header && sig.receiver.is_some() {
711 // We've already defined this inside the struct.
712 return;
713 }
David Tolnaya73853b2020-04-20 01:19:56 -0700714 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400715 if out.header {
716 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700717 return;
David Tolnay7db73692019-10-20 14:51:12 -0400718 }
David Tolnay439cde22020-04-20 00:46:25 -0700719 writeln!(out, " {{");
720 for arg in &sig.args {
721 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
722 out.include.utility = true;
723 write!(out, " ::rust::ManuallyDrop<");
724 write_type(out, &arg.ty);
725 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
726 }
727 }
728 write!(out, " ");
729 let indirect_return = indirect_return(sig, types);
730 if indirect_return {
731 write!(out, "::rust::MaybeUninit<");
732 write_type(out, sig.ret.as_ref().unwrap());
733 writeln!(out, "> return$;");
734 write!(out, " ");
735 } else if let Some(ret) = &sig.ret {
736 write!(out, "return ");
737 match ret {
738 Type::RustBox(_) => {
739 write_type(out, ret);
740 write!(out, "::from_raw(");
741 }
742 Type::UniquePtr(_) => {
743 write_type(out, ret);
744 write!(out, "(");
745 }
746 Type::Ref(_) => write!(out, "*"),
747 _ => {}
748 }
749 }
750 if sig.throws {
751 write!(out, "::rust::Str::Repr error$ = ");
752 }
753 write!(out, "{}(", invoke);
754 if sig.receiver.is_some() {
755 write!(out, "*this");
756 }
757 for (i, arg) in sig.args.iter().enumerate() {
758 if i > 0 || sig.receiver.is_some() {
759 write!(out, ", ");
760 }
761 match &arg.ty {
762 Type::Str(_) => write!(out, "::rust::Str::Repr("),
763 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
764 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
765 _ => {}
766 }
767 write!(out, "{}", arg.ident);
768 match &arg.ty {
769 Type::RustBox(_) => write!(out, ".into_raw()"),
770 Type::UniquePtr(_) => write!(out, ".release()"),
771 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
772 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
773 _ => {}
774 }
775 }
776 if indirect_return {
777 if !sig.args.is_empty() {
778 write!(out, ", ");
779 }
780 write!(out, "&return$.value");
781 }
782 if indirect_call {
783 if !sig.args.is_empty() || indirect_return {
784 write!(out, ", ");
785 }
786 write!(out, "extern$");
787 }
788 write!(out, ")");
789 if let Some(ret) = &sig.ret {
790 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
791 write!(out, ")");
792 }
793 }
794 writeln!(out, ";");
795 if sig.throws {
796 writeln!(out, " if (error$.ptr) {{");
797 writeln!(out, " throw ::rust::Error(error$);");
798 writeln!(out, " }}");
799 }
800 if indirect_return {
801 out.include.utility = true;
802 writeln!(out, " return ::std::move(return$.value);");
803 }
804 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400805}
806
807fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
808 match ty {
809 None => write!(out, "void "),
810 Some(ty) => write_type_space(out, ty),
811 }
812}
813
David Tolnay75dca2e2020-03-25 20:17:52 -0700814fn indirect_return(sig: &Signature, types: &Types) -> bool {
815 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700816 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700817 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700818}
819
David Tolnay99642622020-03-25 13:07:35 -0700820fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
821 match ty {
822 Type::RustBox(ty) | Type::UniquePtr(ty) => {
823 write_type_space(out, &ty.inner);
824 write!(out, "*");
825 }
826 Type::Ref(ty) => {
827 if ty.mutability.is_none() {
828 write!(out, "const ");
829 }
830 write_type(out, &ty.inner);
831 write!(out, " *");
832 }
833 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700834 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700835 _ => write_type(out, ty),
836 }
837}
838
839fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
840 write_indirect_return_type(out, ty);
841 match ty {
842 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700843 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700844 _ => write_space_after_type(out, ty),
845 }
846}
847
848fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400849 match ty {
850 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
851 write_type_space(out, &ty.inner);
852 write!(out, "*");
853 }
David Tolnay4a441222020-01-25 16:24:27 -0800854 Some(Type::Ref(ty)) => {
855 if ty.mutability.is_none() {
856 write!(out, "const ");
857 }
858 write_type(out, &ty.inner);
859 write!(out, " *");
860 }
David Tolnay750755e2020-03-01 13:04:08 -0800861 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700862 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400863 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
864 _ => write_return_type(out, ty),
865 }
866}
867
868fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
869 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700870 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400871 write_type_space(out, &ty.inner);
872 write!(out, "*");
873 }
David Tolnay750755e2020-03-01 13:04:08 -0800874 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700875 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400876 _ => write_type_space(out, &arg.ty),
877 }
878 if types.needs_indirect_abi(&arg.ty) {
879 write!(out, "*");
880 }
881 write!(out, "{}", arg.ident);
882}
883
884fn write_type(out: &mut OutFile, ty: &Type) {
885 match ty {
886 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700887 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400888 None => write!(out, "{}", ident),
889 },
890 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800891 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400892 write_type(out, &ty.inner);
893 write!(out, ">");
894 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700895 Type::RustVec(ty) => {
896 write!(out, "::rust::Vec<");
897 write_type(out, &ty.inner);
898 write!(out, ">");
899 }
David Tolnay7db73692019-10-20 14:51:12 -0400900 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800901 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400902 write_type(out, &ptr.inner);
903 write!(out, ">");
904 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700905 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700906 write!(out, "::std::vector<");
907 write_type(out, &ty.inner);
908 write!(out, ">");
909 }
David Tolnay7db73692019-10-20 14:51:12 -0400910 Type::Ref(r) => {
911 if r.mutability.is_none() {
912 write!(out, "const ");
913 }
914 write_type(out, &r.inner);
915 write!(out, " &");
916 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700917 Type::Slice(_) => {
918 // For now, only U8 slices are supported, which are covered separately below
919 unreachable!()
920 }
David Tolnay7db73692019-10-20 14:51:12 -0400921 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800922 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400923 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700924 Type::SliceRefU8(_) => {
925 write!(out, "::rust::Slice<uint8_t>");
926 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700927 Type::Fn(f) => {
928 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
929 match &f.ret {
930 Some(ret) => write_type(out, ret),
931 None => write!(out, "void"),
932 }
933 write!(out, "(");
934 for (i, arg) in f.args.iter().enumerate() {
935 if i > 0 {
936 write!(out, ", ");
937 }
938 write_type(out, &arg.ty);
939 }
940 write!(out, ")>");
941 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700942 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400943 }
944}
945
David Tolnayf6a89f22020-05-10 23:39:27 -0700946fn write_atom(out: &mut OutFile, atom: Atom) {
947 match atom {
948 Bool => write!(out, "bool"),
949 U8 => write!(out, "uint8_t"),
950 U16 => write!(out, "uint16_t"),
951 U32 => write!(out, "uint32_t"),
952 U64 => write!(out, "uint64_t"),
953 Usize => write!(out, "size_t"),
954 I8 => write!(out, "int8_t"),
955 I16 => write!(out, "int16_t"),
956 I32 => write!(out, "int32_t"),
957 I64 => write!(out, "int64_t"),
958 Isize => write!(out, "::rust::isize"),
959 F32 => write!(out, "float"),
960 F64 => write!(out, "double"),
961 CxxString => write!(out, "::std::string"),
962 RustString => write!(out, "::rust::String"),
963 }
964}
965
David Tolnay7db73692019-10-20 14:51:12 -0400966fn write_type_space(out: &mut OutFile, ty: &Type) {
967 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700968 write_space_after_type(out, ty);
969}
970
971fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400972 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700973 Type::Ident(_)
974 | Type::RustBox(_)
975 | Type::UniquePtr(_)
976 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700977 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700978 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700979 | Type::SliceRefU8(_)
980 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400981 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700982 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400983 }
984}
985
David Tolnaycd08c442020-04-25 10:16:33 -0700986// Only called for legal referent types of unique_ptr and element types of
987// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700988fn to_typename(namespace: &Namespace, ty: &Type) -> String {
989 match ty {
990 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700991 let mut path = String::new();
992 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700993 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700994 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700995 }
David Tolnaycd08c442020-04-25 10:16:33 -0700996 path += &ident.to_string();
997 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700998 }
999 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001000 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001001 }
1002}
1003
David Tolnayacdf20a2020-04-25 12:40:53 -07001004// Only called for legal referent types of unique_ptr and element types of
1005// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001006fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1007 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001008 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001009 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001010 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001011 }
1012}
1013
David Tolnay7db73692019-10-20 14:51:12 -04001014fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001015 out.begin_block("extern \"C\"");
1016 for ty in types {
1017 if let Type::RustBox(ty) = ty {
1018 if let Type::Ident(inner) = &ty.inner {
1019 out.next_section();
1020 write_rust_box_extern(out, inner);
1021 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001022 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001023 if let Type::Ident(inner) = &ty.inner {
1024 if Atom::from(inner).is_none() {
1025 out.next_section();
1026 write_rust_vec_extern(out, inner);
1027 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001028 }
David Tolnay7db73692019-10-20 14:51:12 -04001029 } else if let Type::UniquePtr(ptr) = ty {
1030 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001031 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001032 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001033 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001034 }
1035 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001036 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001037 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001038 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001039 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001040 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001041 }
1042 }
1043 }
1044 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001045 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001046
David Tolnay750755e2020-03-01 13:04:08 -08001047 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001048 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001049 for ty in types {
1050 if let Type::RustBox(ty) = ty {
1051 if let Type::Ident(inner) = &ty.inner {
1052 write_rust_box_impl(out, inner);
1053 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001054 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001055 if let Type::Ident(inner) = &ty.inner {
1056 if Atom::from(inner).is_none() {
1057 write_rust_vec_impl(out, inner);
1058 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001059 }
David Tolnay7db73692019-10-20 14:51:12 -04001060 }
1061 }
David Tolnay69601622020-04-29 18:48:36 -07001062 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001063 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001064}
1065
1066fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1067 let mut inner = String::new();
1068 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001069 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001070 inner += "::";
1071 }
1072 inner += &ident.to_string();
1073 let instance = inner.replace("::", "$");
1074
David Tolnay69601622020-04-29 18:48:36 -07001075 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1076 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001077 writeln!(
1078 out,
David Tolnay69601622020-04-29 18:48:36 -07001079 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001080 instance, inner,
1081 );
1082 writeln!(
1083 out,
David Tolnay69601622020-04-29 18:48:36 -07001084 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001085 instance, inner,
1086 );
David Tolnay69601622020-04-29 18:48:36 -07001087 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001088}
1089
David Tolnay6787be62020-04-25 11:01:02 -07001090fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1091 let element = Type::Ident(element.clone());
1092 let inner = to_typename(&out.namespace, &element);
1093 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001094
David Tolnay69601622020-04-29 18:48:36 -07001095 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1096 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001097 writeln!(
1098 out,
David Tolnay69601622020-04-29 18:48:36 -07001099 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001100 instance, inner,
1101 );
1102 writeln!(
1103 out,
David Tolnay69601622020-04-29 18:48:36 -07001104 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001105 instance, inner,
1106 );
1107 writeln!(
1108 out,
David Tolnay69601622020-04-29 18:48:36 -07001109 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001110 instance, inner,
1111 );
David Tolnay219c0792020-04-24 20:31:37 -07001112 writeln!(
1113 out,
David Tolnay69601622020-04-29 18:48:36 -07001114 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001115 inner, instance,
1116 );
David Tolnay503d0192020-04-24 22:18:56 -07001117 writeln!(
1118 out,
David Tolnay69601622020-04-29 18:48:36 -07001119 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001120 instance,
1121 );
David Tolnay69601622020-04-29 18:48:36 -07001122 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001123}
1124
David Tolnay7db73692019-10-20 14:51:12 -04001125fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1126 let mut inner = String::new();
1127 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001128 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001129 inner += "::";
1130 }
1131 inner += &ident.to_string();
1132 let instance = inner.replace("::", "$");
1133
1134 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001135 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001136 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001137 writeln!(out, "}}");
1138
1139 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001140 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001141 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001142 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001143}
1144
David Tolnay6787be62020-04-25 11:01:02 -07001145fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1146 let element = Type::Ident(element.clone());
1147 let inner = to_typename(&out.namespace, &element);
1148 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001149
Myron Ahneba35cf2020-02-05 19:41:51 +07001150 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001151 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001152 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001153 writeln!(out, "}}");
1154
1155 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001156 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1157 writeln!(
1158 out,
David Tolnay69601622020-04-29 18:48:36 -07001159 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001160 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001161 );
1162 writeln!(out, "}}");
1163
1164 writeln!(out, "template <>");
1165 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001166 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001167 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001168
1169 writeln!(out, "template <>");
1170 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1171 writeln!(
1172 out,
David Tolnay69601622020-04-29 18:48:36 -07001173 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001174 instance,
1175 );
1176 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001177
1178 writeln!(out, "template <>");
1179 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001180 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001181 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001182}
1183
David Tolnay63da4d32020-04-25 09:41:12 -07001184fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1185 let ty = Type::Ident(ident.clone());
1186 let instance = to_mangled(&out.namespace, &ty);
1187
David Tolnay69601622020-04-29 18:48:36 -07001188 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1189 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001190
1191 write_unique_ptr_common(out, &ty, types);
1192
David Tolnay69601622020-04-29 18:48:36 -07001193 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001194}
1195
1196// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1197fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001198 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001199 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001200 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001201 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001202
David Tolnay63da4d32020-04-25 09:41:12 -07001203 let can_construct_from_value = match ty {
1204 Type::Ident(ident) => types.structs.contains_key(ident),
1205 _ => false,
1206 };
1207
David Tolnay7db73692019-10-20 14:51:12 -04001208 writeln!(
1209 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001210 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001211 inner,
1212 );
1213 writeln!(
1214 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001215 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001216 inner,
1217 );
1218 writeln!(
1219 out,
David Tolnay69601622020-04-29 18:48:36 -07001220 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001221 instance, inner,
1222 );
David Tolnay7e219b82020-03-01 13:14:51 -08001223 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001224 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001225 if can_construct_from_value {
1226 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001227 out,
David Tolnay69601622020-04-29 18:48:36 -07001228 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001229 instance, inner, inner,
1230 );
David Tolnay63da4d32020-04-25 09:41:12 -07001231 writeln!(
1232 out,
1233 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1234 inner, inner,
1235 );
1236 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001237 }
David Tolnay7db73692019-10-20 14:51:12 -04001238 writeln!(
1239 out,
David Tolnay69601622020-04-29 18:48:36 -07001240 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001241 instance, inner, inner,
1242 );
David Tolnay7e219b82020-03-01 13:14:51 -08001243 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001244 writeln!(out, "}}");
1245 writeln!(
1246 out,
David Tolnay69601622020-04-29 18:48:36 -07001247 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001248 inner, instance, inner,
1249 );
1250 writeln!(out, " return ptr.get();");
1251 writeln!(out, "}}");
1252 writeln!(
1253 out,
David Tolnay69601622020-04-29 18:48:36 -07001254 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001255 inner, instance, inner,
1256 );
1257 writeln!(out, " return ptr.release();");
1258 writeln!(out, "}}");
1259 writeln!(
1260 out,
David Tolnay69601622020-04-29 18:48:36 -07001261 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001262 instance, inner,
1263 );
1264 writeln!(out, " ptr->~unique_ptr();");
1265 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001266}
Myron Ahneba35cf2020-02-05 19:41:51 +07001267
David Tolnay92105da2020-04-25 11:15:31 -07001268fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001269 let element = Type::Ident(element.clone());
1270 let inner = to_typename(&out.namespace, &element);
1271 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001272
David Tolnay69601622020-04-29 18:48:36 -07001273 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1274 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001275 writeln!(
1276 out,
David Tolnay69601622020-04-29 18:48:36 -07001277 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001278 instance, inner,
1279 );
1280 writeln!(out, " return s.size();");
1281 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001282 writeln!(
1283 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001284 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001285 inner, instance, inner,
1286 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001287 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001288 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001289
1290 write_unique_ptr_common(out, vector_ty, types);
1291
David Tolnay69601622020-04-29 18:48:36 -07001292 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001293}