blob: 04d8e3e2a2052e0475318d748bafa9fc4cb8f9aa [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 {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070089 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040090 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
91 Api::RustFunction(efn) => (efn, write_rust_function_decl),
92 _ => continue,
93 };
94 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -070095 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -040096 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080097 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040098 }
99
100 for api in apis {
101 if let Api::RustFunction(efn) = api {
102 out.next_section();
103 write_rust_function_shim(out, efn, types);
104 }
105 }
106
107 out.next_section();
108 for name in namespace.iter().rev() {
109 writeln!(out, "}} // namespace {}", name);
110 }
111
112 if !header {
113 out.next_section();
114 write_generic_instantiations(out, types);
115 }
116
David Tolnay9c68b1a2020-03-06 11:12:55 -0800117 out.prepend(out.include.to_string());
118
David Tolnay7db73692019-10-20 14:51:12 -0400119 out_file
120}
121
122fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400123 for ty in types {
124 match ty {
125 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700126 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
127 | Some(I64) => out.include.cstdint = true,
128 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800129 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700130 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400131 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800132 Type::RustBox(_) => out.include.type_traits = true,
133 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700134 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700135 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700136 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400137 }
138 }
David Tolnay7db73692019-10-20 14:51:12 -0400139}
140
David Tolnayf51447e2020-03-06 14:14:27 -0800141fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 let mut needs_rust_string = false;
143 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700144 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400145 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700146 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700147 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700148 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400149 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700150 match ty {
151 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700152 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700153 out.include.type_traits = true;
154 needs_rust_box = true;
155 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700156 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700157 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700158 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700159 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700160 needs_rust_vec = true;
161 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700162 Type::Str(_) => {
163 out.include.cstdint = true;
164 out.include.string = true;
165 needs_rust_str = true;
166 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700167 Type::Fn(_) => {
168 needs_rust_fn = true;
169 }
David Tolnay4770b472020-04-14 16:32:59 -0700170 Type::Slice(_) | Type::SliceRefU8(_) => {
171 needs_rust_slice = true;
172 }
David Tolnay7c295462020-04-25 12:45:07 -0700173 ty if ty == Isize => {
174 out.include.base_tsd = true;
175 needs_rust_isize = true;
176 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700177 ty if ty == RustString => {
178 out.include.array = true;
179 out.include.cstdint = true;
180 out.include.string = true;
181 needs_rust_string = true;
182 }
183 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400184 }
185 }
186
David Tolnayb7a7cb62020-03-17 21:18:40 -0700187 let mut needs_rust_error = false;
188 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800189 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800190 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700191 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800192 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 match api {
194 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700195 if efn.throws {
196 needs_trycatch = true;
197 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700198 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700199 let bitcopy = match arg.ty {
200 Type::RustVec(_) => true,
201 _ => arg.ty == RustString,
202 };
203 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700204 needs_unsafe_bitcopy = true;
205 break;
206 }
David Tolnay09011c32020-03-06 14:40:28 -0800207 }
208 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700209 Api::RustFunction(efn) if !out.header => {
210 if efn.throws {
211 out.include.exception = true;
212 needs_rust_error = true;
213 }
214 for arg in &efn.args {
215 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
216 needs_manually_drop = true;
217 break;
218 }
219 }
220 if let Some(ret) = &efn.ret {
221 if types.needs_indirect_abi(ret) {
222 needs_maybe_uninit = true;
223 }
David Tolnayf51447e2020-03-06 14:14:27 -0800224 }
225 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800227 }
228 }
229
David Tolnay750755e2020-03-01 13:04:08 -0800230 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700231 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800232
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 if needs_rust_string
234 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700235 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700236 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700237 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700238 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700240 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 || needs_unsafe_bitcopy
242 || needs_manually_drop
243 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700244 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 {
David Tolnay736cbca2020-03-11 16:49:18 -0700246 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800247 }
248
David Tolnay32ee9d32020-05-12 20:01:09 -0700249 if needs_rust_string || needs_rust_vec {
David Tolnayd1402742020-03-25 22:21:42 -0700250 out.next_section();
251 writeln!(out, "struct unsafe_bitcopy_t;");
252 }
253
David Tolnaye54f3382020-05-12 19:58:36 -0700254 include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
255 include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
256 include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
257 include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
258 include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
259 include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
260 include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
261 include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
262 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800263
264 if needs_manually_drop {
265 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700266 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800267 writeln!(out, "template <typename T>");
268 writeln!(out, "union ManuallyDrop {{");
269 writeln!(out, " T value;");
270 writeln!(
271 out,
272 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
273 );
274 writeln!(out, " ~ManuallyDrop() {{}}");
275 writeln!(out, "}};");
276 }
277
David Tolnay09011c32020-03-06 14:40:28 -0800278 if needs_maybe_uninit {
279 out.next_section();
280 writeln!(out, "template <typename T>");
281 writeln!(out, "union MaybeUninit {{");
282 writeln!(out, " T value;");
283 writeln!(out, " MaybeUninit() {{}}");
284 writeln!(out, " ~MaybeUninit() {{}}");
285 writeln!(out, "}};");
286 }
287
David Tolnay69601622020-04-29 18:48:36 -0700288 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700289
David Tolnay5d121442020-03-17 22:14:40 -0700290 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700291 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700292 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700293 out.include.type_traits = true;
294 out.include.utility = true;
295 writeln!(out, "class missing {{}};");
296 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700297 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700298 writeln!(out, "template <typename Try, typename Fail>");
299 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700300 writeln!(
301 out,
David Tolnay04722332020-03-18 11:31:54 -0700302 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700303 );
David Tolnay04722332020-03-18 11:31:54 -0700304 writeln!(out, " missing>::value>::type");
305 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700306 writeln!(out, " func();");
307 writeln!(out, "}} catch (const ::std::exception &e) {{");
308 writeln!(out, " fail(e.what());");
309 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700310 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700311 }
312
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800313 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400314}
315
316fn write_struct(out: &mut OutFile, strct: &Struct) {
317 for line in strct.doc.to_string().lines() {
318 writeln!(out, "//{}", line);
319 }
320 writeln!(out, "struct {} final {{", strct.ident);
321 for field in &strct.fields {
322 write!(out, " ");
323 write_type_space(out, &field.ty);
324 writeln!(out, "{};", field.ident);
325 }
326 writeln!(out, "}};");
327}
328
329fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
330 writeln!(out, "struct {};", ident);
331}
332
David Tolnay8861bee2020-01-20 18:39:24 -0800333fn write_struct_using(out: &mut OutFile, ident: &Ident) {
334 writeln!(out, "using {} = {};", ident, ident);
335}
336
David Tolnayc1fe0052020-04-17 15:15:06 -0700337fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700338 for line in ety.doc.to_string().lines() {
339 writeln!(out, "//{}", line);
340 }
341 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700342 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700343 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700344 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700345 write!(out, " ");
346 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700347 let local_name = method.ident.to_string();
348 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700349 writeln!(out, ";");
350 }
351 writeln!(out, "}};");
352}
353
Joel Galensonc03402a2020-04-23 17:31:09 -0700354fn write_enum(out: &mut OutFile, enm: &Enum) {
355 for line in enm.doc.to_string().lines() {
356 writeln!(out, "//{}", line);
357 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700358 write!(out, "enum class {} : ", enm.ident);
359 write_atom(out, enm.repr);
360 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700361 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700362 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700363 }
364 writeln!(out, "}};");
365}
366
Joel Galenson905eb2e2020-05-04 14:58:14 -0700367fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700368 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
369 write_atom(out, enm.repr);
370 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700371 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700372 write!(out, "static_assert(static_cast<");
373 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700374 writeln!(
375 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700376 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700377 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700378 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700379 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700380}
381
David Tolnayebef4a22020-03-17 15:33:47 -0700382fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
383 let mut has_cxx_throws = false;
384 for api in apis {
385 if let Api::CxxFunction(efn) = api {
386 if efn.throws {
387 has_cxx_throws = true;
388 break;
389 }
390 }
391 }
392
393 if has_cxx_throws {
394 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700395 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700396 out,
David Tolnay69601622020-04-29 18:48:36 -0700397 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700398 );
399 }
400}
401
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700402fn write_cxx_function_shim(
403 out: &mut OutFile,
404 efn: &ExternFn,
405 types: &Types,
406 impl_annotations: &Option<String>,
407) {
408 if !out.header {
409 if let Some(annotation) = impl_annotations {
410 write!(out, "{} ", annotation);
411 }
412 }
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 {
myronahne3b78ea2020-05-23 01:08:13 +0700439 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400440 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 {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700478 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400479 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700480 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400481 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700482 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400483 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700484 }
485 match &efn.ret {
486 Some(Type::Ref(_)) => write!(out, "&"),
487 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700488 Some(Type::SliceRefU8(_)) if !indirect_return => {
489 write!(out, "::rust::Slice<uint8_t>::Repr(")
490 }
David Tolnay99642622020-03-25 13:07:35 -0700491 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400492 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700493 match &efn.receiver {
494 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700495 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700496 }
David Tolnay7db73692019-10-20 14:51:12 -0400497 for (i, arg) in efn.args.iter().enumerate() {
498 if i > 0 {
499 write!(out, ", ");
500 }
501 if let Type::RustBox(_) = &arg.ty {
502 write_type(out, &arg.ty);
503 write!(out, "::from_raw({})", arg.ident);
504 } else if let Type::UniquePtr(_) = &arg.ty {
505 write_type(out, &arg.ty);
506 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800507 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800508 write!(
509 out,
510 "::rust::String(::rust::unsafe_bitcopy, *{})",
511 arg.ident,
512 );
David Tolnay313b10e2020-04-25 16:30:51 -0700513 } else if let Type::RustVec(_) = arg.ty {
514 write_type(out, &arg.ty);
515 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400516 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700517 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800518 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400519 } else {
520 write!(out, "{}", arg.ident);
521 }
522 }
523 write!(out, ")");
524 match &efn.ret {
525 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
526 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700527 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400528 _ => {}
529 }
530 if indirect_return {
531 write!(out, ")");
532 }
533 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700534 if efn.throws {
535 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700536 writeln!(out, " throw$.ptr = nullptr;");
537 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700538 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700539 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700540 writeln!(
541 out,
David Tolnay69601622020-04-29 18:48:36 -0700542 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700543 );
David Tolnay5d121442020-03-17 22:14:40 -0700544 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700545 writeln!(out, " return throw$;");
546 }
David Tolnay7db73692019-10-20 14:51:12 -0400547 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700548 for arg in &efn.args {
549 if let Type::Fn(f) = &arg.ty {
550 let var = &arg.ident;
551 write_function_pointer_trampoline(out, efn, var, f, types);
552 }
553 }
554}
555
556fn write_function_pointer_trampoline(
557 out: &mut OutFile,
558 efn: &ExternFn,
559 var: &Ident,
560 f: &Signature,
561 types: &Types,
562) {
563 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700564 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700565 let indirect_call = true;
566 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
567
568 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700569 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700570 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400571}
572
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700573fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700574 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700575 let indirect_call = false;
576 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
577}
578
579fn write_rust_function_decl_impl(
580 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700581 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700582 sig: &Signature,
583 types: &Types,
584 indirect_call: bool,
585) {
586 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700587 write!(out, "::rust::Str::Repr ");
588 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700589 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700590 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700591 write!(out, "{}(", link_name);
592 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700593 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700594 if receiver.mutability.is_none() {
595 write!(out, "const ");
596 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700597 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700598 needs_comma = true;
599 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700600 for arg in &sig.args {
601 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400602 write!(out, ", ");
603 }
604 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700605 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400606 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700607 if indirect_return(sig, types) {
608 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400609 write!(out, ", ");
610 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700611 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400612 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700613 needs_comma = true;
614 }
615 if indirect_call {
616 if needs_comma {
617 write!(out, ", ");
618 }
619 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400620 }
621 writeln!(out, ") noexcept;");
622}
623
624fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400625 for line in efn.doc.to_string().lines() {
626 writeln!(out, "//{}", line);
627 }
David Tolnaya73853b2020-04-20 01:19:56 -0700628 let local_name = match &efn.sig.receiver {
629 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700630 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700631 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700632 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700633 let indirect_call = false;
634 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
635}
636
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700637fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700638 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700639 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700640 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700641 indirect_call: bool,
642) {
643 write_return_type(out, &sig.ret);
644 write!(out, "{}(", local_name);
645 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400646 if i > 0 {
647 write!(out, ", ");
648 }
649 write_type_space(out, &arg.ty);
650 write!(out, "{}", arg.ident);
651 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 if indirect_call {
653 if !sig.args.is_empty() {
654 write!(out, ", ");
655 }
656 write!(out, "void *extern$");
657 }
David Tolnay1e548172020-03-16 13:37:09 -0700658 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700659 if let Some(receiver) = &sig.receiver {
660 if receiver.mutability.is_none() {
661 write!(out, " const");
662 }
663 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700664 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700665 write!(out, " noexcept");
666 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700667}
668
669fn write_rust_function_shim_impl(
670 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700671 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700672 sig: &Signature,
673 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700674 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700675 indirect_call: bool,
676) {
677 if out.header && sig.receiver.is_some() {
678 // We've already defined this inside the struct.
679 return;
680 }
David Tolnaya73853b2020-04-20 01:19:56 -0700681 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400682 if out.header {
683 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700684 return;
David Tolnay7db73692019-10-20 14:51:12 -0400685 }
David Tolnay439cde22020-04-20 00:46:25 -0700686 writeln!(out, " {{");
687 for arg in &sig.args {
688 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
689 out.include.utility = true;
690 write!(out, " ::rust::ManuallyDrop<");
691 write_type(out, &arg.ty);
692 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
693 }
694 }
695 write!(out, " ");
696 let indirect_return = indirect_return(sig, types);
697 if indirect_return {
698 write!(out, "::rust::MaybeUninit<");
699 write_type(out, sig.ret.as_ref().unwrap());
700 writeln!(out, "> return$;");
701 write!(out, " ");
702 } else if let Some(ret) = &sig.ret {
703 write!(out, "return ");
704 match ret {
705 Type::RustBox(_) => {
706 write_type(out, ret);
707 write!(out, "::from_raw(");
708 }
709 Type::UniquePtr(_) => {
710 write_type(out, ret);
711 write!(out, "(");
712 }
713 Type::Ref(_) => write!(out, "*"),
714 _ => {}
715 }
716 }
717 if sig.throws {
718 write!(out, "::rust::Str::Repr error$ = ");
719 }
720 write!(out, "{}(", invoke);
721 if sig.receiver.is_some() {
722 write!(out, "*this");
723 }
724 for (i, arg) in sig.args.iter().enumerate() {
725 if i > 0 || sig.receiver.is_some() {
726 write!(out, ", ");
727 }
728 match &arg.ty {
729 Type::Str(_) => write!(out, "::rust::Str::Repr("),
730 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
731 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
732 _ => {}
733 }
734 write!(out, "{}", arg.ident);
735 match &arg.ty {
736 Type::RustBox(_) => write!(out, ".into_raw()"),
737 Type::UniquePtr(_) => write!(out, ".release()"),
738 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
739 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
740 _ => {}
741 }
742 }
743 if indirect_return {
744 if !sig.args.is_empty() {
745 write!(out, ", ");
746 }
747 write!(out, "&return$.value");
748 }
749 if indirect_call {
750 if !sig.args.is_empty() || indirect_return {
751 write!(out, ", ");
752 }
753 write!(out, "extern$");
754 }
755 write!(out, ")");
756 if let Some(ret) = &sig.ret {
757 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
758 write!(out, ")");
759 }
760 }
761 writeln!(out, ";");
762 if sig.throws {
763 writeln!(out, " if (error$.ptr) {{");
764 writeln!(out, " throw ::rust::Error(error$);");
765 writeln!(out, " }}");
766 }
767 if indirect_return {
768 out.include.utility = true;
769 writeln!(out, " return ::std::move(return$.value);");
770 }
771 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400772}
773
774fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
775 match ty {
776 None => write!(out, "void "),
777 Some(ty) => write_type_space(out, ty),
778 }
779}
780
David Tolnay75dca2e2020-03-25 20:17:52 -0700781fn indirect_return(sig: &Signature, types: &Types) -> bool {
782 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700783 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700784 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700785}
786
David Tolnay99642622020-03-25 13:07:35 -0700787fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
788 match ty {
789 Type::RustBox(ty) | Type::UniquePtr(ty) => {
790 write_type_space(out, &ty.inner);
791 write!(out, "*");
792 }
793 Type::Ref(ty) => {
794 if ty.mutability.is_none() {
795 write!(out, "const ");
796 }
797 write_type(out, &ty.inner);
798 write!(out, " *");
799 }
800 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700801 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700802 _ => write_type(out, ty),
803 }
804}
805
806fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
807 write_indirect_return_type(out, ty);
808 match ty {
809 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700810 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700811 _ => write_space_after_type(out, ty),
812 }
813}
814
815fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400816 match ty {
817 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
818 write_type_space(out, &ty.inner);
819 write!(out, "*");
820 }
David Tolnay4a441222020-01-25 16:24:27 -0800821 Some(Type::Ref(ty)) => {
822 if ty.mutability.is_none() {
823 write!(out, "const ");
824 }
825 write_type(out, &ty.inner);
826 write!(out, " *");
827 }
David Tolnay750755e2020-03-01 13:04:08 -0800828 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700829 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400830 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
831 _ => write_return_type(out, ty),
832 }
833}
834
835fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
836 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700837 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400838 write_type_space(out, &ty.inner);
839 write!(out, "*");
840 }
David Tolnay750755e2020-03-01 13:04:08 -0800841 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700842 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400843 _ => write_type_space(out, &arg.ty),
844 }
845 if types.needs_indirect_abi(&arg.ty) {
846 write!(out, "*");
847 }
848 write!(out, "{}", arg.ident);
849}
850
851fn write_type(out: &mut OutFile, ty: &Type) {
852 match ty {
853 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700854 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400855 None => write!(out, "{}", ident),
856 },
857 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800858 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400859 write_type(out, &ty.inner);
860 write!(out, ">");
861 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700862 Type::RustVec(ty) => {
863 write!(out, "::rust::Vec<");
864 write_type(out, &ty.inner);
865 write!(out, ">");
866 }
David Tolnay7db73692019-10-20 14:51:12 -0400867 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800868 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400869 write_type(out, &ptr.inner);
870 write!(out, ">");
871 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700872 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700873 write!(out, "::std::vector<");
874 write_type(out, &ty.inner);
875 write!(out, ">");
876 }
David Tolnay7db73692019-10-20 14:51:12 -0400877 Type::Ref(r) => {
878 if r.mutability.is_none() {
879 write!(out, "const ");
880 }
881 write_type(out, &r.inner);
882 write!(out, " &");
883 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700884 Type::Slice(_) => {
885 // For now, only U8 slices are supported, which are covered separately below
886 unreachable!()
887 }
David Tolnay7db73692019-10-20 14:51:12 -0400888 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800889 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400890 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700891 Type::SliceRefU8(_) => {
892 write!(out, "::rust::Slice<uint8_t>");
893 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700894 Type::Fn(f) => {
895 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
896 match &f.ret {
897 Some(ret) => write_type(out, ret),
898 None => write!(out, "void"),
899 }
900 write!(out, "(");
901 for (i, arg) in f.args.iter().enumerate() {
902 if i > 0 {
903 write!(out, ", ");
904 }
905 write_type(out, &arg.ty);
906 }
907 write!(out, ")>");
908 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700909 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400910 }
911}
912
David Tolnayf6a89f22020-05-10 23:39:27 -0700913fn write_atom(out: &mut OutFile, atom: Atom) {
914 match atom {
915 Bool => write!(out, "bool"),
916 U8 => write!(out, "uint8_t"),
917 U16 => write!(out, "uint16_t"),
918 U32 => write!(out, "uint32_t"),
919 U64 => write!(out, "uint64_t"),
920 Usize => write!(out, "size_t"),
921 I8 => write!(out, "int8_t"),
922 I16 => write!(out, "int16_t"),
923 I32 => write!(out, "int32_t"),
924 I64 => write!(out, "int64_t"),
925 Isize => write!(out, "::rust::isize"),
926 F32 => write!(out, "float"),
927 F64 => write!(out, "double"),
928 CxxString => write!(out, "::std::string"),
929 RustString => write!(out, "::rust::String"),
930 }
931}
932
David Tolnay7db73692019-10-20 14:51:12 -0400933fn write_type_space(out: &mut OutFile, ty: &Type) {
934 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700935 write_space_after_type(out, ty);
936}
937
938fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400939 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700940 Type::Ident(_)
941 | Type::RustBox(_)
942 | Type::UniquePtr(_)
943 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700944 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700945 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700946 | Type::SliceRefU8(_)
947 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400948 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700949 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400950 }
951}
952
David Tolnaycd08c442020-04-25 10:16:33 -0700953// Only called for legal referent types of unique_ptr and element types of
954// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700955fn to_typename(namespace: &Namespace, ty: &Type) -> String {
956 match ty {
957 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700958 let mut path = String::new();
959 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700960 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700961 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700962 }
David Tolnaycd08c442020-04-25 10:16:33 -0700963 path += &ident.to_string();
964 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700965 }
966 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700967 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700968 }
969}
970
David Tolnayacdf20a2020-04-25 12:40:53 -0700971// Only called for legal referent types of unique_ptr and element types of
972// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700973fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
974 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700975 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700976 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700977 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700978 }
979}
980
David Tolnay7db73692019-10-20 14:51:12 -0400981fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400982 out.begin_block("extern \"C\"");
983 for ty in types {
984 if let Type::RustBox(ty) = ty {
985 if let Type::Ident(inner) = &ty.inner {
986 out.next_section();
987 write_rust_box_extern(out, inner);
988 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700989 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700990 if let Type::Ident(inner) = &ty.inner {
991 if Atom::from(inner).is_none() {
992 out.next_section();
993 write_rust_vec_extern(out, inner);
994 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700995 }
David Tolnay7db73692019-10-20 14:51:12 -0400996 } else if let Type::UniquePtr(ptr) = ty {
997 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -0700998 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -0400999 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001000 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001001 }
1002 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001003 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001004 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001005 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001006 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001007 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001008 }
1009 }
1010 }
1011 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001012 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001013
David Tolnay750755e2020-03-01 13:04:08 -08001014 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001015 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001016 for ty in types {
1017 if let Type::RustBox(ty) = ty {
1018 if let Type::Ident(inner) = &ty.inner {
1019 write_rust_box_impl(out, inner);
1020 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001021 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001022 if let Type::Ident(inner) = &ty.inner {
1023 if Atom::from(inner).is_none() {
1024 write_rust_vec_impl(out, inner);
1025 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001026 }
David Tolnay7db73692019-10-20 14:51:12 -04001027 }
1028 }
David Tolnay69601622020-04-29 18:48:36 -07001029 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001030 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001031}
1032
1033fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1034 let mut inner = String::new();
1035 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001036 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001037 inner += "::";
1038 }
1039 inner += &ident.to_string();
1040 let instance = inner.replace("::", "$");
1041
David Tolnay69601622020-04-29 18:48:36 -07001042 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1043 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001044 writeln!(
1045 out,
David Tolnay69601622020-04-29 18:48:36 -07001046 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001047 instance, inner,
1048 );
1049 writeln!(
1050 out,
David Tolnay69601622020-04-29 18:48:36 -07001051 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001052 instance, inner,
1053 );
David Tolnay69601622020-04-29 18:48:36 -07001054 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001055}
1056
David Tolnay6787be62020-04-25 11:01:02 -07001057fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1058 let element = Type::Ident(element.clone());
1059 let inner = to_typename(&out.namespace, &element);
1060 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001061
David Tolnay69601622020-04-29 18:48:36 -07001062 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1063 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001064 writeln!(
1065 out,
David Tolnay69601622020-04-29 18:48:36 -07001066 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001067 instance, inner,
1068 );
1069 writeln!(
1070 out,
David Tolnay69601622020-04-29 18:48:36 -07001071 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001072 instance, inner,
1073 );
1074 writeln!(
1075 out,
David Tolnay69601622020-04-29 18:48:36 -07001076 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001077 instance, inner,
1078 );
David Tolnay219c0792020-04-24 20:31:37 -07001079 writeln!(
1080 out,
David Tolnay69601622020-04-29 18:48:36 -07001081 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001082 inner, instance,
1083 );
David Tolnay503d0192020-04-24 22:18:56 -07001084 writeln!(
1085 out,
David Tolnay69601622020-04-29 18:48:36 -07001086 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001087 instance,
1088 );
David Tolnay69601622020-04-29 18:48:36 -07001089 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001090}
1091
David Tolnay7db73692019-10-20 14:51:12 -04001092fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1093 let mut inner = String::new();
1094 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001095 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001096 inner += "::";
1097 }
1098 inner += &ident.to_string();
1099 let instance = inner.replace("::", "$");
1100
1101 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001102 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001103 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001104 writeln!(out, "}}");
1105
1106 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001107 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001108 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001109 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001110}
1111
David Tolnay6787be62020-04-25 11:01:02 -07001112fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1113 let element = Type::Ident(element.clone());
1114 let inner = to_typename(&out.namespace, &element);
1115 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001116
Myron Ahneba35cf2020-02-05 19:41:51 +07001117 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001118 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001119 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001120 writeln!(out, "}}");
1121
1122 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001123 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1124 writeln!(
1125 out,
David Tolnay69601622020-04-29 18:48:36 -07001126 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001127 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001128 );
1129 writeln!(out, "}}");
1130
1131 writeln!(out, "template <>");
1132 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001133 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001134 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001135
1136 writeln!(out, "template <>");
1137 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1138 writeln!(
1139 out,
David Tolnay69601622020-04-29 18:48:36 -07001140 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001141 instance,
1142 );
1143 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001144
1145 writeln!(out, "template <>");
1146 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001147 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001148 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001149}
1150
David Tolnay63da4d32020-04-25 09:41:12 -07001151fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1152 let ty = Type::Ident(ident.clone());
1153 let instance = to_mangled(&out.namespace, &ty);
1154
David Tolnay69601622020-04-29 18:48:36 -07001155 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1156 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001157
1158 write_unique_ptr_common(out, &ty, types);
1159
David Tolnay69601622020-04-29 18:48:36 -07001160 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001161}
1162
1163// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1164fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001165 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001166 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001167 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001168 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001169
David Tolnay63da4d32020-04-25 09:41:12 -07001170 let can_construct_from_value = match ty {
1171 Type::Ident(ident) => types.structs.contains_key(ident),
1172 _ => false,
1173 };
1174
David Tolnay7db73692019-10-20 14:51:12 -04001175 writeln!(
1176 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001177 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001178 inner,
1179 );
1180 writeln!(
1181 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001182 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001183 inner,
1184 );
1185 writeln!(
1186 out,
David Tolnay69601622020-04-29 18:48:36 -07001187 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001188 instance, inner,
1189 );
David Tolnay7e219b82020-03-01 13:14:51 -08001190 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001191 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001192 if can_construct_from_value {
1193 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001194 out,
David Tolnay69601622020-04-29 18:48:36 -07001195 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001196 instance, inner, inner,
1197 );
David Tolnay63da4d32020-04-25 09:41:12 -07001198 writeln!(
1199 out,
1200 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1201 inner, inner,
1202 );
1203 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001204 }
David Tolnay7db73692019-10-20 14:51:12 -04001205 writeln!(
1206 out,
David Tolnay69601622020-04-29 18:48:36 -07001207 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001208 instance, inner, inner,
1209 );
David Tolnay7e219b82020-03-01 13:14:51 -08001210 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001211 writeln!(out, "}}");
1212 writeln!(
1213 out,
David Tolnay69601622020-04-29 18:48:36 -07001214 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001215 inner, instance, inner,
1216 );
1217 writeln!(out, " return ptr.get();");
1218 writeln!(out, "}}");
1219 writeln!(
1220 out,
David Tolnay69601622020-04-29 18:48:36 -07001221 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001222 inner, instance, inner,
1223 );
1224 writeln!(out, " return ptr.release();");
1225 writeln!(out, "}}");
1226 writeln!(
1227 out,
David Tolnay69601622020-04-29 18:48:36 -07001228 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001229 instance, inner,
1230 );
1231 writeln!(out, " ptr->~unique_ptr();");
1232 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001233}
Myron Ahneba35cf2020-02-05 19:41:51 +07001234
David Tolnay92105da2020-04-25 11:15:31 -07001235fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001236 let element = Type::Ident(element.clone());
1237 let inner = to_typename(&out.namespace, &element);
1238 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001239
David Tolnay69601622020-04-29 18:48:36 -07001240 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1241 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001242 writeln!(
1243 out,
David Tolnay69601622020-04-29 18:48:36 -07001244 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001245 instance, inner,
1246 );
1247 writeln!(out, " return s.size();");
1248 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001249 writeln!(
1250 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001251 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001252 inner, instance, inner,
1253 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001254 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001255 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001256
1257 write_unique_ptr_common(out, vector_ty, types);
1258
David Tolnay69601622020-04-29 18:48:36 -07001259 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001260}