blob: 88a0734fb82b8e82159a068957b1b33046c95c01 [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
David Tolnay33d30292020-03-18 18:02:02 -070020 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040021 for api in apis {
22 if let Api::Include(include) = api {
David Tolnay91e87fa2020-05-11 19:10:23 -070023 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040024 }
25 }
26
27 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080028 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040029
David Tolnay7db73692019-10-20 14:51:12 -040030 out.next_section();
David Tolnay2ec14632020-05-04 00:47:10 -070031 for name in namespace {
David Tolnay7db73692019-10-20 14:51:12 -040032 writeln!(out, "namespace {} {{", name);
33 }
34
David Tolnay7db73692019-10-20 14:51:12 -040035 out.next_section();
36 for api in apis {
37 match api {
38 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080039 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
40 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070041 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040042 }
43 }
44
David Tolnayf94bef12020-04-17 14:46:42 -070045 let mut methods_for_type = HashMap::new();
46 for api in apis {
47 if let Api::RustFunction(efn) = api {
48 if let Some(receiver) = &efn.sig.receiver {
49 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070050 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070051 .or_insert_with(Vec::new)
52 .push(efn);
53 }
54 }
55 }
Joel Galenson968738f2020-04-15 14:19:33 -070056
David Tolnay7db73692019-10-20 14:51:12 -040057 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070058 match api {
59 Api::Struct(strct) => {
60 out.next_section();
61 write_struct(out, strct);
62 }
Joel Galensonc03402a2020-04-23 17:31:09 -070063 Api::Enum(enm) => {
64 out.next_section();
Joel Galenson0f654ff2020-05-04 20:04:21 -070065 if types.cxx.contains(&enm.ident) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070066 check_enum(out, enm);
67 } else {
68 write_enum(out, enm);
69 }
Joel Galensonc03402a2020-04-23 17:31:09 -070070 }
David Tolnayc1fe0052020-04-17 15:15:06 -070071 Api::RustType(ety) => {
72 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070073 out.next_section();
74 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070075 }
David Tolnayc1fe0052020-04-17 15:15:06 -070076 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070077 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040078 }
79 }
80
81 if !header {
82 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070083 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040084 for api in apis {
Adrian Taylor21f0ff02020-07-21 16:21:48 -070085 let (efn, write): (_, fn(_, _, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -040086 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
87 Api::RustFunction(efn) => (efn, write_rust_function_decl),
88 _ => continue,
89 };
90 out.next_section();
Adrian Taylor21f0ff02020-07-21 16:21:48 -070091 write(out, efn, types, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -040092 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080093 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040094 }
95
96 for api in apis {
97 if let Api::RustFunction(efn) = api {
98 out.next_section();
99 write_rust_function_shim(out, efn, types);
100 }
101 }
102
103 out.next_section();
104 for name in namespace.iter().rev() {
105 writeln!(out, "}} // namespace {}", name);
106 }
107
108 if !header {
109 out.next_section();
110 write_generic_instantiations(out, types);
111 }
112
David Tolnayf4632de2020-07-31 10:46:55 -0700113 // We collected necessary includes lazily while generating the above. Now
114 // put it all together.
115 let mut full_file = OutFile::new(namespace.clone(), header);
116 let full = &mut full_file;
117 if header {
118 writeln!(full, "#pragma once");
119 }
120 write!(full, "{}", out.include);
121 full.next_section();
122 full.extend(out);
123 full_file
David Tolnay7db73692019-10-20 14:51:12 -0400124}
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(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700156 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700157 out.include.type_traits = true;
158 needs_rust_box = true;
159 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700160 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700161 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700162 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700163 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700164 needs_rust_vec = true;
165 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 Type::Str(_) => {
167 out.include.cstdint = true;
168 out.include.string = true;
169 needs_rust_str = true;
170 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700171 Type::Fn(_) => {
172 needs_rust_fn = true;
173 }
David Tolnay4770b472020-04-14 16:32:59 -0700174 Type::Slice(_) | Type::SliceRefU8(_) => {
175 needs_rust_slice = true;
176 }
David Tolnay7c295462020-04-25 12:45:07 -0700177 ty if ty == Isize => {
178 out.include.base_tsd = true;
179 needs_rust_isize = true;
180 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700181 ty if ty == RustString => {
182 out.include.array = true;
183 out.include.cstdint = true;
184 out.include.string = true;
185 needs_rust_string = true;
186 }
187 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400188 }
189 }
190
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 let mut needs_rust_error = false;
192 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800193 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800194 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700195 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800196 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700197 match api {
198 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700199 if efn.throws {
200 needs_trycatch = true;
201 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700202 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700203 let bitcopy = match arg.ty {
204 Type::RustVec(_) => true,
205 _ => arg.ty == RustString,
206 };
207 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700208 needs_unsafe_bitcopy = true;
209 break;
210 }
David Tolnay09011c32020-03-06 14:40:28 -0800211 }
212 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 Api::RustFunction(efn) if !out.header => {
214 if efn.throws {
215 out.include.exception = true;
216 needs_rust_error = true;
217 }
218 for arg in &efn.args {
219 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
220 needs_manually_drop = true;
221 break;
222 }
223 }
224 if let Some(ret) = &efn.ret {
225 if types.needs_indirect_abi(ret) {
226 needs_maybe_uninit = true;
227 }
David Tolnayf51447e2020-03-06 14:14:27 -0800228 }
229 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700230 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800231 }
232 }
233
David Tolnay750755e2020-03-01 13:04:08 -0800234 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700235 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800236
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 if needs_rust_string
238 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700239 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700240 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700241 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700242 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700244 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700245 || needs_unsafe_bitcopy
246 || needs_manually_drop
247 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700248 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 {
David Tolnay736cbca2020-03-11 16:49:18 -0700250 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800251 }
252
David Tolnay32ee9d32020-05-12 20:01:09 -0700253 if needs_rust_string || needs_rust_vec {
David Tolnayd1402742020-03-25 22:21:42 -0700254 out.next_section();
255 writeln!(out, "struct unsafe_bitcopy_t;");
256 }
257
David Tolnaye54f3382020-05-12 19:58:36 -0700258 include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
259 include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
260 include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
261 include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
262 include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
263 include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
264 include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
265 include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
266 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800267
268 if needs_manually_drop {
269 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700270 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800271 writeln!(out, "template <typename T>");
272 writeln!(out, "union ManuallyDrop {{");
273 writeln!(out, " T value;");
274 writeln!(
275 out,
276 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
277 );
278 writeln!(out, " ~ManuallyDrop() {{}}");
279 writeln!(out, "}};");
280 }
281
David Tolnay09011c32020-03-06 14:40:28 -0800282 if needs_maybe_uninit {
283 out.next_section();
284 writeln!(out, "template <typename T>");
285 writeln!(out, "union MaybeUninit {{");
286 writeln!(out, " T value;");
287 writeln!(out, " MaybeUninit() {{}}");
288 writeln!(out, " ~MaybeUninit() {{}}");
289 writeln!(out, "}};");
290 }
291
David Tolnay69601622020-04-29 18:48:36 -0700292 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700293
David Tolnay5d121442020-03-17 22:14:40 -0700294 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700295 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700296 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700297 out.include.type_traits = true;
298 out.include.utility = true;
299 writeln!(out, "class missing {{}};");
300 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700301 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700302 writeln!(out, "template <typename Try, typename Fail>");
303 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700304 writeln!(
305 out,
David Tolnay04722332020-03-18 11:31:54 -0700306 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700307 );
David Tolnay04722332020-03-18 11:31:54 -0700308 writeln!(out, " missing>::value>::type");
309 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700310 writeln!(out, " func();");
311 writeln!(out, "}} catch (const ::std::exception &e) {{");
312 writeln!(out, " fail(e.what());");
313 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700314 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700315 }
316
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800317 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400318}
319
320fn write_struct(out: &mut OutFile, strct: &Struct) {
321 for line in strct.doc.to_string().lines() {
322 writeln!(out, "//{}", line);
323 }
324 writeln!(out, "struct {} final {{", strct.ident);
325 for field in &strct.fields {
326 write!(out, " ");
327 write_type_space(out, &field.ty);
328 writeln!(out, "{};", field.ident);
329 }
330 writeln!(out, "}};");
331}
332
333fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
334 writeln!(out, "struct {};", ident);
335}
336
David Tolnay8861bee2020-01-20 18:39:24 -0800337fn write_struct_using(out: &mut OutFile, ident: &Ident) {
338 writeln!(out, "using {} = {};", ident, ident);
339}
340
David Tolnayc1fe0052020-04-17 15:15:06 -0700341fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700342 for line in ety.doc.to_string().lines() {
343 writeln!(out, "//{}", line);
344 }
345 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700346 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700347 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700348 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700349 write!(out, " ");
350 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700351 let local_name = method.ident.to_string();
352 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700353 writeln!(out, ";");
354 }
355 writeln!(out, "}};");
356}
357
Joel Galensonc03402a2020-04-23 17:31:09 -0700358fn write_enum(out: &mut OutFile, enm: &Enum) {
359 for line in enm.doc.to_string().lines() {
360 writeln!(out, "//{}", line);
361 }
David Tolnayf6a89f22020-05-10 23:39:27 -0700362 write!(out, "enum class {} : ", enm.ident);
363 write_atom(out, enm.repr);
364 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700365 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700366 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700367 }
368 writeln!(out, "}};");
369}
370
Joel Galenson905eb2e2020-05-04 14:58:14 -0700371fn check_enum(out: &mut OutFile, enm: &Enum) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700372 write!(out, "static_assert(sizeof({}) == sizeof(", enm.ident);
373 write_atom(out, enm.repr);
374 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700375 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700376 write!(out, "static_assert(static_cast<");
377 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700378 writeln!(
379 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700380 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Joel Galenson88547732020-05-05 08:23:42 -0700381 enm.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700382 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700383 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700384}
385
David Tolnayebef4a22020-03-17 15:33:47 -0700386fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
387 let mut has_cxx_throws = false;
388 for api in apis {
389 if let Api::CxxFunction(efn) = api {
390 if efn.throws {
391 has_cxx_throws = true;
392 break;
393 }
394 }
395 }
396
397 if has_cxx_throws {
398 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700399 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700400 out,
David Tolnay69601622020-04-29 18:48:36 -0700401 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700402 );
403 }
404}
405
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700406fn write_cxx_function_shim(
407 out: &mut OutFile,
408 efn: &ExternFn,
409 types: &Types,
410 impl_annotations: &Option<String>,
411) {
412 if !out.header {
413 if let Some(annotation) = impl_annotations {
414 write!(out, "{} ", annotation);
415 }
416 }
David Tolnayebef4a22020-03-17 15:33:47 -0700417 if efn.throws {
418 write!(out, "::rust::Str::Repr ");
419 } else {
David Tolnay99642622020-03-25 13:07:35 -0700420 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700421 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700422 let mangled = mangle::extern_fn(&out.namespace, efn);
423 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700424 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700425 if receiver.mutability.is_none() {
426 write!(out, "const ");
427 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700428 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700429 }
David Tolnay7db73692019-10-20 14:51:12 -0400430 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700431 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400432 write!(out, ", ");
433 }
David Tolnaya46a2372020-03-06 10:03:48 -0800434 if arg.ty == RustString {
435 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700436 } else if let Type::RustVec(_) = arg.ty {
437 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800438 }
David Tolnay7db73692019-10-20 14:51:12 -0400439 write_extern_arg(out, arg, types);
440 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700441 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400442 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700443 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400444 write!(out, ", ");
445 }
David Tolnay99642622020-03-25 13:07:35 -0700446 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400447 write!(out, "*return$");
448 }
449 writeln!(out, ") noexcept {{");
450 write!(out, " ");
451 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700452 match &efn.receiver {
453 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700454 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700455 }
David Tolnay7db73692019-10-20 14:51:12 -0400456 for (i, arg) in efn.args.iter().enumerate() {
457 if i > 0 {
458 write!(out, ", ");
459 }
460 write_type(out, &arg.ty);
461 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700462 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700463 if let Some(receiver) = &efn.receiver {
464 if receiver.mutability.is_none() {
465 write!(out, " const");
466 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700467 }
468 write!(out, " = ");
469 match &efn.receiver {
470 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700471 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700472 }
473 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400474 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700475 if efn.throws {
476 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700477 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700478 writeln!(out, " [&] {{");
479 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700480 }
David Tolnay7db73692019-10-20 14:51:12 -0400481 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700482 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400483 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700484 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400485 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700486 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400487 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700488 }
489 match &efn.ret {
490 Some(Type::Ref(_)) => write!(out, "&"),
491 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700492 Some(Type::SliceRefU8(_)) if !indirect_return => {
493 write!(out, "::rust::Slice<uint8_t>::Repr(")
494 }
David Tolnay99642622020-03-25 13:07:35 -0700495 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400496 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700497 match &efn.receiver {
498 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700499 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700500 }
David Tolnay7db73692019-10-20 14:51:12 -0400501 for (i, arg) in efn.args.iter().enumerate() {
502 if i > 0 {
503 write!(out, ", ");
504 }
505 if let Type::RustBox(_) = &arg.ty {
506 write_type(out, &arg.ty);
507 write!(out, "::from_raw({})", arg.ident);
508 } else if let Type::UniquePtr(_) = &arg.ty {
509 write_type(out, &arg.ty);
510 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800511 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800512 write!(
513 out,
514 "::rust::String(::rust::unsafe_bitcopy, *{})",
515 arg.ident,
516 );
David Tolnay313b10e2020-04-25 16:30:51 -0700517 } else if let Type::RustVec(_) = arg.ty {
518 write_type(out, &arg.ty);
519 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400520 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700521 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800522 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400523 } else {
524 write!(out, "{}", arg.ident);
525 }
526 }
527 write!(out, ")");
528 match &efn.ret {
529 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
530 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700531 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400532 _ => {}
533 }
534 if indirect_return {
535 write!(out, ")");
536 }
537 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700538 if efn.throws {
539 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700540 writeln!(out, " throw$.ptr = nullptr;");
541 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700542 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700543 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700544 writeln!(
545 out,
David Tolnay69601622020-04-29 18:48:36 -0700546 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700547 );
David Tolnay5d121442020-03-17 22:14:40 -0700548 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700549 writeln!(out, " return throw$;");
550 }
David Tolnay7db73692019-10-20 14:51:12 -0400551 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700552 for arg in &efn.args {
553 if let Type::Fn(f) = &arg.ty {
554 let var = &arg.ident;
555 write_function_pointer_trampoline(out, efn, var, f, types);
556 }
557 }
558}
559
560fn write_function_pointer_trampoline(
561 out: &mut OutFile,
562 efn: &ExternFn,
563 var: &Ident,
564 f: &Signature,
565 types: &Types,
566) {
567 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700568 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700569 let indirect_call = true;
570 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
571
572 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700573 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700574 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400575}
576
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700577fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700578 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700579 let indirect_call = false;
580 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
581}
582
583fn write_rust_function_decl_impl(
584 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700585 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 sig: &Signature,
587 types: &Types,
588 indirect_call: bool,
589) {
590 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700591 write!(out, "::rust::Str::Repr ");
592 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700593 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700594 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700595 write!(out, "{}(", link_name);
596 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700597 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700598 if receiver.mutability.is_none() {
599 write!(out, "const ");
600 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700601 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700602 needs_comma = true;
603 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700604 for arg in &sig.args {
605 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400606 write!(out, ", ");
607 }
608 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700609 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400610 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700611 if indirect_return(sig, types) {
612 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400613 write!(out, ", ");
614 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700615 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400616 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700617 needs_comma = true;
618 }
619 if indirect_call {
620 if needs_comma {
621 write!(out, ", ");
622 }
623 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400624 }
625 writeln!(out, ") noexcept;");
626}
627
628fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400629 for line in efn.doc.to_string().lines() {
630 writeln!(out, "//{}", line);
631 }
David Tolnaya73853b2020-04-20 01:19:56 -0700632 let local_name = match &efn.sig.receiver {
633 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700634 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700635 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700636 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700637 let indirect_call = false;
638 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
639}
640
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700641fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700642 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700643 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700645 indirect_call: bool,
646) {
647 write_return_type(out, &sig.ret);
648 write!(out, "{}(", local_name);
649 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400650 if i > 0 {
651 write!(out, ", ");
652 }
653 write_type_space(out, &arg.ty);
654 write!(out, "{}", arg.ident);
655 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 if indirect_call {
657 if !sig.args.is_empty() {
658 write!(out, ", ");
659 }
660 write!(out, "void *extern$");
661 }
David Tolnay1e548172020-03-16 13:37:09 -0700662 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700663 if let Some(receiver) = &sig.receiver {
664 if receiver.mutability.is_none() {
665 write!(out, " const");
666 }
667 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700668 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700669 write!(out, " noexcept");
670 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700671}
672
673fn write_rust_function_shim_impl(
674 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700675 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700676 sig: &Signature,
677 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700678 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700679 indirect_call: bool,
680) {
681 if out.header && sig.receiver.is_some() {
682 // We've already defined this inside the struct.
683 return;
684 }
David Tolnaya73853b2020-04-20 01:19:56 -0700685 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400686 if out.header {
687 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700688 return;
David Tolnay7db73692019-10-20 14:51:12 -0400689 }
David Tolnay439cde22020-04-20 00:46:25 -0700690 writeln!(out, " {{");
691 for arg in &sig.args {
692 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
693 out.include.utility = true;
694 write!(out, " ::rust::ManuallyDrop<");
695 write_type(out, &arg.ty);
696 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
697 }
698 }
699 write!(out, " ");
700 let indirect_return = indirect_return(sig, types);
701 if indirect_return {
702 write!(out, "::rust::MaybeUninit<");
703 write_type(out, sig.ret.as_ref().unwrap());
704 writeln!(out, "> return$;");
705 write!(out, " ");
706 } else if let Some(ret) = &sig.ret {
707 write!(out, "return ");
708 match ret {
709 Type::RustBox(_) => {
710 write_type(out, ret);
711 write!(out, "::from_raw(");
712 }
713 Type::UniquePtr(_) => {
714 write_type(out, ret);
715 write!(out, "(");
716 }
717 Type::Ref(_) => write!(out, "*"),
718 _ => {}
719 }
720 }
721 if sig.throws {
722 write!(out, "::rust::Str::Repr error$ = ");
723 }
724 write!(out, "{}(", invoke);
725 if sig.receiver.is_some() {
726 write!(out, "*this");
727 }
728 for (i, arg) in sig.args.iter().enumerate() {
729 if i > 0 || sig.receiver.is_some() {
730 write!(out, ", ");
731 }
732 match &arg.ty {
733 Type::Str(_) => write!(out, "::rust::Str::Repr("),
734 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
735 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
736 _ => {}
737 }
738 write!(out, "{}", arg.ident);
739 match &arg.ty {
740 Type::RustBox(_) => write!(out, ".into_raw()"),
741 Type::UniquePtr(_) => write!(out, ".release()"),
742 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
743 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
744 _ => {}
745 }
746 }
747 if indirect_return {
748 if !sig.args.is_empty() {
749 write!(out, ", ");
750 }
751 write!(out, "&return$.value");
752 }
753 if indirect_call {
754 if !sig.args.is_empty() || indirect_return {
755 write!(out, ", ");
756 }
757 write!(out, "extern$");
758 }
759 write!(out, ")");
760 if let Some(ret) = &sig.ret {
761 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
762 write!(out, ")");
763 }
764 }
765 writeln!(out, ";");
766 if sig.throws {
767 writeln!(out, " if (error$.ptr) {{");
768 writeln!(out, " throw ::rust::Error(error$);");
769 writeln!(out, " }}");
770 }
771 if indirect_return {
772 out.include.utility = true;
773 writeln!(out, " return ::std::move(return$.value);");
774 }
775 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400776}
777
778fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
779 match ty {
780 None => write!(out, "void "),
781 Some(ty) => write_type_space(out, ty),
782 }
783}
784
David Tolnay75dca2e2020-03-25 20:17:52 -0700785fn indirect_return(sig: &Signature, types: &Types) -> bool {
786 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700787 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700788 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700789}
790
David Tolnay99642622020-03-25 13:07:35 -0700791fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
792 match ty {
793 Type::RustBox(ty) | Type::UniquePtr(ty) => {
794 write_type_space(out, &ty.inner);
795 write!(out, "*");
796 }
797 Type::Ref(ty) => {
798 if ty.mutability.is_none() {
799 write!(out, "const ");
800 }
801 write_type(out, &ty.inner);
802 write!(out, " *");
803 }
804 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700805 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700806 _ => write_type(out, ty),
807 }
808}
809
810fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
811 write_indirect_return_type(out, ty);
812 match ty {
813 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700814 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700815 _ => write_space_after_type(out, ty),
816 }
817}
818
819fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400820 match ty {
821 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
822 write_type_space(out, &ty.inner);
823 write!(out, "*");
824 }
David Tolnay4a441222020-01-25 16:24:27 -0800825 Some(Type::Ref(ty)) => {
826 if ty.mutability.is_none() {
827 write!(out, "const ");
828 }
829 write_type(out, &ty.inner);
830 write!(out, " *");
831 }
David Tolnay750755e2020-03-01 13:04:08 -0800832 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700833 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400834 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
835 _ => write_return_type(out, ty),
836 }
837}
838
839fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
840 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700841 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400842 write_type_space(out, &ty.inner);
843 write!(out, "*");
844 }
David Tolnay750755e2020-03-01 13:04:08 -0800845 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700846 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400847 _ => write_type_space(out, &arg.ty),
848 }
849 if types.needs_indirect_abi(&arg.ty) {
850 write!(out, "*");
851 }
852 write!(out, "{}", arg.ident);
853}
854
855fn write_type(out: &mut OutFile, ty: &Type) {
856 match ty {
857 Type::Ident(ident) => match Atom::from(ident) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700858 Some(atom) => write_atom(out, atom),
David Tolnay7db73692019-10-20 14:51:12 -0400859 None => write!(out, "{}", ident),
860 },
861 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800862 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400863 write_type(out, &ty.inner);
864 write!(out, ">");
865 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700866 Type::RustVec(ty) => {
867 write!(out, "::rust::Vec<");
868 write_type(out, &ty.inner);
869 write!(out, ">");
870 }
David Tolnay7db73692019-10-20 14:51:12 -0400871 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800872 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400873 write_type(out, &ptr.inner);
874 write!(out, ">");
875 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700876 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700877 write!(out, "::std::vector<");
878 write_type(out, &ty.inner);
879 write!(out, ">");
880 }
David Tolnay7db73692019-10-20 14:51:12 -0400881 Type::Ref(r) => {
882 if r.mutability.is_none() {
883 write!(out, "const ");
884 }
885 write_type(out, &r.inner);
886 write!(out, " &");
887 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700888 Type::Slice(_) => {
889 // For now, only U8 slices are supported, which are covered separately below
890 unreachable!()
891 }
David Tolnay7db73692019-10-20 14:51:12 -0400892 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800893 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400894 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700895 Type::SliceRefU8(_) => {
896 write!(out, "::rust::Slice<uint8_t>");
897 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700898 Type::Fn(f) => {
899 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
900 match &f.ret {
901 Some(ret) => write_type(out, ret),
902 None => write!(out, "void"),
903 }
904 write!(out, "(");
905 for (i, arg) in f.args.iter().enumerate() {
906 if i > 0 {
907 write!(out, ", ");
908 }
909 write_type(out, &arg.ty);
910 }
911 write!(out, ")>");
912 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700913 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400914 }
915}
916
David Tolnayf6a89f22020-05-10 23:39:27 -0700917fn write_atom(out: &mut OutFile, atom: Atom) {
918 match atom {
919 Bool => write!(out, "bool"),
920 U8 => write!(out, "uint8_t"),
921 U16 => write!(out, "uint16_t"),
922 U32 => write!(out, "uint32_t"),
923 U64 => write!(out, "uint64_t"),
924 Usize => write!(out, "size_t"),
925 I8 => write!(out, "int8_t"),
926 I16 => write!(out, "int16_t"),
927 I32 => write!(out, "int32_t"),
928 I64 => write!(out, "int64_t"),
929 Isize => write!(out, "::rust::isize"),
930 F32 => write!(out, "float"),
931 F64 => write!(out, "double"),
932 CxxString => write!(out, "::std::string"),
933 RustString => write!(out, "::rust::String"),
934 }
935}
936
David Tolnay7db73692019-10-20 14:51:12 -0400937fn write_type_space(out: &mut OutFile, ty: &Type) {
938 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700939 write_space_after_type(out, ty);
940}
941
942fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400943 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700944 Type::Ident(_)
945 | Type::RustBox(_)
946 | Type::UniquePtr(_)
947 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700948 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700949 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700950 | Type::SliceRefU8(_)
951 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400952 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700953 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400954 }
955}
956
David Tolnaycd08c442020-04-25 10:16:33 -0700957// Only called for legal referent types of unique_ptr and element types of
958// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700959fn to_typename(namespace: &Namespace, ty: &Type) -> String {
960 match ty {
961 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700962 let mut path = String::new();
963 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700964 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700965 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700966 }
David Tolnaycd08c442020-04-25 10:16:33 -0700967 path += &ident.to_string();
968 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700969 }
970 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700971 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700972 }
973}
974
David Tolnayacdf20a2020-04-25 12:40:53 -0700975// Only called for legal referent types of unique_ptr and element types of
976// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700977fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
978 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700979 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700980 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700981 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700982 }
983}
984
David Tolnay7db73692019-10-20 14:51:12 -0400985fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400986 out.begin_block("extern \"C\"");
987 for ty in types {
988 if let Type::RustBox(ty) = ty {
989 if let Type::Ident(inner) = &ty.inner {
990 out.next_section();
991 write_rust_box_extern(out, inner);
992 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700993 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700994 if let Type::Ident(inner) = &ty.inner {
995 if Atom::from(inner).is_none() {
996 out.next_section();
997 write_rust_vec_extern(out, inner);
998 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700999 }
David Tolnay7db73692019-10-20 14:51:12 -04001000 } else if let Type::UniquePtr(ptr) = ty {
1001 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001002 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
David Tolnay7db73692019-10-20 14:51:12 -04001003 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001004 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001005 }
1006 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001007 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001008 if let Type::Ident(inner) = &ptr.inner {
David Tolnay2b821e62020-05-07 22:55:28 -07001009 if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001010 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001011 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001012 }
1013 }
1014 }
1015 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001016 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001017
David Tolnay750755e2020-03-01 13:04:08 -08001018 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001019 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001020 for ty in types {
1021 if let Type::RustBox(ty) = ty {
1022 if let Type::Ident(inner) = &ty.inner {
1023 write_rust_box_impl(out, inner);
1024 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001025 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001026 if let Type::Ident(inner) = &ty.inner {
1027 if Atom::from(inner).is_none() {
1028 write_rust_vec_impl(out, inner);
1029 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001030 }
David Tolnay7db73692019-10-20 14:51:12 -04001031 }
1032 }
David Tolnay69601622020-04-29 18:48:36 -07001033 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001034 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001035}
1036
1037fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1038 let mut inner = String::new();
1039 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001040 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001041 inner += "::";
1042 }
1043 inner += &ident.to_string();
1044 let instance = inner.replace("::", "$");
1045
David Tolnay69601622020-04-29 18:48:36 -07001046 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1047 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001048 writeln!(
1049 out,
David Tolnay69601622020-04-29 18:48:36 -07001050 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001051 instance, inner,
1052 );
1053 writeln!(
1054 out,
David Tolnay69601622020-04-29 18:48:36 -07001055 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001056 instance, inner,
1057 );
David Tolnay69601622020-04-29 18:48:36 -07001058 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001059}
1060
David Tolnay6787be62020-04-25 11:01:02 -07001061fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1062 let element = Type::Ident(element.clone());
1063 let inner = to_typename(&out.namespace, &element);
1064 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001065
David Tolnay69601622020-04-29 18:48:36 -07001066 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1067 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001068 writeln!(
1069 out,
David Tolnay69601622020-04-29 18:48:36 -07001070 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001071 instance, inner,
1072 );
1073 writeln!(
1074 out,
David Tolnay69601622020-04-29 18:48:36 -07001075 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001076 instance, inner,
1077 );
1078 writeln!(
1079 out,
David Tolnay69601622020-04-29 18:48:36 -07001080 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001081 instance, inner,
1082 );
David Tolnay219c0792020-04-24 20:31:37 -07001083 writeln!(
1084 out,
David Tolnay69601622020-04-29 18:48:36 -07001085 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001086 inner, instance,
1087 );
David Tolnay503d0192020-04-24 22:18:56 -07001088 writeln!(
1089 out,
David Tolnay69601622020-04-29 18:48:36 -07001090 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001091 instance,
1092 );
David Tolnay69601622020-04-29 18:48:36 -07001093 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001094}
1095
David Tolnay7db73692019-10-20 14:51:12 -04001096fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1097 let mut inner = String::new();
1098 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001099 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001100 inner += "::";
1101 }
1102 inner += &ident.to_string();
1103 let instance = inner.replace("::", "$");
1104
1105 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001106 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001107 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001108 writeln!(out, "}}");
1109
1110 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001111 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001112 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001113 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001114}
1115
David Tolnay6787be62020-04-25 11:01:02 -07001116fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1117 let element = Type::Ident(element.clone());
1118 let inner = to_typename(&out.namespace, &element);
1119 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001120
Myron Ahneba35cf2020-02-05 19:41:51 +07001121 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001122 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001123 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001124 writeln!(out, "}}");
1125
1126 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001127 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1128 writeln!(
1129 out,
David Tolnay69601622020-04-29 18:48:36 -07001130 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001131 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001132 );
1133 writeln!(out, "}}");
1134
1135 writeln!(out, "template <>");
1136 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001137 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001138 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001139
1140 writeln!(out, "template <>");
1141 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1142 writeln!(
1143 out,
David Tolnay69601622020-04-29 18:48:36 -07001144 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001145 instance,
1146 );
1147 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001148
1149 writeln!(out, "template <>");
1150 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001151 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001152 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001153}
1154
David Tolnay63da4d32020-04-25 09:41:12 -07001155fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1156 let ty = Type::Ident(ident.clone());
1157 let instance = to_mangled(&out.namespace, &ty);
1158
David Tolnay69601622020-04-29 18:48:36 -07001159 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1160 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001161
1162 write_unique_ptr_common(out, &ty, types);
1163
David Tolnay69601622020-04-29 18:48:36 -07001164 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001165}
1166
1167// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1168fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001169 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001170 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001171 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001172 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001173
David Tolnay63da4d32020-04-25 09:41:12 -07001174 let can_construct_from_value = match ty {
1175 Type::Ident(ident) => types.structs.contains_key(ident),
1176 _ => false,
1177 };
1178
David Tolnay7db73692019-10-20 14:51:12 -04001179 writeln!(
1180 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001181 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001182 inner,
1183 );
1184 writeln!(
1185 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001186 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001187 inner,
1188 );
1189 writeln!(
1190 out,
David Tolnay69601622020-04-29 18:48:36 -07001191 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001192 instance, inner,
1193 );
David Tolnay7e219b82020-03-01 13:14:51 -08001194 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001195 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001196 if can_construct_from_value {
1197 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001198 out,
David Tolnay69601622020-04-29 18:48:36 -07001199 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001200 instance, inner, inner,
1201 );
David Tolnay63da4d32020-04-25 09:41:12 -07001202 writeln!(
1203 out,
1204 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1205 inner, inner,
1206 );
1207 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001208 }
David Tolnay7db73692019-10-20 14:51:12 -04001209 writeln!(
1210 out,
David Tolnay69601622020-04-29 18:48:36 -07001211 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001212 instance, inner, inner,
1213 );
David Tolnay7e219b82020-03-01 13:14:51 -08001214 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001215 writeln!(out, "}}");
1216 writeln!(
1217 out,
David Tolnay69601622020-04-29 18:48:36 -07001218 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001219 inner, instance, inner,
1220 );
1221 writeln!(out, " return ptr.get();");
1222 writeln!(out, "}}");
1223 writeln!(
1224 out,
David Tolnay69601622020-04-29 18:48:36 -07001225 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001226 inner, instance, inner,
1227 );
1228 writeln!(out, " return ptr.release();");
1229 writeln!(out, "}}");
1230 writeln!(
1231 out,
David Tolnay69601622020-04-29 18:48:36 -07001232 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001233 instance, inner,
1234 );
1235 writeln!(out, " ptr->~unique_ptr();");
1236 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001237}
Myron Ahneba35cf2020-02-05 19:41:51 +07001238
David Tolnay92105da2020-04-25 11:15:31 -07001239fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001240 let element = Type::Ident(element.clone());
1241 let inner = to_typename(&out.namespace, &element);
1242 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001243
David Tolnay69601622020-04-29 18:48:36 -07001244 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1245 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001246 writeln!(
1247 out,
David Tolnay69601622020-04-29 18:48:36 -07001248 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001249 instance, inner,
1250 );
1251 writeln!(out, " return s.size();");
1252 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001253 writeln!(
1254 out,
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001255 "const {} *cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001256 inner, instance, inner,
1257 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001258 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001259 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001260
1261 write_unique_ptr_common(out, vector_ty, types);
1262
David Tolnay69601622020-04-29 18:48:36 -07001263 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001264}