blob: d5eae90a6e35093fa516a4f5171b1f96dbcd63cc [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(_) => {
152 out.include.type_traits = true;
153 needs_rust_box = true;
154 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700155 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700156 out.include.array = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700157 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700158 needs_rust_vec = true;
159 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700160 Type::Str(_) => {
161 out.include.cstdint = true;
162 out.include.string = true;
163 needs_rust_str = true;
164 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700165 Type::Fn(_) => {
166 needs_rust_fn = true;
167 }
David Tolnay4770b472020-04-14 16:32:59 -0700168 Type::Slice(_) | Type::SliceRefU8(_) => {
169 needs_rust_slice = true;
170 }
David Tolnay7c295462020-04-25 12:45:07 -0700171 ty if ty == Isize => {
172 out.include.base_tsd = true;
173 needs_rust_isize = true;
174 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700175 ty if ty == RustString => {
176 out.include.array = true;
177 out.include.cstdint = true;
178 out.include.string = true;
179 needs_rust_string = true;
180 }
181 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400182 }
183 }
184
David Tolnayb7a7cb62020-03-17 21:18:40 -0700185 let mut needs_rust_error = false;
186 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800187 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800188 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700189 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800190 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 match api {
192 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700193 if efn.throws {
194 needs_trycatch = true;
195 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700196 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700197 let bitcopy = match arg.ty {
198 Type::RustVec(_) => true,
199 _ => arg.ty == RustString,
200 };
201 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700202 needs_unsafe_bitcopy = true;
203 break;
204 }
David Tolnay09011c32020-03-06 14:40:28 -0800205 }
206 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 Api::RustFunction(efn) if !out.header => {
208 if efn.throws {
209 out.include.exception = true;
210 needs_rust_error = true;
211 }
212 for arg in &efn.args {
213 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
214 needs_manually_drop = true;
215 break;
216 }
217 }
218 if let Some(ret) = &efn.ret {
219 if types.needs_indirect_abi(ret) {
220 needs_maybe_uninit = true;
221 }
David Tolnayf51447e2020-03-06 14:14:27 -0800222 }
223 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700224 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800225 }
226 }
227
David Tolnay750755e2020-03-01 13:04:08 -0800228 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700229 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800230
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 if needs_rust_string
232 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700233 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700234 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700235 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700236 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700238 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 || needs_unsafe_bitcopy
240 || needs_manually_drop
241 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700242 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 {
David Tolnay736cbca2020-03-11 16:49:18 -0700244 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800245 }
246
David Tolnayd1402742020-03-25 22:21:42 -0700247 if needs_rust_string {
248 out.next_section();
249 writeln!(out, "struct unsafe_bitcopy_t;");
250 }
251
David Tolnay69601622020-04-29 18:48:36 -0700252 write_header_section(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
253 write_header_section(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
254 write_header_section(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
255 write_header_section(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
256 write_header_section(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
257 write_header_section(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
258 write_header_section(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
259 write_header_section(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
260 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800261
262 if needs_manually_drop {
263 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700264 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800265 writeln!(out, "template <typename T>");
266 writeln!(out, "union ManuallyDrop {{");
267 writeln!(out, " T value;");
268 writeln!(
269 out,
270 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
271 );
272 writeln!(out, " ~ManuallyDrop() {{}}");
273 writeln!(out, "}};");
274 }
275
David Tolnay09011c32020-03-06 14:40:28 -0800276 if needs_maybe_uninit {
277 out.next_section();
278 writeln!(out, "template <typename T>");
279 writeln!(out, "union MaybeUninit {{");
280 writeln!(out, " T value;");
281 writeln!(out, " MaybeUninit() {{}}");
282 writeln!(out, " ~MaybeUninit() {{}}");
283 writeln!(out, "}};");
284 }
285
David Tolnay69601622020-04-29 18:48:36 -0700286 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700287
David Tolnay5d121442020-03-17 22:14:40 -0700288 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700289 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700290 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700291 out.include.type_traits = true;
292 out.include.utility = true;
293 writeln!(out, "class missing {{}};");
294 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700295 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700296 writeln!(out, "template <typename Try, typename Fail>");
297 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700298 writeln!(
299 out,
David Tolnay04722332020-03-18 11:31:54 -0700300 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700301 );
David Tolnay04722332020-03-18 11:31:54 -0700302 writeln!(out, " missing>::value>::type");
303 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700304 writeln!(out, " func();");
305 writeln!(out, "}} catch (const ::std::exception &e) {{");
306 writeln!(out, " fail(e.what());");
307 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700308 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700309 }
310
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800311 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400312}
313
David Tolnayb7a7cb62020-03-17 21:18:40 -0700314fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700315 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700316 if needed {
317 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700318 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700319 if !line.trim_start().starts_with("//") {
320 writeln!(out, "{}", line);
321 }
322 }
323 }
324}
325
David Tolnay7db73692019-10-20 14:51:12 -0400326fn write_struct(out: &mut OutFile, strct: &Struct) {
327 for line in strct.doc.to_string().lines() {
328 writeln!(out, "//{}", line);
329 }
330 writeln!(out, "struct {} final {{", strct.ident);
331 for field in &strct.fields {
332 write!(out, " ");
333 write_type_space(out, &field.ty);
334 writeln!(out, "{};", field.ident);
335 }
336 writeln!(out, "}};");
337}
338
339fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
340 writeln!(out, "struct {};", ident);
341}
342
David Tolnay8861bee2020-01-20 18:39:24 -0800343fn write_struct_using(out: &mut OutFile, ident: &Ident) {
344 writeln!(out, "using {} = {};", ident, ident);
345}
346
David Tolnayc1fe0052020-04-17 15:15:06 -0700347fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700348 for line in ety.doc.to_string().lines() {
349 writeln!(out, "//{}", line);
350 }
351 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700352 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700353 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700354 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700355 write!(out, " ");
356 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700357 let local_name = method.ident.to_string();
358 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700359 writeln!(out, ";");
360 }
361 writeln!(out, "}};");
362}
363
Joel Galensonc03402a2020-04-23 17:31:09 -0700364fn write_enum(out: &mut OutFile, enm: &Enum) {
365 for line in enm.doc.to_string().lines() {
366 writeln!(out, "//{}", line);
367 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700368 write!(out, "enum class {} : ", enm.ident);
369 write_atom(out, enm.repr);
370 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700371 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700372 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700373 }
374 writeln!(out, "}};");
375}
376
Joel Galenson905eb2e2020-05-04 14:58:14 -0700377fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700378 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
379 write_atom(out, enm.repr);
380 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700381 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700382 write!(out, "static_assert(static_cast<");
383 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700384 writeln!(
385 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700386 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700387 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700388 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700389 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700390}
391
David Tolnayebef4a22020-03-17 15:33:47 -0700392fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
393 let mut has_cxx_throws = false;
394 for api in apis {
395 if let Api::CxxFunction(efn) = api {
396 if efn.throws {
397 has_cxx_throws = true;
398 break;
399 }
400 }
401 }
402
403 if has_cxx_throws {
404 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700405 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700406 out,
David Tolnay69601622020-04-29 18:48:36 -0700407 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700408 );
409 }
410}
411
David Tolnay7db73692019-10-20 14:51:12 -0400412fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700413 if efn.throws {
414 write!(out, "::rust::Str::Repr ");
415 } else {
David Tolnay99642622020-03-25 13:07:35 -0700416 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700417 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700418 let mangled = mangle::extern_fn(&out.namespace, efn);
419 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700420 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700421 if receiver.mutability.is_none() {
422 write!(out, "const ");
423 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700424 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700425 }
David Tolnay7db73692019-10-20 14:51:12 -0400426 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700427 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400428 write!(out, ", ");
429 }
David Tolnaya46a2372020-03-06 10:03:48 -0800430 if arg.ty == RustString {
431 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700432 } else if let Type::RustVec(_) = arg.ty {
433 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800434 }
David Tolnay7db73692019-10-20 14:51:12 -0400435 write_extern_arg(out, arg, types);
436 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700437 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400438 if indirect_return {
439 if !efn.args.is_empty() {
440 write!(out, ", ");
441 }
David Tolnay99642622020-03-25 13:07:35 -0700442 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400443 write!(out, "*return$");
444 }
445 writeln!(out, ") noexcept {{");
446 write!(out, " ");
447 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700448 match &efn.receiver {
449 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700450 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700451 }
David Tolnay7db73692019-10-20 14:51:12 -0400452 for (i, arg) in efn.args.iter().enumerate() {
453 if i > 0 {
454 write!(out, ", ");
455 }
456 write_type(out, &arg.ty);
457 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700458 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700459 if let Some(receiver) = &efn.receiver {
460 if receiver.mutability.is_none() {
461 write!(out, " const");
462 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700463 }
464 write!(out, " = ");
465 match &efn.receiver {
466 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700467 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700468 }
469 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400470 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700471 if efn.throws {
472 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700473 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700474 writeln!(out, " [&] {{");
475 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700476 }
David Tolnay7db73692019-10-20 14:51:12 -0400477 if indirect_return {
478 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700479 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400480 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700481 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400482 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700483 }
484 match &efn.ret {
485 Some(Type::Ref(_)) => write!(out, "&"),
486 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700487 Some(Type::SliceRefU8(_)) if !indirect_return => {
488 write!(out, "::rust::Slice<uint8_t>::Repr(")
489 }
David Tolnay99642622020-03-25 13:07:35 -0700490 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400491 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700492 match &efn.receiver {
493 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700494 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700495 }
David Tolnay7db73692019-10-20 14:51:12 -0400496 for (i, arg) in efn.args.iter().enumerate() {
497 if i > 0 {
498 write!(out, ", ");
499 }
500 if let Type::RustBox(_) = &arg.ty {
501 write_type(out, &arg.ty);
502 write!(out, "::from_raw({})", arg.ident);
503 } else if let Type::UniquePtr(_) = &arg.ty {
504 write_type(out, &arg.ty);
505 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800506 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800507 write!(
508 out,
509 "::rust::String(::rust::unsafe_bitcopy, *{})",
510 arg.ident,
511 );
David Tolnay313b10e2020-04-25 16:30:51 -0700512 } else if let Type::RustVec(_) = arg.ty {
513 write_type(out, &arg.ty);
514 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400515 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700516 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800517 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400518 } else {
519 write!(out, "{}", arg.ident);
520 }
521 }
522 write!(out, ")");
523 match &efn.ret {
524 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
525 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700526 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400527 _ => {}
528 }
529 if indirect_return {
530 write!(out, ")");
531 }
532 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700533 if efn.throws {
534 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700535 writeln!(out, " throw$.ptr = nullptr;");
536 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700537 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700538 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700539 writeln!(
540 out,
David Tolnay69601622020-04-29 18:48:36 -0700541 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700542 );
David Tolnay5d121442020-03-17 22:14:40 -0700543 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700544 writeln!(out, " return throw$;");
545 }
David Tolnay7db73692019-10-20 14:51:12 -0400546 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700547 for arg in &efn.args {
548 if let Type::Fn(f) = &arg.ty {
549 let var = &arg.ident;
550 write_function_pointer_trampoline(out, efn, var, f, types);
551 }
552 }
553}
554
555fn write_function_pointer_trampoline(
556 out: &mut OutFile,
557 efn: &ExternFn,
558 var: &Ident,
559 f: &Signature,
560 types: &Types,
561) {
562 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700563 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700564 let indirect_call = true;
565 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
566
567 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700568 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700569 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400570}
571
572fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700573 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700574 let indirect_call = false;
575 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
576}
577
578fn write_rust_function_decl_impl(
579 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700580 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700581 sig: &Signature,
582 types: &Types,
583 indirect_call: bool,
584) {
585 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700586 write!(out, "::rust::Str::Repr ");
587 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700588 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700589 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700590 write!(out, "{}(", link_name);
591 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700592 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700593 if receiver.mutability.is_none() {
594 write!(out, "const ");
595 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700596 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700597 needs_comma = true;
598 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700599 for arg in &sig.args {
600 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400601 write!(out, ", ");
602 }
603 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700604 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400605 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700606 if indirect_return(sig, types) {
607 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400608 write!(out, ", ");
609 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700610 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400611 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 needs_comma = true;
613 }
614 if indirect_call {
615 if needs_comma {
616 write!(out, ", ");
617 }
618 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400619 }
620 writeln!(out, ") noexcept;");
621}
622
623fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400624 for line in efn.doc.to_string().lines() {
625 writeln!(out, "//{}", line);
626 }
David Tolnaya73853b2020-04-20 01:19:56 -0700627 let local_name = match &efn.sig.receiver {
628 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700629 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700630 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700631 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700632 let indirect_call = false;
633 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
634}
635
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700636fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700637 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700638 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700639 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700640 indirect_call: bool,
641) {
642 write_return_type(out, &sig.ret);
643 write!(out, "{}(", local_name);
644 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400645 if i > 0 {
646 write!(out, ", ");
647 }
648 write_type_space(out, &arg.ty);
649 write!(out, "{}", arg.ident);
650 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700651 if indirect_call {
652 if !sig.args.is_empty() {
653 write!(out, ", ");
654 }
655 write!(out, "void *extern$");
656 }
David Tolnay1e548172020-03-16 13:37:09 -0700657 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700658 if let Some(receiver) = &sig.receiver {
659 if receiver.mutability.is_none() {
660 write!(out, " const");
661 }
662 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700663 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700664 write!(out, " noexcept");
665 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700666}
667
668fn write_rust_function_shim_impl(
669 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700670 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700671 sig: &Signature,
672 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700673 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700674 indirect_call: bool,
675) {
676 if out.header && sig.receiver.is_some() {
677 // We've already defined this inside the struct.
678 return;
679 }
David Tolnaya73853b2020-04-20 01:19:56 -0700680 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400681 if out.header {
682 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700683 return;
David Tolnay7db73692019-10-20 14:51:12 -0400684 }
David Tolnay439cde22020-04-20 00:46:25 -0700685 writeln!(out, " {{");
686 for arg in &sig.args {
687 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
688 out.include.utility = true;
689 write!(out, " ::rust::ManuallyDrop<");
690 write_type(out, &arg.ty);
691 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
692 }
693 }
694 write!(out, " ");
695 let indirect_return = indirect_return(sig, types);
696 if indirect_return {
697 write!(out, "::rust::MaybeUninit<");
698 write_type(out, sig.ret.as_ref().unwrap());
699 writeln!(out, "> return$;");
700 write!(out, " ");
701 } else if let Some(ret) = &sig.ret {
702 write!(out, "return ");
703 match ret {
704 Type::RustBox(_) => {
705 write_type(out, ret);
706 write!(out, "::from_raw(");
707 }
708 Type::UniquePtr(_) => {
709 write_type(out, ret);
710 write!(out, "(");
711 }
712 Type::Ref(_) => write!(out, "*"),
713 _ => {}
714 }
715 }
716 if sig.throws {
717 write!(out, "::rust::Str::Repr error$ = ");
718 }
719 write!(out, "{}(", invoke);
720 if sig.receiver.is_some() {
721 write!(out, "*this");
722 }
723 for (i, arg) in sig.args.iter().enumerate() {
724 if i > 0 || sig.receiver.is_some() {
725 write!(out, ", ");
726 }
727 match &arg.ty {
728 Type::Str(_) => write!(out, "::rust::Str::Repr("),
729 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
730 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
731 _ => {}
732 }
733 write!(out, "{}", arg.ident);
734 match &arg.ty {
735 Type::RustBox(_) => write!(out, ".into_raw()"),
736 Type::UniquePtr(_) => write!(out, ".release()"),
737 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
738 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
739 _ => {}
740 }
741 }
742 if indirect_return {
743 if !sig.args.is_empty() {
744 write!(out, ", ");
745 }
746 write!(out, "&return$.value");
747 }
748 if indirect_call {
749 if !sig.args.is_empty() || indirect_return {
750 write!(out, ", ");
751 }
752 write!(out, "extern$");
753 }
754 write!(out, ")");
755 if let Some(ret) = &sig.ret {
756 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
757 write!(out, ")");
758 }
759 }
760 writeln!(out, ";");
761 if sig.throws {
762 writeln!(out, " if (error$.ptr) {{");
763 writeln!(out, " throw ::rust::Error(error$);");
764 writeln!(out, " }}");
765 }
766 if indirect_return {
767 out.include.utility = true;
768 writeln!(out, " return ::std::move(return$.value);");
769 }
770 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400771}
772
773fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
774 match ty {
775 None => write!(out, "void "),
776 Some(ty) => write_type_space(out, ty),
777 }
778}
779
David Tolnay75dca2e2020-03-25 20:17:52 -0700780fn indirect_return(sig: &Signature, types: &Types) -> bool {
781 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700782 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700783 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700784}
785
David Tolnay99642622020-03-25 13:07:35 -0700786fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
787 match ty {
788 Type::RustBox(ty) | Type::UniquePtr(ty) => {
789 write_type_space(out, &ty.inner);
790 write!(out, "*");
791 }
792 Type::Ref(ty) => {
793 if ty.mutability.is_none() {
794 write!(out, "const ");
795 }
796 write_type(out, &ty.inner);
797 write!(out, " *");
798 }
799 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700800 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700801 _ => write_type(out, ty),
802 }
803}
804
805fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
806 write_indirect_return_type(out, ty);
807 match ty {
808 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700809 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700810 _ => write_space_after_type(out, ty),
811 }
812}
813
814fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400815 match ty {
816 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
817 write_type_space(out, &ty.inner);
818 write!(out, "*");
819 }
David Tolnay4a441222020-01-25 16:24:27 -0800820 Some(Type::Ref(ty)) => {
821 if ty.mutability.is_none() {
822 write!(out, "const ");
823 }
824 write_type(out, &ty.inner);
825 write!(out, " *");
826 }
David Tolnay750755e2020-03-01 13:04:08 -0800827 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700828 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400829 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
830 _ => write_return_type(out, ty),
831 }
832}
833
834fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
835 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700836 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400837 write_type_space(out, &ty.inner);
838 write!(out, "*");
839 }
David Tolnay750755e2020-03-01 13:04:08 -0800840 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700841 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400842 _ => write_type_space(out, &arg.ty),
843 }
844 if types.needs_indirect_abi(&arg.ty) {
845 write!(out, "*");
846 }
847 write!(out, "{}", arg.ident);
848}
849
850fn write_type(out: &mut OutFile, ty: &Type) {
851 match ty {
852 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700853 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400854 None => write!(out, "{}", ident),
855 },
856 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800857 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400858 write_type(out, &ty.inner);
859 write!(out, ">");
860 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700861 Type::RustVec(ty) => {
862 write!(out, "::rust::Vec<");
863 write_type(out, &ty.inner);
864 write!(out, ">");
865 }
David Tolnay7db73692019-10-20 14:51:12 -0400866 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800867 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400868 write_type(out, &ptr.inner);
869 write!(out, ">");
870 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700871 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700872 write!(out, "::std::vector<");
873 write_type(out, &ty.inner);
874 write!(out, ">");
875 }
David Tolnay7db73692019-10-20 14:51:12 -0400876 Type::Ref(r) => {
877 if r.mutability.is_none() {
878 write!(out, "const ");
879 }
880 write_type(out, &r.inner);
881 write!(out, " &");
882 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700883 Type::Slice(_) => {
884 // For now, only U8 slices are supported, which are covered separately below
885 unreachable!()
886 }
David Tolnay7db73692019-10-20 14:51:12 -0400887 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800888 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400889 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700890 Type::SliceRefU8(_) => {
891 write!(out, "::rust::Slice<uint8_t>");
892 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700893 Type::Fn(f) => {
894 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
895 match &f.ret {
896 Some(ret) => write_type(out, ret),
897 None => write!(out, "void"),
898 }
899 write!(out, "(");
900 for (i, arg) in f.args.iter().enumerate() {
901 if i > 0 {
902 write!(out, ", ");
903 }
904 write_type(out, &arg.ty);
905 }
906 write!(out, ")>");
907 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700908 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400909 }
910}
911
David Tolnayf6a89f22020-05-10 23:39:27 -0700912fn write_atom(out: &mut OutFile, atom: Atom) {
913 match atom {
914 Bool => write!(out, "bool"),
915 U8 => write!(out, "uint8_t"),
916 U16 => write!(out, "uint16_t"),
917 U32 => write!(out, "uint32_t"),
918 U64 => write!(out, "uint64_t"),
919 Usize => write!(out, "size_t"),
920 I8 => write!(out, "int8_t"),
921 I16 => write!(out, "int16_t"),
922 I32 => write!(out, "int32_t"),
923 I64 => write!(out, "int64_t"),
924 Isize => write!(out, "::rust::isize"),
925 F32 => write!(out, "float"),
926 F64 => write!(out, "double"),
927 CxxString => write!(out, "::std::string"),
928 RustString => write!(out, "::rust::String"),
929 }
930}
931
David Tolnay7db73692019-10-20 14:51:12 -0400932fn write_type_space(out: &mut OutFile, ty: &Type) {
933 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700934 write_space_after_type(out, ty);
935}
936
937fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400938 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700939 Type::Ident(_)
940 | Type::RustBox(_)
941 | Type::UniquePtr(_)
942 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700943 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700944 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700945 | Type::SliceRefU8(_)
946 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400947 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700948 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400949 }
950}
951
David Tolnaycd08c442020-04-25 10:16:33 -0700952// Only called for legal referent types of unique_ptr and element types of
953// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700954fn to_typename(namespace: &Namespace, ty: &Type) -> String {
955 match ty {
956 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700957 let mut path = String::new();
958 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700959 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700960 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700961 }
David Tolnaycd08c442020-04-25 10:16:33 -0700962 path += &ident.to_string();
963 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700964 }
965 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700966 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700967 }
968}
969
David Tolnayacdf20a2020-04-25 12:40:53 -0700970// Only called for legal referent types of unique_ptr and element types of
971// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700972fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
973 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700974 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700975 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700976 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700977 }
978}
979
David Tolnay7db73692019-10-20 14:51:12 -0400980fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400981 out.begin_block("extern \"C\"");
982 for ty in types {
983 if let Type::RustBox(ty) = ty {
984 if let Type::Ident(inner) = &ty.inner {
985 out.next_section();
986 write_rust_box_extern(out, inner);
987 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700988 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700989 if let Type::Ident(inner) = &ty.inner {
990 if Atom::from(inner).is_none() {
991 out.next_section();
992 write_rust_vec_extern(out, inner);
993 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700994 }
David Tolnay7db73692019-10-20 14:51:12 -0400995 } else if let Type::UniquePtr(ptr) = ty {
996 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -0700997 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -0400998 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -0700999 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001000 }
1001 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001002 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001003 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001004 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001005 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001006 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001007 }
1008 }
1009 }
1010 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001011 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001012
David Tolnay750755e2020-03-01 13:04:08 -08001013 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001014 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001015 for ty in types {
1016 if let Type::RustBox(ty) = ty {
1017 if let Type::Ident(inner) = &ty.inner {
1018 write_rust_box_impl(out, inner);
1019 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001020 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001021 if let Type::Ident(inner) = &ty.inner {
1022 if Atom::from(inner).is_none() {
1023 write_rust_vec_impl(out, inner);
1024 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001025 }
David Tolnay7db73692019-10-20 14:51:12 -04001026 }
1027 }
David Tolnay69601622020-04-29 18:48:36 -07001028 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001029 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001030}
1031
1032fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1033 let mut inner = String::new();
1034 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001035 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001036 inner += "::";
1037 }
1038 inner += &ident.to_string();
1039 let instance = inner.replace("::", "$");
1040
David Tolnay69601622020-04-29 18:48:36 -07001041 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1042 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001043 writeln!(
1044 out,
David Tolnay69601622020-04-29 18:48:36 -07001045 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001046 instance, inner,
1047 );
1048 writeln!(
1049 out,
David Tolnay69601622020-04-29 18:48:36 -07001050 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001051 instance, inner,
1052 );
David Tolnay69601622020-04-29 18:48:36 -07001053 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001054}
1055
David Tolnay6787be62020-04-25 11:01:02 -07001056fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1057 let element = Type::Ident(element.clone());
1058 let inner = to_typename(&out.namespace, &element);
1059 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001060
David Tolnay69601622020-04-29 18:48:36 -07001061 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1062 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001063 writeln!(
1064 out,
David Tolnay69601622020-04-29 18:48:36 -07001065 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001066 instance, inner,
1067 );
1068 writeln!(
1069 out,
David Tolnay69601622020-04-29 18:48:36 -07001070 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001071 instance, inner,
1072 );
1073 writeln!(
1074 out,
David Tolnay69601622020-04-29 18:48:36 -07001075 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001076 instance, inner,
1077 );
David Tolnay219c0792020-04-24 20:31:37 -07001078 writeln!(
1079 out,
David Tolnay69601622020-04-29 18:48:36 -07001080 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001081 inner, instance,
1082 );
David Tolnay503d0192020-04-24 22:18:56 -07001083 writeln!(
1084 out,
David Tolnay69601622020-04-29 18:48:36 -07001085 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001086 instance,
1087 );
David Tolnay69601622020-04-29 18:48:36 -07001088 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001089}
1090
David Tolnay7db73692019-10-20 14:51:12 -04001091fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1092 let mut inner = String::new();
1093 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001094 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001095 inner += "::";
1096 }
1097 inner += &ident.to_string();
1098 let instance = inner.replace("::", "$");
1099
1100 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001101 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001102 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001103 writeln!(out, "}}");
1104
1105 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001106 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001107 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001108 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001109}
1110
David Tolnay6787be62020-04-25 11:01:02 -07001111fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1112 let element = Type::Ident(element.clone());
1113 let inner = to_typename(&out.namespace, &element);
1114 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001115
Myron Ahneba35cf2020-02-05 19:41:51 +07001116 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001117 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001118 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001119 writeln!(out, "}}");
1120
1121 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001122 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1123 writeln!(
1124 out,
David Tolnay69601622020-04-29 18:48:36 -07001125 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001126 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001127 );
1128 writeln!(out, "}}");
1129
1130 writeln!(out, "template <>");
1131 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001132 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001133 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001134
1135 writeln!(out, "template <>");
1136 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1137 writeln!(
1138 out,
David Tolnay69601622020-04-29 18:48:36 -07001139 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001140 instance,
1141 );
1142 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001143
1144 writeln!(out, "template <>");
1145 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001146 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001147 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001148}
1149
David Tolnay63da4d32020-04-25 09:41:12 -07001150fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1151 let ty = Type::Ident(ident.clone());
1152 let instance = to_mangled(&out.namespace, &ty);
1153
David Tolnay69601622020-04-29 18:48:36 -07001154 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1155 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001156
1157 write_unique_ptr_common(out, &ty, types);
1158
David Tolnay69601622020-04-29 18:48:36 -07001159 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001160}
1161
1162// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1163fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001164 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001165 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001166 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001167
David Tolnay63da4d32020-04-25 09:41:12 -07001168 let can_construct_from_value = match ty {
1169 Type::Ident(ident) => types.structs.contains_key(ident),
1170 _ => false,
1171 };
1172
David Tolnay7db73692019-10-20 14:51:12 -04001173 writeln!(
1174 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001175 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001176 inner,
1177 );
1178 writeln!(
1179 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001180 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001181 inner,
1182 );
1183 writeln!(
1184 out,
David Tolnay69601622020-04-29 18:48:36 -07001185 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001186 instance, inner,
1187 );
David Tolnay7e219b82020-03-01 13:14:51 -08001188 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001189 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001190 if can_construct_from_value {
1191 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001192 out,
David Tolnay69601622020-04-29 18:48:36 -07001193 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001194 instance, inner, inner,
1195 );
David Tolnay63da4d32020-04-25 09:41:12 -07001196 writeln!(
1197 out,
1198 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1199 inner, inner,
1200 );
1201 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001202 }
David Tolnay7db73692019-10-20 14:51:12 -04001203 writeln!(
1204 out,
David Tolnay69601622020-04-29 18:48:36 -07001205 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001206 instance, inner, inner,
1207 );
David Tolnay7e219b82020-03-01 13:14:51 -08001208 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001209 writeln!(out, "}}");
1210 writeln!(
1211 out,
David Tolnay69601622020-04-29 18:48:36 -07001212 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001213 inner, instance, inner,
1214 );
1215 writeln!(out, " return ptr.get();");
1216 writeln!(out, "}}");
1217 writeln!(
1218 out,
David Tolnay69601622020-04-29 18:48:36 -07001219 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001220 inner, instance, inner,
1221 );
1222 writeln!(out, " return ptr.release();");
1223 writeln!(out, "}}");
1224 writeln!(
1225 out,
David Tolnay69601622020-04-29 18:48:36 -07001226 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001227 instance, inner,
1228 );
1229 writeln!(out, " ptr->~unique_ptr();");
1230 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001231}
Myron Ahneba35cf2020-02-05 19:41:51 +07001232
David Tolnay92105da2020-04-25 11:15:31 -07001233fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001234 let element = Type::Ident(element.clone());
1235 let inner = to_typename(&out.namespace, &element);
1236 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001237
David Tolnay69601622020-04-29 18:48:36 -07001238 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1239 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001240 writeln!(
1241 out,
David Tolnay69601622020-04-29 18:48:36 -07001242 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001243 instance, inner,
1244 );
1245 writeln!(out, " return s.size();");
1246 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001247 writeln!(
1248 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001249 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001250 inner, instance, inner,
1251 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001252 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001253 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001254
1255 write_unique_ptr_common(out, vector_ty, types);
1256
David Tolnay69601622020-04-29 18:48:36 -07001257 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001258}