blob: 2ea6ca85b39b3620e270fdd00704964621910d3f [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 Galenson905eb2e2020-05-04 14:58:14 -07008use std::collections::{HashMap, HashSet};
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 Tolnay9c68b1a2020-03-06 11:12:55 -080027 out.include.insert(include.value());
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
Joel Galenson905eb2e2020-05-04 14:58:14 -070049 let mut cxx_types = HashSet::new();
David Tolnayf94bef12020-04-17 14:46:42 -070050 let mut methods_for_type = HashMap::new();
51 for api in apis {
52 if let Api::RustFunction(efn) = api {
53 if let Some(receiver) = &efn.sig.receiver {
54 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070055 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070056 .or_insert_with(Vec::new)
57 .push(efn);
58 }
59 }
Joel Galenson905eb2e2020-05-04 14:58:14 -070060 if let Api::CxxType(enm) = api {
61 cxx_types.insert(&enm.ident);
62 }
David Tolnayf94bef12020-04-17 14:46:42 -070063 }
Joel Galenson968738f2020-04-15 14:19:33 -070064
David Tolnay7db73692019-10-20 14:51:12 -040065 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070066 match api {
67 Api::Struct(strct) => {
68 out.next_section();
69 write_struct(out, strct);
70 }
Joel Galensonc03402a2020-04-23 17:31:09 -070071 Api::Enum(enm) => {
72 out.next_section();
Joel Galenson905eb2e2020-05-04 14:58:14 -070073 if cxx_types.contains(&enm.ident) {
74 check_enum(out, enm);
75 } else {
76 write_enum(out, enm);
77 }
Joel Galensonc03402a2020-04-23 17:31:09 -070078 }
David Tolnayc1fe0052020-04-17 15:15:06 -070079 Api::RustType(ety) => {
80 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070081 out.next_section();
82 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070083 }
David Tolnayc1fe0052020-04-17 15:15:06 -070084 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070085 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040086 }
87 }
88
89 if !header {
90 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070091 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040092 for api in apis {
93 let (efn, write): (_, fn(_, _, _)) = match api {
94 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
95 Api::RustFunction(efn) => (efn, write_rust_function_decl),
96 _ => continue,
97 };
98 out.next_section();
99 write(out, efn, types);
100 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800101 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400102 }
103
104 for api in apis {
105 if let Api::RustFunction(efn) = api {
106 out.next_section();
107 write_rust_function_shim(out, efn, types);
108 }
109 }
110
111 out.next_section();
112 for name in namespace.iter().rev() {
113 writeln!(out, "}} // namespace {}", name);
114 }
115
116 if !header {
117 out.next_section();
118 write_generic_instantiations(out, types);
119 }
120
David Tolnay9c68b1a2020-03-06 11:12:55 -0800121 out.prepend(out.include.to_string());
122
David Tolnay7db73692019-10-20 14:51:12 -0400123 out_file
124}
125
126fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400127 for ty in types {
128 match ty {
129 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700130 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
131 | Some(I64) => out.include.cstdint = true,
132 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800133 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700134 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400135 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800136 Type::RustBox(_) => out.include.type_traits = true,
137 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700138 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700139 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700140 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400141 }
142 }
David Tolnay7db73692019-10-20 14:51:12 -0400143}
144
David Tolnayf51447e2020-03-06 14:14:27 -0800145fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700146 let mut needs_rust_string = false;
147 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700148 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400149 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700150 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700151 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700152 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400153 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700154 match ty {
155 Type::RustBox(_) => {
156 out.include.type_traits = true;
157 needs_rust_box = true;
158 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700159 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700160 out.include.array = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700161 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700162 needs_rust_vec = true;
163 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700164 Type::Str(_) => {
165 out.include.cstdint = true;
166 out.include.string = true;
167 needs_rust_str = true;
168 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700169 Type::Fn(_) => {
170 needs_rust_fn = true;
171 }
David Tolnay4770b472020-04-14 16:32:59 -0700172 Type::Slice(_) | Type::SliceRefU8(_) => {
173 needs_rust_slice = true;
174 }
David Tolnay7c295462020-04-25 12:45:07 -0700175 ty if ty == Isize => {
176 out.include.base_tsd = true;
177 needs_rust_isize = true;
178 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700179 ty if ty == RustString => {
180 out.include.array = true;
181 out.include.cstdint = true;
182 out.include.string = true;
183 needs_rust_string = true;
184 }
185 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400186 }
187 }
188
David Tolnayb7a7cb62020-03-17 21:18:40 -0700189 let mut needs_rust_error = false;
190 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800191 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800192 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700193 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800194 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700195 match api {
196 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700197 if efn.throws {
198 needs_trycatch = true;
199 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700200 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700201 let bitcopy = match arg.ty {
202 Type::RustVec(_) => true,
203 _ => arg.ty == RustString,
204 };
205 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700206 needs_unsafe_bitcopy = true;
207 break;
208 }
David Tolnay09011c32020-03-06 14:40:28 -0800209 }
210 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700211 Api::RustFunction(efn) if !out.header => {
212 if efn.throws {
213 out.include.exception = true;
214 needs_rust_error = true;
215 }
216 for arg in &efn.args {
217 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
218 needs_manually_drop = true;
219 break;
220 }
221 }
222 if let Some(ret) = &efn.ret {
223 if types.needs_indirect_abi(ret) {
224 needs_maybe_uninit = true;
225 }
David Tolnayf51447e2020-03-06 14:14:27 -0800226 }
227 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800229 }
230 }
231
David Tolnay750755e2020-03-01 13:04:08 -0800232 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700233 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800234
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 if needs_rust_string
236 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700237 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700238 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700239 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700240 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700242 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 || needs_unsafe_bitcopy
244 || needs_manually_drop
245 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700246 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700247 {
David Tolnay736cbca2020-03-11 16:49:18 -0700248 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800249 }
250
David Tolnayd1402742020-03-25 22:21:42 -0700251 if needs_rust_string {
252 out.next_section();
253 writeln!(out, "struct unsafe_bitcopy_t;");
254 }
255
David Tolnay69601622020-04-29 18:48:36 -0700256 write_header_section(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
257 write_header_section(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
258 write_header_section(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
259 write_header_section(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
260 write_header_section(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
261 write_header_section(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
262 write_header_section(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
263 write_header_section(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
264 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800265
266 if needs_manually_drop {
267 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700268 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800269 writeln!(out, "template <typename T>");
270 writeln!(out, "union ManuallyDrop {{");
271 writeln!(out, " T value;");
272 writeln!(
273 out,
274 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
275 );
276 writeln!(out, " ~ManuallyDrop() {{}}");
277 writeln!(out, "}};");
278 }
279
David Tolnay09011c32020-03-06 14:40:28 -0800280 if needs_maybe_uninit {
281 out.next_section();
282 writeln!(out, "template <typename T>");
283 writeln!(out, "union MaybeUninit {{");
284 writeln!(out, " T value;");
285 writeln!(out, " MaybeUninit() {{}}");
286 writeln!(out, " ~MaybeUninit() {{}}");
287 writeln!(out, "}};");
288 }
289
David Tolnay69601622020-04-29 18:48:36 -0700290 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700291
David Tolnay5d121442020-03-17 22:14:40 -0700292 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700293 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700294 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700295 out.include.type_traits = true;
296 out.include.utility = true;
297 writeln!(out, "class missing {{}};");
298 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700299 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700300 writeln!(out, "template <typename Try, typename Fail>");
301 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700302 writeln!(
303 out,
David Tolnay04722332020-03-18 11:31:54 -0700304 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700305 );
David Tolnay04722332020-03-18 11:31:54 -0700306 writeln!(out, " missing>::value>::type");
307 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700308 writeln!(out, " func();");
309 writeln!(out, "}} catch (const ::std::exception &e) {{");
310 writeln!(out, " fail(e.what());");
311 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700312 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700313 }
314
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800315 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400316}
317
David Tolnayb7a7cb62020-03-17 21:18:40 -0700318fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700319 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700320 if needed {
321 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700322 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700323 if !line.trim_start().starts_with("//") {
324 writeln!(out, "{}", line);
325 }
326 }
327 }
328}
329
David Tolnay7db73692019-10-20 14:51:12 -0400330fn write_struct(out: &mut OutFile, strct: &Struct) {
331 for line in strct.doc.to_string().lines() {
332 writeln!(out, "//{}", line);
333 }
334 writeln!(out, "struct {} final {{", strct.ident);
335 for field in &strct.fields {
336 write!(out, " ");
337 write_type_space(out, &field.ty);
338 writeln!(out, "{};", field.ident);
339 }
340 writeln!(out, "}};");
341}
342
343fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
344 writeln!(out, "struct {};", ident);
345}
346
David Tolnay8861bee2020-01-20 18:39:24 -0800347fn write_struct_using(out: &mut OutFile, ident: &Ident) {
348 writeln!(out, "using {} = {};", ident, ident);
349}
350
David Tolnayc1fe0052020-04-17 15:15:06 -0700351fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700352 for line in ety.doc.to_string().lines() {
353 writeln!(out, "//{}", line);
354 }
355 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700356 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700357 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700358 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700359 write!(out, " ");
360 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700361 let local_name = method.ident.to_string();
362 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700363 writeln!(out, ";");
364 }
365 writeln!(out, "}};");
366}
367
Joel Galensonc03402a2020-04-23 17:31:09 -0700368fn write_enum(out: &mut OutFile, enm: &Enum) {
369 for line in enm.doc.to_string().lines() {
370 writeln!(out, "//{}", line);
371 }
372 writeln!(out, "enum class {} : uint32_t {{", enm.ident);
373 for variant in &enm.variants {
374 write!(out, " ");
375 write!(out, "{}", variant.ident);
376 if let Some(discriminant) = &variant.discriminant {
377 write!(out, " = {}", discriminant);
378 }
379 writeln!(out, ",");
380 }
381 writeln!(out, "}};");
382}
383
Joel Galenson905eb2e2020-05-04 14:58:14 -0700384fn check_enum(out: &mut OutFile, enm: &Enum) {
385 let discriminants = enm
386 .variants
387 .iter()
388 .scan(None, |prev_discriminant, variant| {
389 let discriminant = variant
390 .discriminant
391 .unwrap_or_else(|| prev_discriminant.map_or(0, |n| n + 1));
392 *prev_discriminant = Some(discriminant);
393 Some(discriminant)
394 });
395 writeln!(
396 out,
397 "static_assert(sizeof({}) == sizeof(uint32_t));",
398 enm.ident
399 );
400 enm.variants
401 .iter()
402 .zip(discriminants)
403 .for_each(|(variant, discriminant)| {
404 writeln!(
405 out,
406 "static_assert({}::{} == {});",
407 enm.ident, variant.ident, discriminant
408 );
409 });
410}
411
David Tolnayebef4a22020-03-17 15:33:47 -0700412fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
413 let mut has_cxx_throws = false;
414 for api in apis {
415 if let Api::CxxFunction(efn) = api {
416 if efn.throws {
417 has_cxx_throws = true;
418 break;
419 }
420 }
421 }
422
423 if has_cxx_throws {
424 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700425 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700426 out,
David Tolnay69601622020-04-29 18:48:36 -0700427 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700428 );
429 }
430}
431
David Tolnay7db73692019-10-20 14:51:12 -0400432fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700433 if efn.throws {
434 write!(out, "::rust::Str::Repr ");
435 } else {
David Tolnay99642622020-03-25 13:07:35 -0700436 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700437 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700438 let mangled = mangle::extern_fn(&out.namespace, efn);
439 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700440 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700441 if receiver.mutability.is_none() {
442 write!(out, "const ");
443 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700444 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700445 }
David Tolnay7db73692019-10-20 14:51:12 -0400446 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700447 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400448 write!(out, ", ");
449 }
David Tolnaya46a2372020-03-06 10:03:48 -0800450 if arg.ty == RustString {
451 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700452 } else if let Type::RustVec(_) = arg.ty {
453 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800454 }
David Tolnay7db73692019-10-20 14:51:12 -0400455 write_extern_arg(out, arg, types);
456 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700457 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400458 if indirect_return {
459 if !efn.args.is_empty() {
460 write!(out, ", ");
461 }
David Tolnay99642622020-03-25 13:07:35 -0700462 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400463 write!(out, "*return$");
464 }
465 writeln!(out, ") noexcept {{");
466 write!(out, " ");
467 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700468 match &efn.receiver {
469 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700470 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700471 }
David Tolnay7db73692019-10-20 14:51:12 -0400472 for (i, arg) in efn.args.iter().enumerate() {
473 if i > 0 {
474 write!(out, ", ");
475 }
476 write_type(out, &arg.ty);
477 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700478 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700479 if let Some(receiver) = &efn.receiver {
480 if receiver.mutability.is_none() {
481 write!(out, " const");
482 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700483 }
484 write!(out, " = ");
485 match &efn.receiver {
486 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700487 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700488 }
489 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400490 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700491 if efn.throws {
492 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700493 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700494 writeln!(out, " [&] {{");
495 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700496 }
David Tolnay7db73692019-10-20 14:51:12 -0400497 if indirect_return {
498 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700499 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400500 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700501 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400502 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700503 }
504 match &efn.ret {
505 Some(Type::Ref(_)) => write!(out, "&"),
506 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700507 Some(Type::SliceRefU8(_)) if !indirect_return => {
508 write!(out, "::rust::Slice<uint8_t>::Repr(")
509 }
David Tolnay99642622020-03-25 13:07:35 -0700510 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400511 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700512 match &efn.receiver {
513 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700514 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700515 }
David Tolnay7db73692019-10-20 14:51:12 -0400516 for (i, arg) in efn.args.iter().enumerate() {
517 if i > 0 {
518 write!(out, ", ");
519 }
520 if let Type::RustBox(_) = &arg.ty {
521 write_type(out, &arg.ty);
522 write!(out, "::from_raw({})", arg.ident);
523 } else if let Type::UniquePtr(_) = &arg.ty {
524 write_type(out, &arg.ty);
525 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800526 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800527 write!(
528 out,
529 "::rust::String(::rust::unsafe_bitcopy, *{})",
530 arg.ident,
531 );
David Tolnay313b10e2020-04-25 16:30:51 -0700532 } else if let Type::RustVec(_) = arg.ty {
533 write_type(out, &arg.ty);
534 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400535 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700536 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800537 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400538 } else {
539 write!(out, "{}", arg.ident);
540 }
541 }
542 write!(out, ")");
543 match &efn.ret {
544 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
545 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay4377a9e2020-04-24 15:20:26 -0700546 Some(Type::CxxVector(_)) => write!(
Myron Ahneba35cf2020-02-05 19:41:51 +0700547 out,
548 " /* Use RVO to convert to r-value and move construct */"
549 ),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700550 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400551 _ => {}
552 }
553 if indirect_return {
554 write!(out, ")");
555 }
556 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700557 if efn.throws {
558 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700559 writeln!(out, " throw$.ptr = nullptr;");
560 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700561 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700562 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700563 writeln!(
564 out,
David Tolnay69601622020-04-29 18:48:36 -0700565 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700566 );
David Tolnay5d121442020-03-17 22:14:40 -0700567 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700568 writeln!(out, " return throw$;");
569 }
David Tolnay7db73692019-10-20 14:51:12 -0400570 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700571 for arg in &efn.args {
572 if let Type::Fn(f) = &arg.ty {
573 let var = &arg.ident;
574 write_function_pointer_trampoline(out, efn, var, f, types);
575 }
576 }
577}
578
579fn write_function_pointer_trampoline(
580 out: &mut OutFile,
581 efn: &ExternFn,
582 var: &Ident,
583 f: &Signature,
584 types: &Types,
585) {
586 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700587 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700588 let indirect_call = true;
589 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
590
591 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700592 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700593 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400594}
595
596fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700597 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 let indirect_call = false;
599 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
600}
601
602fn write_rust_function_decl_impl(
603 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700604 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700605 sig: &Signature,
606 types: &Types,
607 indirect_call: bool,
608) {
609 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700610 write!(out, "::rust::Str::Repr ");
611 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700613 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700614 write!(out, "{}(", link_name);
615 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700616 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700617 if receiver.mutability.is_none() {
618 write!(out, "const ");
619 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700620 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700621 needs_comma = true;
622 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700623 for arg in &sig.args {
624 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400625 write!(out, ", ");
626 }
627 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400629 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700630 if indirect_return(sig, types) {
631 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400632 write!(out, ", ");
633 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700634 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400635 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700636 needs_comma = true;
637 }
638 if indirect_call {
639 if needs_comma {
640 write!(out, ", ");
641 }
642 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400643 }
644 writeln!(out, ") noexcept;");
645}
646
647fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400648 for line in efn.doc.to_string().lines() {
649 writeln!(out, "//{}", line);
650 }
David Tolnaya73853b2020-04-20 01:19:56 -0700651 let local_name = match &efn.sig.receiver {
652 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700653 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700654 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700655 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 let indirect_call = false;
657 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
658}
659
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700660fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700661 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700662 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700663 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700664 indirect_call: bool,
665) {
666 write_return_type(out, &sig.ret);
667 write!(out, "{}(", local_name);
668 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400669 if i > 0 {
670 write!(out, ", ");
671 }
672 write_type_space(out, &arg.ty);
673 write!(out, "{}", arg.ident);
674 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700675 if indirect_call {
676 if !sig.args.is_empty() {
677 write!(out, ", ");
678 }
679 write!(out, "void *extern$");
680 }
David Tolnay1e548172020-03-16 13:37:09 -0700681 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700682 if let Some(receiver) = &sig.receiver {
683 if receiver.mutability.is_none() {
684 write!(out, " const");
685 }
686 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700687 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700688 write!(out, " noexcept");
689 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700690}
691
692fn write_rust_function_shim_impl(
693 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700694 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700695 sig: &Signature,
696 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700697 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700698 indirect_call: bool,
699) {
700 if out.header && sig.receiver.is_some() {
701 // We've already defined this inside the struct.
702 return;
703 }
David Tolnaya73853b2020-04-20 01:19:56 -0700704 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400705 if out.header {
706 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700707 return;
David Tolnay7db73692019-10-20 14:51:12 -0400708 }
David Tolnay439cde22020-04-20 00:46:25 -0700709 writeln!(out, " {{");
710 for arg in &sig.args {
711 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
712 out.include.utility = true;
713 write!(out, " ::rust::ManuallyDrop<");
714 write_type(out, &arg.ty);
715 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
716 }
717 }
718 write!(out, " ");
719 let indirect_return = indirect_return(sig, types);
720 if indirect_return {
721 write!(out, "::rust::MaybeUninit<");
722 write_type(out, sig.ret.as_ref().unwrap());
723 writeln!(out, "> return$;");
724 write!(out, " ");
725 } else if let Some(ret) = &sig.ret {
726 write!(out, "return ");
727 match ret {
728 Type::RustBox(_) => {
729 write_type(out, ret);
730 write!(out, "::from_raw(");
731 }
732 Type::UniquePtr(_) => {
733 write_type(out, ret);
734 write!(out, "(");
735 }
736 Type::Ref(_) => write!(out, "*"),
737 _ => {}
738 }
739 }
740 if sig.throws {
741 write!(out, "::rust::Str::Repr error$ = ");
742 }
743 write!(out, "{}(", invoke);
744 if sig.receiver.is_some() {
745 write!(out, "*this");
746 }
747 for (i, arg) in sig.args.iter().enumerate() {
748 if i > 0 || sig.receiver.is_some() {
749 write!(out, ", ");
750 }
751 match &arg.ty {
752 Type::Str(_) => write!(out, "::rust::Str::Repr("),
753 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
754 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
755 _ => {}
756 }
757 write!(out, "{}", arg.ident);
758 match &arg.ty {
759 Type::RustBox(_) => write!(out, ".into_raw()"),
760 Type::UniquePtr(_) => write!(out, ".release()"),
761 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
762 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
763 _ => {}
764 }
765 }
766 if indirect_return {
767 if !sig.args.is_empty() {
768 write!(out, ", ");
769 }
770 write!(out, "&return$.value");
771 }
772 if indirect_call {
773 if !sig.args.is_empty() || indirect_return {
774 write!(out, ", ");
775 }
776 write!(out, "extern$");
777 }
778 write!(out, ")");
779 if let Some(ret) = &sig.ret {
780 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
781 write!(out, ")");
782 }
783 }
784 writeln!(out, ";");
785 if sig.throws {
786 writeln!(out, " if (error$.ptr) {{");
787 writeln!(out, " throw ::rust::Error(error$);");
788 writeln!(out, " }}");
789 }
790 if indirect_return {
791 out.include.utility = true;
792 writeln!(out, " return ::std::move(return$.value);");
793 }
794 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400795}
796
797fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
798 match ty {
799 None => write!(out, "void "),
800 Some(ty) => write_type_space(out, ty),
801 }
802}
803
David Tolnay75dca2e2020-03-25 20:17:52 -0700804fn indirect_return(sig: &Signature, types: &Types) -> bool {
805 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700806 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700807 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700808}
809
David Tolnay99642622020-03-25 13:07:35 -0700810fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
811 match ty {
812 Type::RustBox(ty) | Type::UniquePtr(ty) => {
813 write_type_space(out, &ty.inner);
814 write!(out, "*");
815 }
816 Type::Ref(ty) => {
817 if ty.mutability.is_none() {
818 write!(out, "const ");
819 }
820 write_type(out, &ty.inner);
821 write!(out, " *");
822 }
823 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700824 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700825 _ => write_type(out, ty),
826 }
827}
828
829fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
830 write_indirect_return_type(out, ty);
831 match ty {
832 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700833 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700834 _ => write_space_after_type(out, ty),
835 }
836}
837
838fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400839 match ty {
840 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
841 write_type_space(out, &ty.inner);
842 write!(out, "*");
843 }
David Tolnay4a441222020-01-25 16:24:27 -0800844 Some(Type::Ref(ty)) => {
845 if ty.mutability.is_none() {
846 write!(out, "const ");
847 }
848 write_type(out, &ty.inner);
849 write!(out, " *");
850 }
David Tolnay750755e2020-03-01 13:04:08 -0800851 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700852 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400853 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
854 _ => write_return_type(out, ty),
855 }
856}
857
858fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
859 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700860 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400861 write_type_space(out, &ty.inner);
862 write!(out, "*");
863 }
David Tolnay750755e2020-03-01 13:04:08 -0800864 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700865 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400866 _ => write_type_space(out, &arg.ty),
867 }
868 if types.needs_indirect_abi(&arg.ty) {
869 write!(out, "*");
870 }
871 write!(out, "{}", arg.ident);
872}
873
874fn write_type(out: &mut OutFile, ty: &Type) {
875 match ty {
876 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay029f1d62020-04-25 10:19:25 -0700877 Some(Bool) => write!(out, "bool"),
878 Some(U8) => write!(out, "uint8_t"),
879 Some(U16) => write!(out, "uint16_t"),
880 Some(U32) => write!(out, "uint32_t"),
881 Some(U64) => write!(out, "uint64_t"),
882 Some(Usize) => write!(out, "size_t"),
883 Some(I8) => write!(out, "int8_t"),
884 Some(I16) => write!(out, "int16_t"),
885 Some(I32) => write!(out, "int32_t"),
886 Some(I64) => write!(out, "int64_t"),
887 Some(Isize) => write!(out, "::rust::isize"),
888 Some(F32) => write!(out, "float"),
889 Some(F64) => write!(out, "double"),
890 Some(CxxString) => write!(out, "::std::string"),
891 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400892 None => write!(out, "{}", ident),
893 },
894 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800895 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400896 write_type(out, &ty.inner);
897 write!(out, ">");
898 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700899 Type::RustVec(ty) => {
900 write!(out, "::rust::Vec<");
901 write_type(out, &ty.inner);
902 write!(out, ">");
903 }
David Tolnay7db73692019-10-20 14:51:12 -0400904 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800905 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400906 write_type(out, &ptr.inner);
907 write!(out, ">");
908 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700909 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700910 write!(out, "::std::vector<");
911 write_type(out, &ty.inner);
912 write!(out, ">");
913 }
David Tolnay7db73692019-10-20 14:51:12 -0400914 Type::Ref(r) => {
915 if r.mutability.is_none() {
916 write!(out, "const ");
917 }
918 write_type(out, &r.inner);
919 write!(out, " &");
920 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700921 Type::Slice(_) => {
922 // For now, only U8 slices are supported, which are covered separately below
923 unreachable!()
924 }
David Tolnay7db73692019-10-20 14:51:12 -0400925 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800926 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400927 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700928 Type::SliceRefU8(_) => {
929 write!(out, "::rust::Slice<uint8_t>");
930 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700931 Type::Fn(f) => {
932 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
933 match &f.ret {
934 Some(ret) => write_type(out, ret),
935 None => write!(out, "void"),
936 }
937 write!(out, "(");
938 for (i, arg) in f.args.iter().enumerate() {
939 if i > 0 {
940 write!(out, ", ");
941 }
942 write_type(out, &arg.ty);
943 }
944 write!(out, ")>");
945 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700946 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400947 }
948}
949
950fn write_type_space(out: &mut OutFile, ty: &Type) {
951 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700952 write_space_after_type(out, ty);
953}
954
955fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400956 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700957 Type::Ident(_)
958 | Type::RustBox(_)
959 | Type::UniquePtr(_)
960 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700961 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700962 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700963 | Type::SliceRefU8(_)
964 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400965 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700966 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400967 }
968}
969
David Tolnaycd08c442020-04-25 10:16:33 -0700970// Only called for legal referent types of unique_ptr and element types of
971// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700972fn to_typename(namespace: &Namespace, ty: &Type) -> String {
973 match ty {
974 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700975 let mut path = String::new();
976 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700977 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700978 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700979 }
David Tolnaycd08c442020-04-25 10:16:33 -0700980 path += &ident.to_string();
981 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700982 }
983 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700984 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700985 }
986}
987
David Tolnayacdf20a2020-04-25 12:40:53 -0700988// Only called for legal referent types of unique_ptr and element types of
989// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700990fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
991 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700992 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700993 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700994 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700995 }
996}
997
David Tolnay7db73692019-10-20 14:51:12 -0400998fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
999 fn allow_unique_ptr(ident: &Ident) -> bool {
1000 Atom::from(ident).is_none()
1001 }
1002
1003 out.begin_block("extern \"C\"");
1004 for ty in types {
1005 if let Type::RustBox(ty) = ty {
1006 if let Type::Ident(inner) = &ty.inner {
1007 out.next_section();
1008 write_rust_box_extern(out, inner);
1009 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001010 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001011 if let Type::Ident(inner) = &ty.inner {
1012 if Atom::from(inner).is_none() {
1013 out.next_section();
1014 write_rust_vec_extern(out, inner);
1015 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001016 }
David Tolnay7db73692019-10-20 14:51:12 -04001017 } else if let Type::UniquePtr(ptr) = ty {
1018 if let Type::Ident(inner) = &ptr.inner {
1019 if allow_unique_ptr(inner) {
1020 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001021 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001022 }
1023 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001024 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001025 if let Type::Ident(inner) = &ptr.inner {
David Tolnay63da4d32020-04-25 09:41:12 -07001026 if Atom::from(inner).is_none() {
Myron Ahneba35cf2020-02-05 19:41:51 +07001027 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001028 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001029 }
1030 }
1031 }
1032 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001033 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001034
David Tolnay750755e2020-03-01 13:04:08 -08001035 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001036 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001037 for ty in types {
1038 if let Type::RustBox(ty) = ty {
1039 if let Type::Ident(inner) = &ty.inner {
1040 write_rust_box_impl(out, inner);
1041 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001042 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001043 if let Type::Ident(inner) = &ty.inner {
1044 if Atom::from(inner).is_none() {
1045 write_rust_vec_impl(out, inner);
1046 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001047 }
David Tolnay7db73692019-10-20 14:51:12 -04001048 }
1049 }
David Tolnay69601622020-04-29 18:48:36 -07001050 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001051 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001052}
1053
1054fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1055 let mut inner = String::new();
1056 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001057 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001058 inner += "::";
1059 }
1060 inner += &ident.to_string();
1061 let instance = inner.replace("::", "$");
1062
David Tolnay69601622020-04-29 18:48:36 -07001063 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1064 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001065 writeln!(
1066 out,
David Tolnay69601622020-04-29 18:48:36 -07001067 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001068 instance, inner,
1069 );
1070 writeln!(
1071 out,
David Tolnay69601622020-04-29 18:48:36 -07001072 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001073 instance, inner,
1074 );
David Tolnay69601622020-04-29 18:48:36 -07001075 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001076}
1077
David Tolnay6787be62020-04-25 11:01:02 -07001078fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1079 let element = Type::Ident(element.clone());
1080 let inner = to_typename(&out.namespace, &element);
1081 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001082
David Tolnay69601622020-04-29 18:48:36 -07001083 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1084 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001085 writeln!(
1086 out,
David Tolnay69601622020-04-29 18:48:36 -07001087 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001088 instance, inner,
1089 );
1090 writeln!(
1091 out,
David Tolnay69601622020-04-29 18:48:36 -07001092 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001093 instance, inner,
1094 );
1095 writeln!(
1096 out,
David Tolnay69601622020-04-29 18:48:36 -07001097 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001098 instance, inner,
1099 );
David Tolnay219c0792020-04-24 20:31:37 -07001100 writeln!(
1101 out,
David Tolnay69601622020-04-29 18:48:36 -07001102 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001103 inner, instance,
1104 );
David Tolnay503d0192020-04-24 22:18:56 -07001105 writeln!(
1106 out,
David Tolnay69601622020-04-29 18:48:36 -07001107 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001108 instance,
1109 );
David Tolnay69601622020-04-29 18:48:36 -07001110 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001111}
1112
David Tolnay7db73692019-10-20 14:51:12 -04001113fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1114 let mut inner = String::new();
1115 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001116 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001117 inner += "::";
1118 }
1119 inner += &ident.to_string();
1120 let instance = inner.replace("::", "$");
1121
1122 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001123 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001124 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001125 writeln!(out, "}}");
1126
1127 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001128 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001129 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001130 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001131}
1132
David Tolnay6787be62020-04-25 11:01:02 -07001133fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1134 let element = Type::Ident(element.clone());
1135 let inner = to_typename(&out.namespace, &element);
1136 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001137
Myron Ahneba35cf2020-02-05 19:41:51 +07001138 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001139 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001140 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001141 writeln!(out, "}}");
1142
1143 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001144 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1145 writeln!(
1146 out,
David Tolnay69601622020-04-29 18:48:36 -07001147 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001148 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001149 );
1150 writeln!(out, "}}");
1151
1152 writeln!(out, "template <>");
1153 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001154 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001155 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001156
1157 writeln!(out, "template <>");
1158 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1159 writeln!(
1160 out,
David Tolnay69601622020-04-29 18:48:36 -07001161 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001162 instance,
1163 );
1164 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001165
1166 writeln!(out, "template <>");
1167 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001168 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001169 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001170}
1171
David Tolnay63da4d32020-04-25 09:41:12 -07001172fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1173 let ty = Type::Ident(ident.clone());
1174 let instance = to_mangled(&out.namespace, &ty);
1175
David Tolnay69601622020-04-29 18:48:36 -07001176 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1177 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001178
1179 write_unique_ptr_common(out, &ty, types);
1180
David Tolnay69601622020-04-29 18:48:36 -07001181 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001182}
1183
1184// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1185fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001186 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001187 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001188 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001189
David Tolnay63da4d32020-04-25 09:41:12 -07001190 let can_construct_from_value = match ty {
1191 Type::Ident(ident) => types.structs.contains_key(ident),
1192 _ => false,
1193 };
1194
David Tolnay7db73692019-10-20 14:51:12 -04001195 writeln!(
1196 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001197 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001198 inner,
1199 );
1200 writeln!(
1201 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001202 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001203 inner,
1204 );
1205 writeln!(
1206 out,
David Tolnay69601622020-04-29 18:48:36 -07001207 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001208 instance, inner,
1209 );
David Tolnay7e219b82020-03-01 13:14:51 -08001210 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001211 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001212 if can_construct_from_value {
1213 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001214 out,
David Tolnay69601622020-04-29 18:48:36 -07001215 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001216 instance, inner, inner,
1217 );
David Tolnay63da4d32020-04-25 09:41:12 -07001218 writeln!(
1219 out,
1220 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1221 inner, inner,
1222 );
1223 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001224 }
David Tolnay7db73692019-10-20 14:51:12 -04001225 writeln!(
1226 out,
David Tolnay69601622020-04-29 18:48:36 -07001227 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001228 instance, inner, inner,
1229 );
David Tolnay7e219b82020-03-01 13:14:51 -08001230 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001231 writeln!(out, "}}");
1232 writeln!(
1233 out,
David Tolnay69601622020-04-29 18:48:36 -07001234 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001235 inner, instance, inner,
1236 );
1237 writeln!(out, " return ptr.get();");
1238 writeln!(out, "}}");
1239 writeln!(
1240 out,
David Tolnay69601622020-04-29 18:48:36 -07001241 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001242 inner, instance, inner,
1243 );
1244 writeln!(out, " return ptr.release();");
1245 writeln!(out, "}}");
1246 writeln!(
1247 out,
David Tolnay69601622020-04-29 18:48:36 -07001248 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001249 instance, inner,
1250 );
1251 writeln!(out, " ptr->~unique_ptr();");
1252 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001253}
Myron Ahneba35cf2020-02-05 19:41:51 +07001254
David Tolnay92105da2020-04-25 11:15:31 -07001255fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001256 let element = Type::Ident(element.clone());
1257 let inner = to_typename(&out.namespace, &element);
1258 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001259
David Tolnay69601622020-04-29 18:48:36 -07001260 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1261 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001262 writeln!(
1263 out,
David Tolnay69601622020-04-29 18:48:36 -07001264 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001265 instance, inner,
1266 );
1267 writeln!(out, " return s.size();");
1268 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001269 writeln!(
1270 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001271 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001272 inner, instance, inner,
1273 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001274 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001275 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001276
1277 write_unique_ptr_common(out, vector_ty, types);
1278
David Tolnay69601622020-04-29 18:48:36 -07001279 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001280}