blob: 46b6151c1a166f175538a77a72a730a70ef60fa5 [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
20 if header {
21 writeln!(out, "#pragma once");
22 }
23
David Tolnay33d30292020-03-18 18:02:02 -070024 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070027 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070035 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040036 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070045 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040046 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
65 write_struct(out, strct);
66 }
Joel Galensonc03402a2020-04-23 17:31:09 -070067 Api::Enum(enm) => {
68 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070069 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070070 check_enum(out, enm);
71 } else {
72 write_enum(out, enm);
73 }
Joel Galensonc03402a2020-04-23 17:31:09 -070074 }
David Tolnayc1fe0052020-04-17 15:15:06 -070075 Api::RustType(ety) => {
76 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070077 out.next_section();
78 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070079 }
David Tolnayc1fe0052020-04-17 15:15:06 -070080 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070081 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040082 }
83 }
84
85 if !header {
86 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070087 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040088 for api in apis {
89 let (efn, write): (_, fn(_, _, _)) = match api {
90 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
91 Api::RustFunction(efn) => (efn, write_rust_function_decl),
92 _ => continue,
93 };
94 out.next_section();
95 write(out, efn, types);
96 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080097 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040098 }
99
100 for api in apis {
101 if let Api::RustFunction(efn) = api {
102 out.next_section();
103 write_rust_function_shim(out, efn, types);
104 }
105 }
106
107 out.next_section();
108 for name in namespace.iter().rev() {
109 writeln!(out, "}} // namespace {}", name);
110 }
111
112 if !header {
113 out.next_section();
114 write_generic_instantiations(out, types);
115 }
116
David Tolnay9c68b1a2020-03-06 11:12:55 -0800117 out.prepend(out.include.to_string());
118
David Tolnay7db73692019-10-20 14:51:12 -0400119 out_file
120}
121
122fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400123 for ty in types {
124 match ty {
125 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700126 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
127 | Some(I64) => out.include.cstdint = true,
128 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800129 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700130 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400131 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800132 Type::RustBox(_) => out.include.type_traits = true,
133 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700134 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700135 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700136 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400137 }
138 }
David Tolnay7db73692019-10-20 14:51:12 -0400139}
140
David Tolnayf51447e2020-03-06 14:14:27 -0800141fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 let mut needs_rust_string = false;
143 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700144 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400145 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700146 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700147 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700148 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400149 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700150 match ty {
151 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700152 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700153 out.include.type_traits = true;
154 needs_rust_box = true;
155 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700156 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700157 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700158 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700159 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700160 needs_rust_vec = true;
161 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700162 Type::Str(_) => {
163 out.include.cstdint = true;
164 out.include.string = true;
165 needs_rust_str = true;
166 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700167 Type::Fn(_) => {
168 needs_rust_fn = true;
169 }
David Tolnay4770b472020-04-14 16:32:59 -0700170 Type::Slice(_) | Type::SliceRefU8(_) => {
171 needs_rust_slice = true;
172 }
David Tolnay7c295462020-04-25 12:45:07 -0700173 ty if ty == Isize => {
174 out.include.base_tsd = true;
175 needs_rust_isize = true;
176 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700177 ty if ty == RustString => {
178 out.include.array = true;
179 out.include.cstdint = true;
180 out.include.string = true;
181 needs_rust_string = true;
182 }
183 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400184 }
185 }
186
David Tolnayb7a7cb62020-03-17 21:18:40 -0700187 let mut needs_rust_error = false;
188 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800189 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800190 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700191 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800192 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 match api {
194 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700195 if efn.throws {
196 needs_trycatch = true;
197 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700198 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700199 let bitcopy = match arg.ty {
200 Type::RustVec(_) => true,
201 _ => arg.ty == RustString,
202 };
203 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700204 needs_unsafe_bitcopy = true;
205 break;
206 }
David Tolnay09011c32020-03-06 14:40:28 -0800207 }
208 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700209 Api::RustFunction(efn) if !out.header => {
210 if efn.throws {
211 out.include.exception = true;
212 needs_rust_error = true;
213 }
214 for arg in &efn.args {
215 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
216 needs_manually_drop = true;
217 break;
218 }
219 }
220 if let Some(ret) = &efn.ret {
221 if types.needs_indirect_abi(ret) {
222 needs_maybe_uninit = true;
223 }
David Tolnayf51447e2020-03-06 14:14:27 -0800224 }
225 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800227 }
228 }
229
David Tolnay750755e2020-03-01 13:04:08 -0800230 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700231 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800232
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 if needs_rust_string
234 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700235 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700236 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700237 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700238 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700240 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 || needs_unsafe_bitcopy
242 || needs_manually_drop
243 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700244 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 {
David Tolnay736cbca2020-03-11 16:49:18 -0700246 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800247 }
248
David Tolnay32ee9d32020-05-12 20:01:09 -0700249 if needs_rust_string || needs_rust_vec {
David Tolnayd1402742020-03-25 22:21:42 -0700250 out.next_section();
251 writeln!(out, "struct unsafe_bitcopy_t;");
252 }
253
David Tolnaye54f3382020-05-12 19:58:36 -0700254 include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
255 include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
256 include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
257 include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
258 include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
259 include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
260 include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
261 include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
262 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800263
264 if needs_manually_drop {
265 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700266 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800267 writeln!(out, "template <typename T>");
268 writeln!(out, "union ManuallyDrop {{");
269 writeln!(out, " T value;");
270 writeln!(
271 out,
272 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
273 );
274 writeln!(out, " ~ManuallyDrop() {{}}");
275 writeln!(out, "}};");
276 }
277
David Tolnay09011c32020-03-06 14:40:28 -0800278 if needs_maybe_uninit {
279 out.next_section();
280 writeln!(out, "template <typename T>");
281 writeln!(out, "union MaybeUninit {{");
282 writeln!(out, " T value;");
283 writeln!(out, " MaybeUninit() {{}}");
284 writeln!(out, " ~MaybeUninit() {{}}");
285 writeln!(out, "}};");
286 }
287
David Tolnay69601622020-04-29 18:48:36 -0700288 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700289
David Tolnay5d121442020-03-17 22:14:40 -0700290 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700291 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700292 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700293 out.include.type_traits = true;
294 out.include.utility = true;
295 writeln!(out, "class missing {{}};");
296 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700297 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700298 writeln!(out, "template <typename Try, typename Fail>");
299 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700300 writeln!(
301 out,
David Tolnay04722332020-03-18 11:31:54 -0700302 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700303 );
David Tolnay04722332020-03-18 11:31:54 -0700304 writeln!(out, " missing>::value>::type");
305 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700306 writeln!(out, " func();");
307 writeln!(out, "}} catch (const ::std::exception &e) {{");
308 writeln!(out, " fail(e.what());");
309 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700310 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700311 }
312
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800313 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400314}
315
316fn write_struct(out: &mut OutFile, strct: &Struct) {
317 for line in strct.doc.to_string().lines() {
318 writeln!(out, "//{}", line);
319 }
320 writeln!(out, "struct {} final {{", strct.ident);
321 for field in &strct.fields {
322 write!(out, " ");
323 write_type_space(out, &field.ty);
324 writeln!(out, "{};", field.ident);
325 }
326 writeln!(out, "}};");
327}
328
329fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
330 writeln!(out, "struct {};", ident);
331}
332
David Tolnay8861bee2020-01-20 18:39:24 -0800333fn write_struct_using(out: &mut OutFile, ident: &Ident) {
334 writeln!(out, "using {} = {};", ident, ident);
335}
336
David Tolnayc1fe0052020-04-17 15:15:06 -0700337fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700338 for line in ety.doc.to_string().lines() {
339 writeln!(out, "//{}", line);
340 }
341 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700342 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700343 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700344 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700345 write!(out, " ");
346 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700347 let local_name = method.ident.to_string();
348 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700349 writeln!(out, ";");
350 }
351 writeln!(out, "}};");
352}
353
Joel Galensonc03402a2020-04-23 17:31:09 -0700354fn write_enum(out: &mut OutFile, enm: &Enum) {
355 for line in enm.doc.to_string().lines() {
356 writeln!(out, "//{}", line);
357 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700358 write!(out, "enum class {} : ", enm.ident);
359 write_atom(out, enm.repr);
360 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700361 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700362 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700363 }
364 writeln!(out, "}};");
365}
366
Joel Galenson905eb2e2020-05-04 14:58:14 -0700367fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700368 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
369 write_atom(out, enm.repr);
370 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700371 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700372 write!(out, "static_assert(static_cast<");
373 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700374 writeln!(
375 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700376 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700377 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700378 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700379 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700380}
381
David Tolnayebef4a22020-03-17 15:33:47 -0700382fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
383 let mut has_cxx_throws = false;
384 for api in apis {
385 if let Api::CxxFunction(efn) = api {
386 if efn.throws {
387 has_cxx_throws = true;
388 break;
389 }
390 }
391 }
392
393 if has_cxx_throws {
394 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700395 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700396 out,
David Tolnay69601622020-04-29 18:48:36 -0700397 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700398 );
399 }
400}
401
David Tolnay7db73692019-10-20 14:51:12 -0400402fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700403 if efn.throws {
404 write!(out, "::rust::Str::Repr ");
405 } else {
David Tolnay99642622020-03-25 13:07:35 -0700406 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700407 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700408 let mangled = mangle::extern_fn(&out.namespace, efn);
409 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700410 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700411 if receiver.mutability.is_none() {
412 write!(out, "const ");
413 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700414 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700415 }
David Tolnay7db73692019-10-20 14:51:12 -0400416 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700417 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400418 write!(out, ", ");
419 }
David Tolnaya46a2372020-03-06 10:03:48 -0800420 if arg.ty == RustString {
421 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700422 } else if let Type::RustVec(_) = arg.ty {
423 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800424 }
David Tolnay7db73692019-10-20 14:51:12 -0400425 write_extern_arg(out, arg, types);
426 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700427 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400428 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700429 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400430 write!(out, ", ");
431 }
David Tolnay99642622020-03-25 13:07:35 -0700432 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400433 write!(out, "*return$");
434 }
435 writeln!(out, ") noexcept {{");
436 write!(out, " ");
437 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700438 match &efn.receiver {
439 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700440 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700441 }
David Tolnay7db73692019-10-20 14:51:12 -0400442 for (i, arg) in efn.args.iter().enumerate() {
443 if i > 0 {
444 write!(out, ", ");
445 }
446 write_type(out, &arg.ty);
447 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700448 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700449 if let Some(receiver) = &efn.receiver {
450 if receiver.mutability.is_none() {
451 write!(out, " const");
452 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700453 }
454 write!(out, " = ");
455 match &efn.receiver {
456 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700457 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700458 }
459 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400460 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700461 if efn.throws {
462 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700463 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700464 writeln!(out, " [&] {{");
465 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700466 }
David Tolnay7db73692019-10-20 14:51:12 -0400467 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700468 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400469 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700470 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400471 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700472 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400473 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700474 }
475 match &efn.ret {
476 Some(Type::Ref(_)) => write!(out, "&"),
477 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700478 Some(Type::SliceRefU8(_)) if !indirect_return => {
479 write!(out, "::rust::Slice<uint8_t>::Repr(")
480 }
David Tolnay99642622020-03-25 13:07:35 -0700481 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400482 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700483 match &efn.receiver {
484 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700485 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700486 }
David Tolnay7db73692019-10-20 14:51:12 -0400487 for (i, arg) in efn.args.iter().enumerate() {
488 if i > 0 {
489 write!(out, ", ");
490 }
491 if let Type::RustBox(_) = &arg.ty {
492 write_type(out, &arg.ty);
493 write!(out, "::from_raw({})", arg.ident);
494 } else if let Type::UniquePtr(_) = &arg.ty {
495 write_type(out, &arg.ty);
496 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800497 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800498 write!(
499 out,
500 "::rust::String(::rust::unsafe_bitcopy, *{})",
501 arg.ident,
502 );
David Tolnay313b10e2020-04-25 16:30:51 -0700503 } else if let Type::RustVec(_) = arg.ty {
504 write_type(out, &arg.ty);
505 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400506 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700507 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800508 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400509 } else {
510 write!(out, "{}", arg.ident);
511 }
512 }
513 write!(out, ")");
514 match &efn.ret {
515 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
516 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700517 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400518 _ => {}
519 }
520 if indirect_return {
521 write!(out, ")");
522 }
523 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700524 if efn.throws {
525 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700526 writeln!(out, " throw$.ptr = nullptr;");
527 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700528 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700529 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700530 writeln!(
531 out,
David Tolnay69601622020-04-29 18:48:36 -0700532 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700533 );
David Tolnay5d121442020-03-17 22:14:40 -0700534 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700535 writeln!(out, " return throw$;");
536 }
David Tolnay7db73692019-10-20 14:51:12 -0400537 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700538 for arg in &efn.args {
539 if let Type::Fn(f) = &arg.ty {
540 let var = &arg.ident;
541 write_function_pointer_trampoline(out, efn, var, f, types);
542 }
543 }
544}
545
546fn write_function_pointer_trampoline(
547 out: &mut OutFile,
548 efn: &ExternFn,
549 var: &Ident,
550 f: &Signature,
551 types: &Types,
552) {
553 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700554 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700555 let indirect_call = true;
556 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
557
558 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700559 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700560 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400561}
562
563fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700564 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700565 let indirect_call = false;
566 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
567}
568
569fn write_rust_function_decl_impl(
570 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700571 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700572 sig: &Signature,
573 types: &Types,
574 indirect_call: bool,
575) {
576 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700577 write!(out, "::rust::Str::Repr ");
578 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700579 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700580 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700581 write!(out, "{}(", link_name);
582 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700583 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700584 if receiver.mutability.is_none() {
585 write!(out, "const ");
586 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700587 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700588 needs_comma = true;
589 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700590 for arg in &sig.args {
591 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400592 write!(out, ", ");
593 }
594 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700595 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400596 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700597 if indirect_return(sig, types) {
598 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400599 write!(out, ", ");
600 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700601 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400602 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700603 needs_comma = true;
604 }
605 if indirect_call {
606 if needs_comma {
607 write!(out, ", ");
608 }
609 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400610 }
611 writeln!(out, ") noexcept;");
612}
613
614fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400615 for line in efn.doc.to_string().lines() {
616 writeln!(out, "//{}", line);
617 }
David Tolnaya73853b2020-04-20 01:19:56 -0700618 let local_name = match &efn.sig.receiver {
619 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700620 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700621 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700622 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700623 let indirect_call = false;
624 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
625}
626
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700627fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700629 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700630 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700631 indirect_call: bool,
632) {
633 write_return_type(out, &sig.ret);
634 write!(out, "{}(", local_name);
635 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400636 if i > 0 {
637 write!(out, ", ");
638 }
639 write_type_space(out, &arg.ty);
640 write!(out, "{}", arg.ident);
641 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700642 if indirect_call {
643 if !sig.args.is_empty() {
644 write!(out, ", ");
645 }
646 write!(out, "void *extern$");
647 }
David Tolnay1e548172020-03-16 13:37:09 -0700648 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700649 if let Some(receiver) = &sig.receiver {
650 if receiver.mutability.is_none() {
651 write!(out, " const");
652 }
653 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700654 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700655 write!(out, " noexcept");
656 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700657}
658
659fn write_rust_function_shim_impl(
660 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700661 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700662 sig: &Signature,
663 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700664 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700665 indirect_call: bool,
666) {
667 if out.header && sig.receiver.is_some() {
668 // We've already defined this inside the struct.
669 return;
670 }
David Tolnaya73853b2020-04-20 01:19:56 -0700671 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400672 if out.header {
673 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700674 return;
David Tolnay7db73692019-10-20 14:51:12 -0400675 }
David Tolnay439cde22020-04-20 00:46:25 -0700676 writeln!(out, " {{");
677 for arg in &sig.args {
678 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
679 out.include.utility = true;
680 write!(out, " ::rust::ManuallyDrop<");
681 write_type(out, &arg.ty);
682 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
683 }
684 }
685 write!(out, " ");
686 let indirect_return = indirect_return(sig, types);
687 if indirect_return {
688 write!(out, "::rust::MaybeUninit<");
689 write_type(out, sig.ret.as_ref().unwrap());
690 writeln!(out, "> return$;");
691 write!(out, " ");
692 } else if let Some(ret) = &sig.ret {
693 write!(out, "return ");
694 match ret {
695 Type::RustBox(_) => {
696 write_type(out, ret);
697 write!(out, "::from_raw(");
698 }
699 Type::UniquePtr(_) => {
700 write_type(out, ret);
701 write!(out, "(");
702 }
703 Type::Ref(_) => write!(out, "*"),
704 _ => {}
705 }
706 }
707 if sig.throws {
708 write!(out, "::rust::Str::Repr error$ = ");
709 }
710 write!(out, "{}(", invoke);
711 if sig.receiver.is_some() {
712 write!(out, "*this");
713 }
714 for (i, arg) in sig.args.iter().enumerate() {
715 if i > 0 || sig.receiver.is_some() {
716 write!(out, ", ");
717 }
718 match &arg.ty {
719 Type::Str(_) => write!(out, "::rust::Str::Repr("),
720 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
721 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
722 _ => {}
723 }
724 write!(out, "{}", arg.ident);
725 match &arg.ty {
726 Type::RustBox(_) => write!(out, ".into_raw()"),
727 Type::UniquePtr(_) => write!(out, ".release()"),
728 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
729 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
730 _ => {}
731 }
732 }
733 if indirect_return {
734 if !sig.args.is_empty() {
735 write!(out, ", ");
736 }
737 write!(out, "&return$.value");
738 }
739 if indirect_call {
740 if !sig.args.is_empty() || indirect_return {
741 write!(out, ", ");
742 }
743 write!(out, "extern$");
744 }
745 write!(out, ")");
746 if let Some(ret) = &sig.ret {
747 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
748 write!(out, ")");
749 }
750 }
751 writeln!(out, ";");
752 if sig.throws {
753 writeln!(out, " if (error$.ptr) {{");
754 writeln!(out, " throw ::rust::Error(error$);");
755 writeln!(out, " }}");
756 }
757 if indirect_return {
758 out.include.utility = true;
759 writeln!(out, " return ::std::move(return$.value);");
760 }
761 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400762}
763
764fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
765 match ty {
766 None => write!(out, "void "),
767 Some(ty) => write_type_space(out, ty),
768 }
769}
770
David Tolnay75dca2e2020-03-25 20:17:52 -0700771fn indirect_return(sig: &Signature, types: &Types) -> bool {
772 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700773 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700774 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700775}
776
David Tolnay99642622020-03-25 13:07:35 -0700777fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
778 match ty {
779 Type::RustBox(ty) | Type::UniquePtr(ty) => {
780 write_type_space(out, &ty.inner);
781 write!(out, "*");
782 }
783 Type::Ref(ty) => {
784 if ty.mutability.is_none() {
785 write!(out, "const ");
786 }
787 write_type(out, &ty.inner);
788 write!(out, " *");
789 }
790 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700791 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700792 _ => write_type(out, ty),
793 }
794}
795
796fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
797 write_indirect_return_type(out, ty);
798 match ty {
799 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700800 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700801 _ => write_space_after_type(out, ty),
802 }
803}
804
805fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400806 match ty {
807 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
808 write_type_space(out, &ty.inner);
809 write!(out, "*");
810 }
David Tolnay4a441222020-01-25 16:24:27 -0800811 Some(Type::Ref(ty)) => {
812 if ty.mutability.is_none() {
813 write!(out, "const ");
814 }
815 write_type(out, &ty.inner);
816 write!(out, " *");
817 }
David Tolnay750755e2020-03-01 13:04:08 -0800818 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700819 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400820 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
821 _ => write_return_type(out, ty),
822 }
823}
824
825fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
826 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700827 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400828 write_type_space(out, &ty.inner);
829 write!(out, "*");
830 }
David Tolnay750755e2020-03-01 13:04:08 -0800831 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700832 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400833 _ => write_type_space(out, &arg.ty),
834 }
835 if types.needs_indirect_abi(&arg.ty) {
836 write!(out, "*");
837 }
838 write!(out, "{}", arg.ident);
839}
840
841fn write_type(out: &mut OutFile, ty: &Type) {
842 match ty {
843 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700844 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400845 None => write!(out, "{}", ident),
846 },
847 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800848 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400849 write_type(out, &ty.inner);
850 write!(out, ">");
851 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700852 Type::RustVec(ty) => {
853 write!(out, "::rust::Vec<");
854 write_type(out, &ty.inner);
855 write!(out, ">");
856 }
David Tolnay7db73692019-10-20 14:51:12 -0400857 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800858 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400859 write_type(out, &ptr.inner);
860 write!(out, ">");
861 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700862 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700863 write!(out, "::std::vector<");
864 write_type(out, &ty.inner);
865 write!(out, ">");
866 }
David Tolnay7db73692019-10-20 14:51:12 -0400867 Type::Ref(r) => {
868 if r.mutability.is_none() {
869 write!(out, "const ");
870 }
871 write_type(out, &r.inner);
872 write!(out, " &");
873 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700874 Type::Slice(_) => {
875 // For now, only U8 slices are supported, which are covered separately below
876 unreachable!()
877 }
David Tolnay7db73692019-10-20 14:51:12 -0400878 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800879 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400880 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700881 Type::SliceRefU8(_) => {
882 write!(out, "::rust::Slice<uint8_t>");
883 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700884 Type::Fn(f) => {
885 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
886 match &f.ret {
887 Some(ret) => write_type(out, ret),
888 None => write!(out, "void"),
889 }
890 write!(out, "(");
891 for (i, arg) in f.args.iter().enumerate() {
892 if i > 0 {
893 write!(out, ", ");
894 }
895 write_type(out, &arg.ty);
896 }
897 write!(out, ")>");
898 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700899 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400900 }
901}
902
David Tolnayf6a89f22020-05-10 23:39:27 -0700903fn write_atom(out: &mut OutFile, atom: Atom) {
904 match atom {
905 Bool => write!(out, "bool"),
906 U8 => write!(out, "uint8_t"),
907 U16 => write!(out, "uint16_t"),
908 U32 => write!(out, "uint32_t"),
909 U64 => write!(out, "uint64_t"),
910 Usize => write!(out, "size_t"),
911 I8 => write!(out, "int8_t"),
912 I16 => write!(out, "int16_t"),
913 I32 => write!(out, "int32_t"),
914 I64 => write!(out, "int64_t"),
915 Isize => write!(out, "::rust::isize"),
916 F32 => write!(out, "float"),
917 F64 => write!(out, "double"),
918 CxxString => write!(out, "::std::string"),
919 RustString => write!(out, "::rust::String"),
920 }
921}
922
David Tolnay7db73692019-10-20 14:51:12 -0400923fn write_type_space(out: &mut OutFile, ty: &Type) {
924 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700925 write_space_after_type(out, ty);
926}
927
928fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400929 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700930 Type::Ident(_)
931 | Type::RustBox(_)
932 | Type::UniquePtr(_)
933 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700934 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700935 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700936 | Type::SliceRefU8(_)
937 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400938 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700939 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400940 }
941}
942
David Tolnaycd08c442020-04-25 10:16:33 -0700943// Only called for legal referent types of unique_ptr and element types of
944// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700945fn to_typename(namespace: &Namespace, ty: &Type) -> String {
946 match ty {
947 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700948 let mut path = String::new();
949 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700950 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700951 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700952 }
David Tolnaycd08c442020-04-25 10:16:33 -0700953 path += &ident.to_string();
954 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700955 }
956 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700957 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700958 }
959}
960
David Tolnayacdf20a2020-04-25 12:40:53 -0700961// Only called for legal referent types of unique_ptr and element types of
962// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700963fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
964 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700965 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700966 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700967 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700968 }
969}
970
David Tolnay7db73692019-10-20 14:51:12 -0400971fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400972 out.begin_block("extern \"C\"");
973 for ty in types {
974 if let Type::RustBox(ty) = ty {
975 if let Type::Ident(inner) = &ty.inner {
976 out.next_section();
977 write_rust_box_extern(out, inner);
978 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700979 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700980 if let Type::Ident(inner) = &ty.inner {
981 if Atom::from(inner).is_none() {
982 out.next_section();
983 write_rust_vec_extern(out, inner);
984 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700985 }
David Tolnay7db73692019-10-20 14:51:12 -0400986 } else if let Type::UniquePtr(ptr) = ty {
987 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -0700988 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -0400989 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -0700990 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +0700991 }
992 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700993 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +0700994 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -0700995 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700996 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -0700997 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400998 }
999 }
1000 }
1001 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001002 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001003
David Tolnay750755e2020-03-01 13:04:08 -08001004 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001005 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001006 for ty in types {
1007 if let Type::RustBox(ty) = ty {
1008 if let Type::Ident(inner) = &ty.inner {
1009 write_rust_box_impl(out, inner);
1010 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001011 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001012 if let Type::Ident(inner) = &ty.inner {
1013 if Atom::from(inner).is_none() {
1014 write_rust_vec_impl(out, inner);
1015 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001016 }
David Tolnay7db73692019-10-20 14:51:12 -04001017 }
1018 }
David Tolnay69601622020-04-29 18:48:36 -07001019 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001020 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001021}
1022
1023fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1024 let mut inner = String::new();
1025 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001026 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001027 inner += "::";
1028 }
1029 inner += &ident.to_string();
1030 let instance = inner.replace("::", "$");
1031
David Tolnay69601622020-04-29 18:48:36 -07001032 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1033 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001034 writeln!(
1035 out,
David Tolnay69601622020-04-29 18:48:36 -07001036 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001037 instance, inner,
1038 );
1039 writeln!(
1040 out,
David Tolnay69601622020-04-29 18:48:36 -07001041 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001042 instance, inner,
1043 );
David Tolnay69601622020-04-29 18:48:36 -07001044 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001045}
1046
David Tolnay6787be62020-04-25 11:01:02 -07001047fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1048 let element = Type::Ident(element.clone());
1049 let inner = to_typename(&out.namespace, &element);
1050 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001051
David Tolnay69601622020-04-29 18:48:36 -07001052 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1053 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001054 writeln!(
1055 out,
David Tolnay69601622020-04-29 18:48:36 -07001056 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001057 instance, inner,
1058 );
1059 writeln!(
1060 out,
David Tolnay69601622020-04-29 18:48:36 -07001061 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001062 instance, inner,
1063 );
1064 writeln!(
1065 out,
David Tolnay69601622020-04-29 18:48:36 -07001066 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001067 instance, inner,
1068 );
David Tolnay219c0792020-04-24 20:31:37 -07001069 writeln!(
1070 out,
David Tolnay69601622020-04-29 18:48:36 -07001071 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001072 inner, instance,
1073 );
David Tolnay503d0192020-04-24 22:18:56 -07001074 writeln!(
1075 out,
David Tolnay69601622020-04-29 18:48:36 -07001076 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001077 instance,
1078 );
David Tolnay69601622020-04-29 18:48:36 -07001079 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001080}
1081
David Tolnay7db73692019-10-20 14:51:12 -04001082fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1083 let mut inner = String::new();
1084 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001085 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001086 inner += "::";
1087 }
1088 inner += &ident.to_string();
1089 let instance = inner.replace("::", "$");
1090
1091 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001092 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001093 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001094 writeln!(out, "}}");
1095
1096 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001097 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001098 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001099 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001100}
1101
David Tolnay6787be62020-04-25 11:01:02 -07001102fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1103 let element = Type::Ident(element.clone());
1104 let inner = to_typename(&out.namespace, &element);
1105 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001106
Myron Ahneba35cf2020-02-05 19:41:51 +07001107 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001108 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001109 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001110 writeln!(out, "}}");
1111
1112 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001113 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1114 writeln!(
1115 out,
David Tolnay69601622020-04-29 18:48:36 -07001116 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001117 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001118 );
1119 writeln!(out, "}}");
1120
1121 writeln!(out, "template <>");
1122 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001123 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001124 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001125
1126 writeln!(out, "template <>");
1127 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1128 writeln!(
1129 out,
David Tolnay69601622020-04-29 18:48:36 -07001130 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001131 instance,
1132 );
1133 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001134
1135 writeln!(out, "template <>");
1136 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001137 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001138 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001139}
1140
David Tolnay63da4d32020-04-25 09:41:12 -07001141fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1142 let ty = Type::Ident(ident.clone());
1143 let instance = to_mangled(&out.namespace, &ty);
1144
David Tolnay69601622020-04-29 18:48:36 -07001145 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1146 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001147
1148 write_unique_ptr_common(out, &ty, types);
1149
David Tolnay69601622020-04-29 18:48:36 -07001150 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001151}
1152
1153// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1154fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001155 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001156 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001157 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001158 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001159
David Tolnay63da4d32020-04-25 09:41:12 -07001160 let can_construct_from_value = match ty {
1161 Type::Ident(ident) => types.structs.contains_key(ident),
1162 _ => false,
1163 };
1164
David Tolnay7db73692019-10-20 14:51:12 -04001165 writeln!(
1166 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001167 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001168 inner,
1169 );
1170 writeln!(
1171 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001172 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001173 inner,
1174 );
1175 writeln!(
1176 out,
David Tolnay69601622020-04-29 18:48:36 -07001177 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001178 instance, inner,
1179 );
David Tolnay7e219b82020-03-01 13:14:51 -08001180 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001181 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001182 if can_construct_from_value {
1183 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001184 out,
David Tolnay69601622020-04-29 18:48:36 -07001185 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001186 instance, inner, inner,
1187 );
David Tolnay63da4d32020-04-25 09:41:12 -07001188 writeln!(
1189 out,
1190 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1191 inner, inner,
1192 );
1193 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001194 }
David Tolnay7db73692019-10-20 14:51:12 -04001195 writeln!(
1196 out,
David Tolnay69601622020-04-29 18:48:36 -07001197 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001198 instance, inner, inner,
1199 );
David Tolnay7e219b82020-03-01 13:14:51 -08001200 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001201 writeln!(out, "}}");
1202 writeln!(
1203 out,
David Tolnay69601622020-04-29 18:48:36 -07001204 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001205 inner, instance, inner,
1206 );
1207 writeln!(out, " return ptr.get();");
1208 writeln!(out, "}}");
1209 writeln!(
1210 out,
David Tolnay69601622020-04-29 18:48:36 -07001211 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001212 inner, instance, inner,
1213 );
1214 writeln!(out, " return ptr.release();");
1215 writeln!(out, "}}");
1216 writeln!(
1217 out,
David Tolnay69601622020-04-29 18:48:36 -07001218 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001219 instance, inner,
1220 );
1221 writeln!(out, " ptr->~unique_ptr();");
1222 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001223}
Myron Ahneba35cf2020-02-05 19:41:51 +07001224
David Tolnay92105da2020-04-25 11:15:31 -07001225fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001226 let element = Type::Ident(element.clone());
1227 let inner = to_typename(&out.namespace, &element);
1228 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001229
David Tolnay69601622020-04-29 18:48:36 -07001230 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1231 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001232 writeln!(
1233 out,
David Tolnay69601622020-04-29 18:48:36 -07001234 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001235 instance, inner,
1236 );
1237 writeln!(out, " return s.size();");
1238 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001239 writeln!(
1240 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001241 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001242 inner, instance, inner,
1243 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001244 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001245 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001246
1247 write_unique_ptr_common(out, vector_ty, types);
1248
David Tolnay69601622020-04-29 18:48:36 -07001249 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001250}