blob: 3c81dc889ab69eefa662b0489b141251535d0cad [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Joel Galensonc03402a2020-04-23 17:31:09 -07006use crate::syntax::{mangle, Api, Enum, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -07008use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04009
David Tolnay33d30292020-03-18 18:02:02 -070010pub(super) fn gen(
David Tolnay2ec14632020-05-04 00:47:10 -070011 namespace: &Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070012 apis: &[Api],
13 types: &Types,
David Tolnaya5cca312020-08-29 23:40:04 -070014 opt: &Opt,
David Tolnay33d30292020-03-18 18:02:02 -070015 header: bool,
16) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040017 let mut out_file = OutFile::new(namespace.clone(), header);
18 let out = &mut out_file;
19
David Tolnay54702b92020-07-31 11:50:09 -070020 if header {
21 writeln!(out.front, "#pragma once");
22 }
23
David Tolnaya5cca312020-08-29 23:40:04 -070024 out.include.extend(opt.include.clone());
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070027 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070035 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040036 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070045 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040046 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
David Tolnaya593d6e2020-08-29 19:48:08 -070065 if !types.cxx.contains(&strct.ident) {
66 write_struct(out, strct);
67 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070068 }
Joel Galensonc03402a2020-04-23 17:31:09 -070069 Api::Enum(enm) => {
70 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070071 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070072 check_enum(out, enm);
73 } else {
74 write_enum(out, enm);
75 }
Joel Galensonc03402a2020-04-23 17:31:09 -070076 }
David Tolnayc1fe0052020-04-17 15:15:06 -070077 Api::RustType(ety) => {
78 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070079 out.next_section();
80 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070081 }
David Tolnayc1fe0052020-04-17 15:15:06 -070082 }
Adrian Taylor405e8742020-09-25 14:01:25 -070083 Api::TypeAlias(ety) => {
David Tolnay3208fd72020-10-03 20:19:40 -070084 if types.required_trivial.contains_key(&ety.ident) {
Adrian Taylor405e8742020-09-25 14:01:25 -070085 check_trivial_extern_type(out, &ety.ident)
86 }
87 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070088 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040089 }
90 }
91
92 if !header {
93 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070094 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040095 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070096 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040097 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
98 Api::RustFunction(efn) => (efn, write_rust_function_decl),
99 _ => continue,
100 };
101 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700102 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400103 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800104 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400105 }
106
107 for api in apis {
108 if let Api::RustFunction(efn) = api {
109 out.next_section();
110 write_rust_function_shim(out, efn, types);
111 }
112 }
113
114 out.next_section();
115 for name in namespace.iter().rev() {
116 writeln!(out, "}} // namespace {}", name);
117 }
118
119 if !header {
120 out.next_section();
121 write_generic_instantiations(out, types);
122 }
123
David Tolnay54702b92020-07-31 11:50:09 -0700124 write!(out.front, "{}", out.include);
125
126 out_file
David Tolnay7db73692019-10-20 14:51:12 -0400127}
128
129fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400130 for ty in types {
131 match ty {
David Tolnay89e386d2020-10-03 19:02:19 -0700132 Type::Ident(ident) => match Atom::from(ident) {
133 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
134 | Some(I64) => out.include.cstdint = true,
135 Some(Usize) => out.include.cstddef = true,
136 Some(CxxString) => out.include.string = true,
137 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) => {}
138 None => {
David Tolnay3208fd72020-10-03 20:19:40 -0700139 if types.aliases.contains_key(ident)
140 && types.required_trivial.contains_key(ident)
141 {
David Tolnay89e386d2020-10-03 19:02:19 -0700142 out.include.type_traits = true;
143 }
144 }
145 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800146 Type::RustBox(_) => out.include.type_traits = true,
147 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700148 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700149 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700150 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400151 }
152 }
David Tolnay7db73692019-10-20 14:51:12 -0400153}
154
David Tolnayf51447e2020-03-06 14:14:27 -0800155fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay16ab1462020-09-02 15:10:09 -0700156 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700157 let mut needs_rust_string = false;
158 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700159 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400160 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700161 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700162 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700163 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700164 let mut needs_unsafe_bitcopy = false;
David Tolnay7db73692019-10-20 14:51:12 -0400165 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 match ty {
167 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700168 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700169 out.include.type_traits = true;
170 needs_rust_box = true;
171 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700172 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700173 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700174 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700175 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700176 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700177 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700178 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700179 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700180 Type::Str(_) => {
181 out.include.cstdint = true;
182 out.include.string = true;
183 needs_rust_str = true;
184 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700185 Type::Fn(_) => {
186 needs_rust_fn = true;
187 }
David Tolnay4770b472020-04-14 16:32:59 -0700188 Type::Slice(_) | Type::SliceRefU8(_) => {
189 needs_rust_slice = true;
190 }
David Tolnay7c295462020-04-25 12:45:07 -0700191 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400192 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700193 needs_rust_isize = true;
194 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700195 ty if ty == RustString => {
196 out.include.array = true;
197 out.include.cstdint = true;
198 out.include.string = true;
199 needs_rust_string = true;
200 }
201 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400202 }
203 }
204
David Tolnayb7a7cb62020-03-17 21:18:40 -0700205 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800206 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800207 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700208 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800209 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700210 match api {
211 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700212 if efn.throws {
213 needs_trycatch = true;
214 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700215 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700216 let bitcopy = match arg.ty {
217 Type::RustVec(_) => true,
218 _ => arg.ty == RustString,
219 };
220 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 needs_unsafe_bitcopy = true;
222 break;
223 }
David Tolnay09011c32020-03-06 14:40:28 -0800224 }
225 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226 Api::RustFunction(efn) if !out.header => {
227 if efn.throws {
228 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700229 out.include.string = true;
230 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200232 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 }
234 for arg in &efn.args {
235 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
236 needs_manually_drop = true;
237 break;
238 }
239 }
240 if let Some(ret) = &efn.ret {
241 if types.needs_indirect_abi(ret) {
242 needs_maybe_uninit = true;
243 }
David Tolnayf51447e2020-03-06 14:14:27 -0800244 }
245 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700246 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800247 }
248 }
249
David Tolnay750755e2020-03-01 13:04:08 -0800250 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -0700251 out.begin_block("inline namespace cxxbridge04");
David Tolnayf51447e2020-03-06 14:14:27 -0800252
David Tolnay16ab1462020-09-02 15:10:09 -0700253 if needs_panic
254 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700255 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700256 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700257 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700258 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700259 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700260 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700261 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700262 || needs_unsafe_bitcopy
263 || needs_manually_drop
264 || needs_maybe_uninit
265 {
David Tolnay736cbca2020-03-11 16:49:18 -0700266 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800267 }
268
David Tolnay16ab1462020-09-02 15:10:09 -0700269 include::write(out, needs_panic, "CXXBRIDGE04_PANIC");
270
David Tolnay99a95e62020-09-02 15:05:53 -0700271 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700272 out.next_section();
273 writeln!(out, "struct unsafe_bitcopy_t;");
274 }
275
David Tolnay591dcb62020-09-01 23:00:38 -0700276 include::write(out, needs_rust_string, "CXXBRIDGE04_RUST_STRING");
277 include::write(out, needs_rust_str, "CXXBRIDGE04_RUST_STR");
278 include::write(out, needs_rust_slice, "CXXBRIDGE04_RUST_SLICE");
279 include::write(out, needs_rust_box, "CXXBRIDGE04_RUST_BOX");
David Tolnay99a95e62020-09-02 15:05:53 -0700280 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE04_RUST_BITCOPY");
David Tolnay591dcb62020-09-01 23:00:38 -0700281 include::write(out, needs_rust_vec, "CXXBRIDGE04_RUST_VEC");
282 include::write(out, needs_rust_fn, "CXXBRIDGE04_RUST_FN");
283 include::write(out, needs_rust_error, "CXXBRIDGE04_RUST_ERROR");
284 include::write(out, needs_rust_isize, "CXXBRIDGE04_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800285
286 if needs_manually_drop {
287 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700288 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800289 writeln!(out, "template <typename T>");
290 writeln!(out, "union ManuallyDrop {{");
291 writeln!(out, " T value;");
292 writeln!(
293 out,
294 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
295 );
296 writeln!(out, " ~ManuallyDrop() {{}}");
297 writeln!(out, "}};");
298 }
299
David Tolnay09011c32020-03-06 14:40:28 -0800300 if needs_maybe_uninit {
301 out.next_section();
302 writeln!(out, "template <typename T>");
303 writeln!(out, "union MaybeUninit {{");
304 writeln!(out, " T value;");
305 writeln!(out, " MaybeUninit() {{}}");
306 writeln!(out, " ~MaybeUninit() {{}}");
307 writeln!(out, "}};");
308 }
309
David Tolnay591dcb62020-09-01 23:00:38 -0700310 out.end_block("namespace cxxbridge04");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700311
David Tolnay5d121442020-03-17 22:14:40 -0700312 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700313 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700314 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700315 out.include.type_traits = true;
316 out.include.utility = true;
317 writeln!(out, "class missing {{}};");
318 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700319 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700320 writeln!(out, "template <typename Try, typename Fail>");
321 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700322 writeln!(
323 out,
David Tolnay04722332020-03-18 11:31:54 -0700324 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700325 );
David Tolnay04722332020-03-18 11:31:54 -0700326 writeln!(out, " missing>::value>::type");
327 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700328 writeln!(out, " func();");
329 writeln!(out, "}} catch (const ::std::exception &e) {{");
330 writeln!(out, " fail(e.what());");
331 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700332 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700333 }
334
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800335 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400336}
337
338fn write_struct(out: &mut OutFile, strct: &Struct) {
David Tolnay591dcb62020-09-01 23:00:38 -0700339 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, strct.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700340 writeln!(out, "#ifndef {}", guard);
341 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400342 for line in strct.doc.to_string().lines() {
343 writeln!(out, "//{}", line);
344 }
345 writeln!(out, "struct {} final {{", strct.ident);
346 for field in &strct.fields {
347 write!(out, " ");
348 write_type_space(out, &field.ty);
349 writeln!(out, "{};", field.ident);
350 }
351 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700352 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400353}
354
355fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
356 writeln!(out, "struct {};", ident);
357}
358
David Tolnay8861bee2020-01-20 18:39:24 -0800359fn write_struct_using(out: &mut OutFile, ident: &Ident) {
360 writeln!(out, "using {} = {};", ident, ident);
361}
362
David Tolnayc1fe0052020-04-17 15:15:06 -0700363fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
David Tolnay591dcb62020-09-01 23:00:38 -0700364 let guard = format!("CXXBRIDGE04_STRUCT_{}{}", out.namespace, ety.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700365 writeln!(out, "#ifndef {}", guard);
366 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700367 for line in ety.doc.to_string().lines() {
368 writeln!(out, "//{}", line);
369 }
370 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700371 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700372 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700373 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700374 write!(out, " ");
375 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700376 let local_name = method.ident.to_string();
377 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700378 writeln!(out, ";");
379 }
380 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700381 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700382}
383
Joel Galensonc03402a2020-04-23 17:31:09 -0700384fn write_enum(out: &mut OutFile, enm: &Enum) {
David Tolnay591dcb62020-09-01 23:00:38 -0700385 let guard = format!("CXXBRIDGE04_ENUM_{}{}", out.namespace, enm.ident);
David Tolnaya25ea9c2020-08-27 22:59:38 -0700386 writeln!(out, "#ifndef {}", guard);
387 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700388 for line in enm.doc.to_string().lines() {
389 writeln!(out, "//{}", line);
390 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700391 write!(out, "enum class {} : ", enm.ident);
392 write_atom(out, enm.repr);
393 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700394 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700395 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700396 }
397 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700398 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700399}
400
Joel Galenson905eb2e2020-05-04 14:58:14 -0700401fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700402 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
403 write_atom(out, enm.repr);
404 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700405 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700406 write!(out, "static_assert(static_cast<");
407 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700408 writeln!(
409 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700410 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700411 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700412 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700413 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700414}
415
Adrian Taylor405e8742020-09-25 14:01:25 -0700416fn check_trivial_extern_type(out: &mut OutFile, id: &Ident) {
David Tolnay7426cc12020-10-03 19:04:04 -0700417 writeln!(out, "static_assert(");
418 writeln!(
419 out,
420 " std::is_trivially_move_constructible<{}>::value,",
421 id,
422 );
423 writeln!(
424 out,
425 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
426 id,
427 );
428 writeln!(out, "static_assert(");
429 writeln!(out, " std::is_trivially_destructible<{}>::value,", id);
430 writeln!(
431 out,
432 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
433 id,
434 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700435}
436
David Tolnayebef4a22020-03-17 15:33:47 -0700437fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
438 let mut has_cxx_throws = false;
439 for api in apis {
440 if let Api::CxxFunction(efn) = api {
441 if efn.throws {
442 has_cxx_throws = true;
443 break;
444 }
445 }
446 }
447
448 if has_cxx_throws {
449 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700450 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700451 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700452 "const char *cxxbridge04$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700453 );
454 }
455}
456
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700457fn write_cxx_function_shim(
458 out: &mut OutFile,
459 efn: &ExternFn,
460 types: &Types,
461 impl_annotations: &Option<String>,
462) {
463 if !out.header {
464 if let Some(annotation) = impl_annotations {
465 write!(out, "{} ", annotation);
466 }
467 }
David Tolnayebef4a22020-03-17 15:33:47 -0700468 if efn.throws {
469 write!(out, "::rust::Str::Repr ");
470 } else {
David Tolnay99642622020-03-25 13:07:35 -0700471 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700472 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700473 let mangled = mangle::extern_fn(&out.namespace, efn);
474 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700475 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700476 if receiver.mutability.is_none() {
477 write!(out, "const ");
478 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700479 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700480 }
David Tolnay7db73692019-10-20 14:51:12 -0400481 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700482 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400483 write!(out, ", ");
484 }
David Tolnaya46a2372020-03-06 10:03:48 -0800485 if arg.ty == RustString {
486 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700487 } else if let Type::RustVec(_) = arg.ty {
488 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800489 }
David Tolnay7db73692019-10-20 14:51:12 -0400490 write_extern_arg(out, arg, types);
491 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700492 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400493 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700494 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400495 write!(out, ", ");
496 }
David Tolnay99642622020-03-25 13:07:35 -0700497 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400498 write!(out, "*return$");
499 }
500 writeln!(out, ") noexcept {{");
501 write!(out, " ");
502 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700503 match &efn.receiver {
504 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700505 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700506 }
David Tolnay7db73692019-10-20 14:51:12 -0400507 for (i, arg) in efn.args.iter().enumerate() {
508 if i > 0 {
509 write!(out, ", ");
510 }
511 write_type(out, &arg.ty);
512 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700513 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700514 if let Some(receiver) = &efn.receiver {
515 if receiver.mutability.is_none() {
516 write!(out, " const");
517 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700518 }
519 write!(out, " = ");
520 match &efn.receiver {
521 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700522 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700523 }
524 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400525 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700526 if efn.throws {
527 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700528 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700529 writeln!(out, " [&] {{");
530 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700531 }
David Tolnay7db73692019-10-20 14:51:12 -0400532 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700533 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400534 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700535 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400536 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700537 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400538 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700539 }
540 match &efn.ret {
541 Some(Type::Ref(_)) => write!(out, "&"),
542 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700543 Some(Type::SliceRefU8(_)) if !indirect_return => {
544 write!(out, "::rust::Slice<uint8_t>::Repr(")
545 }
David Tolnay99642622020-03-25 13:07:35 -0700546 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400547 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700548 match &efn.receiver {
549 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700550 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700551 }
David Tolnay7db73692019-10-20 14:51:12 -0400552 for (i, arg) in efn.args.iter().enumerate() {
553 if i > 0 {
554 write!(out, ", ");
555 }
556 if let Type::RustBox(_) = &arg.ty {
557 write_type(out, &arg.ty);
558 write!(out, "::from_raw({})", arg.ident);
559 } else if let Type::UniquePtr(_) = &arg.ty {
560 write_type(out, &arg.ty);
561 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800562 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800563 write!(
564 out,
565 "::rust::String(::rust::unsafe_bitcopy, *{})",
566 arg.ident,
567 );
David Tolnay313b10e2020-04-25 16:30:51 -0700568 } else if let Type::RustVec(_) = arg.ty {
569 write_type(out, &arg.ty);
570 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400571 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700572 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800573 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400574 } else {
575 write!(out, "{}", arg.ident);
576 }
577 }
578 write!(out, ")");
579 match &efn.ret {
580 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
581 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700582 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400583 _ => {}
584 }
585 if indirect_return {
586 write!(out, ")");
587 }
588 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700589 if efn.throws {
590 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700591 writeln!(out, " throw$.ptr = nullptr;");
592 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700593 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700594 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700595 writeln!(
596 out,
David Tolnay591dcb62020-09-01 23:00:38 -0700597 " throw$.ptr = cxxbridge04$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700598 );
David Tolnay5d121442020-03-17 22:14:40 -0700599 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700600 writeln!(out, " return throw$;");
601 }
David Tolnay7db73692019-10-20 14:51:12 -0400602 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 for arg in &efn.args {
604 if let Type::Fn(f) = &arg.ty {
605 let var = &arg.ident;
606 write_function_pointer_trampoline(out, efn, var, f, types);
607 }
608 }
609}
610
611fn write_function_pointer_trampoline(
612 out: &mut OutFile,
613 efn: &ExternFn,
614 var: &Ident,
615 f: &Signature,
616 types: &Types,
617) {
618 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700619 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700620 let indirect_call = true;
621 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
622
623 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700624 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700625 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400626}
627
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700628fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700629 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700630 let indirect_call = false;
631 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
632}
633
634fn write_rust_function_decl_impl(
635 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700636 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700637 sig: &Signature,
638 types: &Types,
639 indirect_call: bool,
640) {
641 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700642 write!(out, "::rust::Str::Repr ");
643 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700645 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700646 write!(out, "{}(", link_name);
647 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700648 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700649 if receiver.mutability.is_none() {
650 write!(out, "const ");
651 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700652 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700653 needs_comma = true;
654 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700655 for arg in &sig.args {
656 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400657 write!(out, ", ");
658 }
659 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700660 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400661 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700662 if indirect_return(sig, types) {
663 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400664 write!(out, ", ");
665 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700666 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400667 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700668 needs_comma = true;
669 }
670 if indirect_call {
671 if needs_comma {
672 write!(out, ", ");
673 }
674 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400675 }
676 writeln!(out, ") noexcept;");
677}
678
679fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400680 for line in efn.doc.to_string().lines() {
681 writeln!(out, "//{}", line);
682 }
David Tolnaya73853b2020-04-20 01:19:56 -0700683 let local_name = match &efn.sig.receiver {
684 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700685 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700686 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700687 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700688 let indirect_call = false;
689 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
690}
691
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700692fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700693 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700694 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700695 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700696 indirect_call: bool,
697) {
698 write_return_type(out, &sig.ret);
699 write!(out, "{}(", local_name);
700 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400701 if i > 0 {
702 write!(out, ", ");
703 }
704 write_type_space(out, &arg.ty);
705 write!(out, "{}", arg.ident);
706 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700707 if indirect_call {
708 if !sig.args.is_empty() {
709 write!(out, ", ");
710 }
711 write!(out, "void *extern$");
712 }
David Tolnay1e548172020-03-16 13:37:09 -0700713 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700714 if let Some(receiver) = &sig.receiver {
715 if receiver.mutability.is_none() {
716 write!(out, " const");
717 }
718 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700719 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700720 write!(out, " noexcept");
721 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700722}
723
724fn write_rust_function_shim_impl(
725 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700726 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700727 sig: &Signature,
728 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700729 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700730 indirect_call: bool,
731) {
732 if out.header && sig.receiver.is_some() {
733 // We've already defined this inside the struct.
734 return;
735 }
David Tolnaya73853b2020-04-20 01:19:56 -0700736 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400737 if out.header {
738 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700739 return;
David Tolnay7db73692019-10-20 14:51:12 -0400740 }
David Tolnay439cde22020-04-20 00:46:25 -0700741 writeln!(out, " {{");
742 for arg in &sig.args {
743 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
744 out.include.utility = true;
745 write!(out, " ::rust::ManuallyDrop<");
746 write_type(out, &arg.ty);
747 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
748 }
749 }
750 write!(out, " ");
751 let indirect_return = indirect_return(sig, types);
752 if indirect_return {
753 write!(out, "::rust::MaybeUninit<");
754 write_type(out, sig.ret.as_ref().unwrap());
755 writeln!(out, "> return$;");
756 write!(out, " ");
757 } else if let Some(ret) = &sig.ret {
758 write!(out, "return ");
759 match ret {
760 Type::RustBox(_) => {
761 write_type(out, ret);
762 write!(out, "::from_raw(");
763 }
764 Type::UniquePtr(_) => {
765 write_type(out, ret);
766 write!(out, "(");
767 }
768 Type::Ref(_) => write!(out, "*"),
769 _ => {}
770 }
771 }
772 if sig.throws {
773 write!(out, "::rust::Str::Repr error$ = ");
774 }
775 write!(out, "{}(", invoke);
776 if sig.receiver.is_some() {
777 write!(out, "*this");
778 }
779 for (i, arg) in sig.args.iter().enumerate() {
780 if i > 0 || sig.receiver.is_some() {
781 write!(out, ", ");
782 }
783 match &arg.ty {
784 Type::Str(_) => write!(out, "::rust::Str::Repr("),
785 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
786 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
787 _ => {}
788 }
789 write!(out, "{}", arg.ident);
790 match &arg.ty {
791 Type::RustBox(_) => write!(out, ".into_raw()"),
792 Type::UniquePtr(_) => write!(out, ".release()"),
793 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
794 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
795 _ => {}
796 }
797 }
798 if indirect_return {
799 if !sig.args.is_empty() {
800 write!(out, ", ");
801 }
802 write!(out, "&return$.value");
803 }
804 if indirect_call {
805 if !sig.args.is_empty() || indirect_return {
806 write!(out, ", ");
807 }
808 write!(out, "extern$");
809 }
810 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400811 if !indirect_return {
812 if let Some(ret) = &sig.ret {
813 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
814 write!(out, ")");
815 }
David Tolnay439cde22020-04-20 00:46:25 -0700816 }
817 }
818 writeln!(out, ";");
819 if sig.throws {
820 writeln!(out, " if (error$.ptr) {{");
821 writeln!(out, " throw ::rust::Error(error$);");
822 writeln!(out, " }}");
823 }
824 if indirect_return {
825 out.include.utility = true;
826 writeln!(out, " return ::std::move(return$.value);");
827 }
828 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400829}
830
831fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
832 match ty {
833 None => write!(out, "void "),
834 Some(ty) => write_type_space(out, ty),
835 }
836}
837
David Tolnay75dca2e2020-03-25 20:17:52 -0700838fn indirect_return(sig: &Signature, types: &Types) -> bool {
839 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700840 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700841 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700842}
843
David Tolnay99642622020-03-25 13:07:35 -0700844fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
845 match ty {
846 Type::RustBox(ty) | Type::UniquePtr(ty) => {
847 write_type_space(out, &ty.inner);
848 write!(out, "*");
849 }
850 Type::Ref(ty) => {
851 if ty.mutability.is_none() {
852 write!(out, "const ");
853 }
854 write_type(out, &ty.inner);
855 write!(out, " *");
856 }
857 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700858 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700859 _ => write_type(out, ty),
860 }
861}
862
863fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
864 write_indirect_return_type(out, ty);
865 match ty {
866 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700867 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700868 _ => write_space_after_type(out, ty),
869 }
870}
871
872fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400873 match ty {
874 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
875 write_type_space(out, &ty.inner);
876 write!(out, "*");
877 }
David Tolnay4a441222020-01-25 16:24:27 -0800878 Some(Type::Ref(ty)) => {
879 if ty.mutability.is_none() {
880 write!(out, "const ");
881 }
882 write_type(out, &ty.inner);
883 write!(out, " *");
884 }
David Tolnay750755e2020-03-01 13:04:08 -0800885 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700886 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400887 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
888 _ => write_return_type(out, ty),
889 }
890}
891
892fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
893 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700894 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400895 write_type_space(out, &ty.inner);
896 write!(out, "*");
897 }
David Tolnay750755e2020-03-01 13:04:08 -0800898 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700899 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400900 _ => write_type_space(out, &arg.ty),
901 }
902 if types.needs_indirect_abi(&arg.ty) {
903 write!(out, "*");
904 }
905 write!(out, "{}", arg.ident);
906}
907
908fn write_type(out: &mut OutFile, ty: &Type) {
909 match ty {
910 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700911 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400912 None => write!(out, "{}", ident),
913 },
914 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800915 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400916 write_type(out, &ty.inner);
917 write!(out, ">");
918 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700919 Type::RustVec(ty) => {
920 write!(out, "::rust::Vec<");
921 write_type(out, &ty.inner);
922 write!(out, ">");
923 }
David Tolnay7db73692019-10-20 14:51:12 -0400924 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800925 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400926 write_type(out, &ptr.inner);
927 write!(out, ">");
928 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700929 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700930 write!(out, "::std::vector<");
931 write_type(out, &ty.inner);
932 write!(out, ">");
933 }
David Tolnay7db73692019-10-20 14:51:12 -0400934 Type::Ref(r) => {
935 if r.mutability.is_none() {
936 write!(out, "const ");
937 }
938 write_type(out, &r.inner);
939 write!(out, " &");
940 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700941 Type::Slice(_) => {
942 // For now, only U8 slices are supported, which are covered separately below
943 unreachable!()
944 }
David Tolnay7db73692019-10-20 14:51:12 -0400945 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800946 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400947 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700948 Type::SliceRefU8(_) => {
949 write!(out, "::rust::Slice<uint8_t>");
950 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700951 Type::Fn(f) => {
952 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
953 match &f.ret {
954 Some(ret) => write_type(out, ret),
955 None => write!(out, "void"),
956 }
957 write!(out, "(");
958 for (i, arg) in f.args.iter().enumerate() {
959 if i > 0 {
960 write!(out, ", ");
961 }
962 write_type(out, &arg.ty);
963 }
964 write!(out, ")>");
965 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700966 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400967 }
968}
969
David Tolnayf6a89f22020-05-10 23:39:27 -0700970fn write_atom(out: &mut OutFile, atom: Atom) {
971 match atom {
972 Bool => write!(out, "bool"),
973 U8 => write!(out, "uint8_t"),
974 U16 => write!(out, "uint16_t"),
975 U32 => write!(out, "uint32_t"),
976 U64 => write!(out, "uint64_t"),
977 Usize => write!(out, "size_t"),
978 I8 => write!(out, "int8_t"),
979 I16 => write!(out, "int16_t"),
980 I32 => write!(out, "int32_t"),
981 I64 => write!(out, "int64_t"),
982 Isize => write!(out, "::rust::isize"),
983 F32 => write!(out, "float"),
984 F64 => write!(out, "double"),
985 CxxString => write!(out, "::std::string"),
986 RustString => write!(out, "::rust::String"),
987 }
988}
989
David Tolnay7db73692019-10-20 14:51:12 -0400990fn write_type_space(out: &mut OutFile, ty: &Type) {
991 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700992 write_space_after_type(out, ty);
993}
994
995fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400996 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700997 Type::Ident(_)
998 | Type::RustBox(_)
999 | Type::UniquePtr(_)
1000 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001001 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001002 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001003 | Type::SliceRefU8(_)
1004 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001005 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001006 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001007 }
1008}
1009
David Tolnaycd08c442020-04-25 10:16:33 -07001010// Only called for legal referent types of unique_ptr and element types of
1011// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -07001012fn to_typename(namespace: &Namespace, ty: &Type) -> String {
1013 match ty {
1014 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -07001015 let mut path = String::new();
1016 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001017 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -07001018 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -07001019 }
David Tolnaycd08c442020-04-25 10:16:33 -07001020 path += &ident.to_string();
1021 path
David Tolnay2eca4a02020-04-24 19:50:51 -07001022 }
1023 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -07001024 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001025 }
1026}
1027
David Tolnayacdf20a2020-04-25 12:40:53 -07001028// Only called for legal referent types of unique_ptr and element types of
1029// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -07001030fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
1031 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -07001032 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -07001033 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -07001034 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001035 }
1036}
1037
David Tolnay7db73692019-10-20 14:51:12 -04001038fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001039 out.begin_block("extern \"C\"");
1040 for ty in types {
1041 if let Type::RustBox(ty) = ty {
1042 if let Type::Ident(inner) = &ty.inner {
1043 out.next_section();
1044 write_rust_box_extern(out, inner);
1045 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001046 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001047 if let Type::Ident(inner) = &ty.inner {
1048 if Atom::from(inner).is_none() {
1049 out.next_section();
1050 write_rust_vec_extern(out, inner);
1051 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001052 }
David Tolnay7db73692019-10-20 14:51:12 -04001053 } else if let Type::UniquePtr(ptr) = ty {
1054 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001055 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001056 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001057 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001058 }
1059 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001060 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001061 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001062 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001063 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001064 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001065 }
1066 }
1067 }
1068 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001069 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001070
David Tolnay750755e2020-03-01 13:04:08 -08001071 out.begin_block("namespace rust");
David Tolnay591dcb62020-09-01 23:00:38 -07001072 out.begin_block("inline namespace cxxbridge04");
David Tolnay7db73692019-10-20 14:51:12 -04001073 for ty in types {
1074 if let Type::RustBox(ty) = ty {
1075 if let Type::Ident(inner) = &ty.inner {
1076 write_rust_box_impl(out, inner);
1077 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001078 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001079 if let Type::Ident(inner) = &ty.inner {
1080 if Atom::from(inner).is_none() {
1081 write_rust_vec_impl(out, inner);
1082 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001083 }
David Tolnay7db73692019-10-20 14:51:12 -04001084 }
1085 }
David Tolnay591dcb62020-09-01 23:00:38 -07001086 out.end_block("namespace cxxbridge04");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001087 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001088}
1089
1090fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1091 let mut inner = String::new();
1092 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001093 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001094 inner += "::";
1095 }
1096 inner += &ident.to_string();
1097 let instance = inner.replace("::", "$");
1098
David Tolnay591dcb62020-09-01 23:00:38 -07001099 writeln!(out, "#ifndef CXXBRIDGE04_RUST_BOX_{}", instance);
1100 writeln!(out, "#define CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001101 writeln!(
1102 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001103 "void cxxbridge04$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001104 instance, inner,
1105 );
1106 writeln!(
1107 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001108 "void cxxbridge04$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001109 instance, inner,
1110 );
David Tolnay591dcb62020-09-01 23:00:38 -07001111 writeln!(out, "#endif // CXXBRIDGE04_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001112}
1113
David Tolnay6787be62020-04-25 11:01:02 -07001114fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1115 let element = Type::Ident(element.clone());
1116 let inner = to_typename(&out.namespace, &element);
1117 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001118
David Tolnay591dcb62020-09-01 23:00:38 -07001119 writeln!(out, "#ifndef CXXBRIDGE04_RUST_VEC_{}", instance);
1120 writeln!(out, "#define CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001121 writeln!(
1122 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001123 "void cxxbridge04$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001124 instance, inner,
1125 );
1126 writeln!(
1127 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001128 "void cxxbridge04$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001129 instance, inner,
1130 );
1131 writeln!(
1132 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001133 "size_t cxxbridge04$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001134 instance, inner,
1135 );
David Tolnay219c0792020-04-24 20:31:37 -07001136 writeln!(
1137 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001138 "const {} *cxxbridge04$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001139 inner, instance,
1140 );
David Tolnay503d0192020-04-24 22:18:56 -07001141 writeln!(
1142 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001143 "size_t cxxbridge04$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001144 instance,
1145 );
David Tolnay591dcb62020-09-01 23:00:38 -07001146 writeln!(out, "#endif // CXXBRIDGE04_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001147}
1148
David Tolnay7db73692019-10-20 14:51:12 -04001149fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1150 let mut inner = String::new();
1151 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001152 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001153 inner += "::";
1154 }
1155 inner += &ident.to_string();
1156 let instance = inner.replace("::", "$");
1157
1158 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001159 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001160 writeln!(out, " cxxbridge04$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001161 writeln!(out, "}}");
1162
1163 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001164 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001165 writeln!(out, " cxxbridge04$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001166 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001167}
1168
David Tolnay6787be62020-04-25 11:01:02 -07001169fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1170 let element = Type::Ident(element.clone());
1171 let inner = to_typename(&out.namespace, &element);
1172 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001173
Myron Ahneba35cf2020-02-05 19:41:51 +07001174 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001175 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001176 writeln!(out, " cxxbridge04$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001177 writeln!(out, "}}");
1178
1179 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001180 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1181 writeln!(
1182 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001183 " return cxxbridge04$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001184 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001185 );
1186 writeln!(out, "}}");
1187
1188 writeln!(out, "template <>");
1189 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001190 writeln!(out, " return cxxbridge04$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001191 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001192
1193 writeln!(out, "template <>");
1194 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1195 writeln!(
1196 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001197 " return cxxbridge04$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001198 instance,
1199 );
1200 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001201
1202 writeln!(out, "template <>");
1203 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay591dcb62020-09-01 23:00:38 -07001204 writeln!(out, " return cxxbridge04$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001205 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001206}
1207
David Tolnay63da4d32020-04-25 09:41:12 -07001208fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1209 let ty = Type::Ident(ident.clone());
1210 let instance = to_mangled(&out.namespace, &ty);
1211
David Tolnay591dcb62020-09-01 23:00:38 -07001212 writeln!(out, "#ifndef CXXBRIDGE04_UNIQUE_PTR_{}", instance);
1213 writeln!(out, "#define CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001214
1215 write_unique_ptr_common(out, &ty, types);
1216
David Tolnay591dcb62020-09-01 23:00:38 -07001217 writeln!(out, "#endif // CXXBRIDGE04_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001218}
1219
1220// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1221fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001222 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001223 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001224 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001225 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001226
David Tolnay63da4d32020-04-25 09:41:12 -07001227 let can_construct_from_value = match ty {
1228 Type::Ident(ident) => types.structs.contains_key(ident),
1229 _ => false,
1230 };
1231
David Tolnay7db73692019-10-20 14:51:12 -04001232 writeln!(
1233 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001234 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001235 inner,
1236 );
1237 writeln!(
1238 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001239 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001240 inner,
1241 );
1242 writeln!(
1243 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001244 "void cxxbridge04$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001245 instance, inner,
1246 );
David Tolnay7e219b82020-03-01 13:14:51 -08001247 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001248 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001249 if can_construct_from_value {
1250 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001251 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001252 "void cxxbridge04$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001253 instance, inner, inner,
1254 );
David Tolnay63da4d32020-04-25 09:41:12 -07001255 writeln!(
1256 out,
1257 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1258 inner, inner,
1259 );
1260 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001261 }
David Tolnay7db73692019-10-20 14:51:12 -04001262 writeln!(
1263 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001264 "void cxxbridge04$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001265 instance, inner, inner,
1266 );
David Tolnay7e219b82020-03-01 13:14:51 -08001267 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001268 writeln!(out, "}}");
1269 writeln!(
1270 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001271 "const {} *cxxbridge04$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001272 inner, instance, inner,
1273 );
1274 writeln!(out, " return ptr.get();");
1275 writeln!(out, "}}");
1276 writeln!(
1277 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001278 "{} *cxxbridge04$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001279 inner, instance, inner,
1280 );
1281 writeln!(out, " return ptr.release();");
1282 writeln!(out, "}}");
1283 writeln!(
1284 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001285 "void cxxbridge04$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001286 instance, inner,
1287 );
1288 writeln!(out, " ptr->~unique_ptr();");
1289 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001290}
Myron Ahneba35cf2020-02-05 19:41:51 +07001291
David Tolnay92105da2020-04-25 11:15:31 -07001292fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001293 let element = Type::Ident(element.clone());
1294 let inner = to_typename(&out.namespace, &element);
1295 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001296
David Tolnay591dcb62020-09-01 23:00:38 -07001297 writeln!(out, "#ifndef CXXBRIDGE04_VECTOR_{}", instance);
1298 writeln!(out, "#define CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001299 writeln!(
1300 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001301 "size_t cxxbridge04$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001302 instance, inner,
1303 );
1304 writeln!(out, " return s.size();");
1305 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001306 writeln!(
1307 out,
David Tolnay591dcb62020-09-01 23:00:38 -07001308 "const {} *cxxbridge04$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001309 inner, instance, inner,
1310 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001311 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001312 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001313
1314 write_unique_ptr_common(out, vector_ty, types);
1315
David Tolnay591dcb62020-09-01 23:00:38 -07001316 writeln!(out, "#endif // CXXBRIDGE04_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001317}