1use crate::gen::block::Block;
2use crate::gen::guard::Guard;
3use crate::gen::nested::NamespaceEntries;
4use crate::gen::out::OutFile;
5use crate::gen::{builtin, include, pragma, Opt};
6use crate::syntax::atom::Atom::{self, *};
7use crate::syntax::discriminant::{Discriminant, Limits};
8use crate::syntax::instantiate::{ImplKey, NamedImplKey};
9use crate::syntax::map::UnorderedMap as Map;
10use crate::syntax::namespace::Namespace;
11use crate::syntax::primitive::{self, PrimitiveKind};
12use crate::syntax::set::UnorderedSet;
13use crate::syntax::symbol::{self, Symbol};
14use crate::syntax::trivial::{self, TrivialReason};
15use crate::syntax::{
16 derive, mangle, Api, Doc, Enum, ExternFn, ExternType, FnKind, Lang, Pair, Signature, Struct,
17 Trait, Type, TypeAlias, Types, Var,
18};
19use proc_macro2::Ident;
20
21pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
22 let mut out_file = OutFile::new(header, opt, types);
23 let out = &mut out_file;
24
25 pick_includes_and_builtins(out, apis);
26 out.include.extend(&opt.include);
27
28 write_macros(out, apis);
29 write_forward_declarations(out, apis);
30 write_data_structures(out, apis);
31 write_functions(out, apis);
32 write_generic_instantiations(out);
33
34 builtin::write(out);
35 pragma::write(out);
36 include::write(out);
37
38 out_file.content()
39}
40
41fn write_macros(out: &mut OutFile, apis: &[Api]) {
42 let mut needs_default_value = false;
43 for api in apis {
44 if let Api::Struct(strct) = api {
45 if !out.types.cxx.contains(&strct.name.rust) {
46 for field in &strct.fields {
47 needs_default_value |= primitive::kind(&field.ty).is_some();
48 }
49 }
50 }
51 }
52
53 if needs_default_value {
54 out.next_section();
55 writeln!(out, "#if __cplusplus >= 201402L");
56 writeln!(out, "#define CXX_DEFAULT_VALUE(value) = value");
57 writeln!(out, "#else");
58 writeln!(out, "#define CXX_DEFAULT_VALUE(value)");
59 writeln!(out, "#endif");
60 }
61}
62
63fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
64 let needs_forward_declaration = |api: &&Api| match api {
65 Api::Struct(_) | Api::CxxType(_) | Api::RustType(_) => true,
66 Api::Enum(enm) => !out.types.cxx.contains(&enm.name.rust),
67 _ => false,
68 };
69
70 let apis_by_namespace =
71 NamespaceEntries::new(apis.iter().filter(needs_forward_declaration).collect());
72
73 out.next_section();
74 write(out, &apis_by_namespace, 0);
75
76 fn write(out: &mut OutFile, ns_entries: &NamespaceEntries, indent: usize) {
77 let apis = ns_entries.direct_content();
78
79 for api in apis {
80 write!(out, "{:1$}", "", indent);
81 match api {
82 Api::Struct(strct) => write_struct_decl(out, &strct.name),
83 Api::Enum(enm) => write_enum_decl(out, enm),
84 Api::CxxType(ety) => write_struct_using(out, &ety.name),
85 Api::RustType(ety) => write_struct_decl(out, &ety.name),
86 _ => unreachable!(),
87 }
88 }
89
90 for (namespace, nested_ns_entries) in ns_entries.nested_content() {
91 writeln!(out, "{:2$}namespace {} {{", "", namespace, indent);
92 write(out, nested_ns_entries, indent + 2);
93 writeln!(out, "{:1$}}}", "", indent);
94 }
95 }
96}
97
98fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
99 let mut methods_for_type = Map::new();
100 for api in apis {
101 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
102 if let Some(self_type) = efn.self_type() {
103 methods_for_type
104 .entry(self_type)
105 .or_insert_with(Vec::new)
106 .push(efn);
107 }
108 }
109 }
110
111 let mut structs_written = UnorderedSet::new();
112 let mut toposorted_structs = out.types.toposorted_structs.iter();
113 for api in apis {
114 match api {
115 Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
116 for next in &mut toposorted_structs {
117 if !out.types.cxx.contains(&next.name.rust) {
118 out.next_section();
119 let methods = methods_for_type
120 .get(&next.name.rust)
121 .map(Vec::as_slice)
122 .unwrap_or_default();
123 write_struct(out, next, methods);
124 }
125 structs_written.insert(&next.name.rust);
126 if next.name.rust == strct.name.rust {
127 break;
128 }
129 }
130 }
131 Api::Enum(enm) => {
132 out.next_section();
133 if out.types.cxx.contains(&enm.name.rust) {
134 check_enum(out, enm);
135 } else {
136 write_enum(out, enm);
137 }
138 }
139 Api::RustType(ety) => {
140 out.next_section();
141 let methods = methods_for_type
142 .get(&ety.name.rust)
143 .map(Vec::as_slice)
144 .unwrap_or_default();
145 write_opaque_type(out, ety, methods);
146 }
147 _ => {}
148 }
149 }
150
151 if out.header {
152 return;
153 }
154
155 out.set_namespace(Default::default());
156
157 out.next_section();
158 for api in apis {
159 if let Api::TypeAlias(ety) = api {
160 if let Some(reasons) = out.types.required_trivial.get(&ety.name.rust) {
161 check_trivial_extern_type(out, ety, reasons);
162 }
163 }
164 }
165}
166
167fn write_functions<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
168 if !out.header {
169 for api in apis {
170 match api {
171 Api::Struct(strct) => write_struct_operator_decls(out, strct),
172 Api::RustType(ety) => write_opaque_type_layout_decls(out, ety),
173 Api::CxxFunction(efn) => write_cxx_function_shim(out, efn),
174 Api::RustFunction(efn) => write_rust_function_decl(out, efn),
175 _ => {}
176 }
177 }
178
179 write_std_specializations(out, apis);
180 }
181
182 for api in apis {
183 match api {
184 Api::Struct(strct) => write_struct_operators(out, strct),
185 Api::RustType(ety) => write_opaque_type_layout(out, ety),
186 Api::RustFunction(efn) => {
187 out.next_section();
188 write_rust_function_shim(out, efn);
189 }
190 _ => {}
191 }
192 }
193}
194
195fn write_std_specializations(out: &mut OutFile, apis: &[Api]) {
196 out.set_namespace(Default::default());
197 out.begin_block(Block::Namespace("std"));
198
199 for api in apis {
200 if let Api::Struct(strct) = api {
201 if derive::contains(&strct.derives, Trait::Hash) {
202 out.next_section();
203 out.include.cstddef = true;
204 out.include.functional = true;
205 out.pragma.dollar_in_identifier = true;
206 let qualified = strct.name.to_fully_qualified();
207 writeln!(out, "template <> struct hash<{}> {{", qualified);
208 writeln!(
209 out,
210 " ::std::size_t operator()({} const &self) const noexcept {{",
211 qualified,
212 );
213 let link_name = mangle::operator(&strct.name, "hash");
214 write!(out, " return ::");
215 for name in &strct.name.namespace {
216 write!(out, "{}::", name);
217 }
218 writeln!(out, "{}(self);", link_name);
219 writeln!(out, " }}");
220 writeln!(out, "}};");
221 }
222 }
223 }
224
225 out.end_block(Block::Namespace("std"));
226}
227
228fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
229 for api in apis {
230 if let Api::Include(include) = api {
231 out.include.insert(include);
232 }
233 }
234
235 for ty in out.types {
236 match ty {
237 Type::Ident(ident) => match Atom::from(&ident.rust) {
238 Some(U8 | U16 | U32 | U64 | I8 | I16 | I32 | I64) => out.include.cstdint = true,
239 Some(Usize) => out.include.cstddef = true,
240 Some(Isize) => out.builtin.rust_isize = true,
241 Some(CxxString) => out.include.string = true,
242 Some(RustString) => out.builtin.rust_string = true,
243 Some(Bool | Char | F32 | F64) | None => {}
244 },
245 Type::RustBox(_) => out.builtin.rust_box = true,
246 Type::RustVec(_) => out.builtin.rust_vec = true,
247 Type::UniquePtr(_) => out.include.memory = true,
248 Type::SharedPtr(_) | Type::WeakPtr(_) => out.include.memory = true,
249 Type::Str(_) => out.builtin.rust_str = true,
250 Type::CxxVector(_) => out.include.vector = true,
251 Type::Fn(_) => out.builtin.rust_fn = true,
252 Type::SliceRef(_) => out.builtin.rust_slice = true,
253 Type::Array(_) => out.include.array = true,
254 Type::Ref(_) | Type::Void(_) | Type::Ptr(_) => {}
255 }
256 }
257}
258
259fn write_doc(out: &mut OutFile, indent: &str, doc: &Doc) {
260 let mut lines = 0;
261 for line in doc.to_string().lines() {
262 if out.opt.doxygen {
263 writeln!(out, "{}///{}", indent, line);
264 } else {
265 writeln!(out, "{}//{}", indent, line);
266 }
267 lines += 1;
268 }
269 if out.opt.doxygen && lines == 1 {
274 writeln!(out, "{}///", indent);
275 }
276}
277
278fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
279 let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
280 let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
281
282 out.set_namespace(&strct.name.namespace);
283 let guard = Guard::new(out, "CXXBRIDGE1_STRUCT", &strct.name);
284 writeln!(out, "#ifndef {}", guard);
285 writeln!(out, "#define {}", guard);
286 write_doc(out, "", &strct.doc);
287 write!(out, "struct");
288 if let Some(align) = &strct.align {
289 out.builtin.alignmax = true;
290 writeln!(out, " alignas(::rust::repr::alignmax<");
291 writeln!(out, " {},", align.base10_parse::<u32>().unwrap());
292 for (i, field) in strct.fields.iter().enumerate() {
293 write!(out, " alignof(");
294 write_type(out, &field.ty);
295 write!(out, ")");
296 if i + 1 != strct.fields.len() {
297 write!(out, ",");
298 }
299 writeln!(out);
300 }
301 write!(out, ">)");
302 }
303 writeln!(out, " {} final {{", strct.name.cxx);
304
305 for field in &strct.fields {
306 write_doc(out, " ", &field.doc);
307 write!(out, " ");
308 write_type_space(out, &field.ty);
309 write!(out, "{}", field.name.cxx);
310 if let Some(primitive) = primitive::kind(&field.ty) {
311 let default_value = match primitive {
312 PrimitiveKind::Boolean => "false",
313 PrimitiveKind::Number => "0",
314 PrimitiveKind::Pointer => "nullptr",
315 };
316 write!(out, " CXX_DEFAULT_VALUE({})", default_value);
317 }
318 writeln!(out, ";");
319 }
320
321 out.next_section();
322
323 for method in methods {
324 if !method.doc.is_empty() {
325 out.next_section();
326 }
327 write_doc(out, " ", &method.doc);
328 write!(out, " ");
329 let local_name = method.name.cxx.to_string();
330 let sig = &method.sig;
331 let in_class = true;
332 let indirect_call = false;
333 let main = false;
334 write_rust_function_shim_decl(out, &local_name, sig, in_class, indirect_call, main);
335 writeln!(out, ";");
336 if !method.doc.is_empty() {
337 out.next_section();
338 }
339 }
340
341 if operator_eq {
342 writeln!(
343 out,
344 " bool operator==({} const &) const noexcept;",
345 strct.name.cxx,
346 );
347 writeln!(
348 out,
349 " bool operator!=({} const &) const noexcept;",
350 strct.name.cxx,
351 );
352 }
353
354 if operator_ord {
355 writeln!(
356 out,
357 " bool operator<({} const &) const noexcept;",
358 strct.name.cxx,
359 );
360 writeln!(
361 out,
362 " bool operator<=({} const &) const noexcept;",
363 strct.name.cxx,
364 );
365 writeln!(
366 out,
367 " bool operator>({} const &) const noexcept;",
368 strct.name.cxx,
369 );
370 writeln!(
371 out,
372 " bool operator>=({} const &) const noexcept;",
373 strct.name.cxx,
374 );
375 }
376
377 out.include.type_traits = true;
378 writeln!(out, " using IsRelocatable = ::std::true_type;");
379
380 writeln!(out, "}};");
381 writeln!(out, "#endif // {}", guard);
382}
383
384fn write_struct_decl(out: &mut OutFile, ident: &Pair) {
385 writeln!(out, "struct {};", ident.cxx);
386}
387
388fn write_enum_decl(out: &mut OutFile, enm: &Enum) {
389 write!(out, "enum class {} : ", enm.name.cxx);
390 write_atom(out, enm.repr.atom);
391 writeln!(out, ";");
392}
393
394fn write_struct_using(out: &mut OutFile, ident: &Pair) {
395 writeln!(out, "using {} = {};", ident.cxx, ident.to_fully_qualified());
396}
397
398fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[&ExternFn]) {
399 out.set_namespace(&ety.name.namespace);
400 let guard = Guard::new(out, "CXXBRIDGE1_STRUCT", &ety.name);
401 writeln!(out, "#ifndef {}", guard);
402 writeln!(out, "#define {}", guard);
403 write_doc(out, "", &ety.doc);
404
405 out.builtin.opaque = true;
406 writeln!(
407 out,
408 "struct {} final : public ::rust::Opaque {{",
409 ety.name.cxx,
410 );
411
412 for (i, method) in methods.iter().enumerate() {
413 if i > 0 && !method.doc.is_empty() {
414 out.next_section();
415 }
416 write_doc(out, " ", &method.doc);
417 write!(out, " ");
418 let local_name = method.name.cxx.to_string();
419 let sig = &method.sig;
420 let in_class = true;
421 let indirect_call = false;
422 let main = false;
423 write_rust_function_shim_decl(out, &local_name, sig, in_class, indirect_call, main);
424 writeln!(out, ";");
425 if !method.doc.is_empty() {
426 out.next_section();
427 }
428 }
429
430 writeln!(out, " ~{}() = delete;", ety.name.cxx);
431 writeln!(out);
432
433 out.builtin.layout = true;
434 out.include.cstddef = true;
435 writeln!(out, "private:");
436 writeln!(out, " friend ::rust::layout;");
437 writeln!(out, " struct layout {{");
438 writeln!(out, " static ::std::size_t size() noexcept;");
439 writeln!(out, " static ::std::size_t align() noexcept;");
440 writeln!(out, " }};");
441 writeln!(out, "}};");
442 writeln!(out, "#endif // {}", guard);
443}
444
445fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
446 out.set_namespace(&enm.name.namespace);
447 let guard = Guard::new(out, "CXXBRIDGE1_ENUM", &enm.name);
448 writeln!(out, "#ifndef {}", guard);
449 writeln!(out, "#define {}", guard);
450
451 write_doc(out, "", &enm.doc);
452 write!(out, "enum class {} : ", enm.name.cxx);
453 write_atom(out, enm.repr.atom);
454 writeln!(out, " {{");
455 for variant in &enm.variants {
456 write_doc(out, " ", &variant.doc);
457 write!(out, " {} = ", variant.name.cxx);
458 write_discriminant(out, enm.repr.atom, variant.discriminant);
459 writeln!(out, ",");
460 }
461 writeln!(out, "}};");
462
463 if out.header {
464 write_enum_operators(out, enm);
465 }
466
467 writeln!(out, "#endif // {}", guard);
468}
469
470fn check_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
471 out.set_namespace(&enm.name.namespace);
472 out.include.type_traits = true;
473 writeln!(
474 out,
475 "static_assert(::std::is_enum<{}>::value, \"expected enum\");",
476 enm.name.cxx,
477 );
478 write!(out, "static_assert(sizeof({}) == sizeof(", enm.name.cxx);
479 write_atom(out, enm.repr.atom);
480 writeln!(out, "), \"incorrect size\");");
481 for variant in &enm.variants {
482 write!(out, "static_assert(static_cast<");
483 write_atom(out, enm.repr.atom);
484 writeln!(out, ">({}::{}) == ", enm.name.cxx, variant.name.cxx);
485 write_discriminant(out, enm.repr.atom, variant.discriminant);
486 writeln!(out, ", \"disagrees with the value in #[cxx::bridge]\");");
487 }
488
489 if out.header
490 && (derive::contains(&enm.derives, Trait::BitAnd)
491 || derive::contains(&enm.derives, Trait::BitOr)
492 || derive::contains(&enm.derives, Trait::BitXor))
493 {
494 out.next_section();
495 let guard = Guard::new(out, "CXXBRIDGE1_ENUM", &enm.name);
496 writeln!(out, "#ifndef {}", guard);
497 writeln!(out, "#define {}", guard);
498 out.suppress_next_section();
499 write_enum_operators(out, enm);
500 writeln!(out, "#endif // {}", guard);
501 }
502}
503
504fn write_discriminant(out: &mut OutFile, repr: Atom, discriminant: Discriminant) {
505 let limits = Limits::of(repr).unwrap();
506 if discriminant == limits.min && limits.min < Discriminant::zero() {
507 out.include.limits = true;
508 write!(out, "::std::numeric_limits<");
509 write_atom(out, repr);
510 write!(out, ">::min()");
511 } else {
512 write!(out, "{}", discriminant);
513 }
514}
515
516fn write_enum_operators(out: &mut OutFile, enm: &Enum) {
517 if derive::contains(&enm.derives, Trait::BitAnd) {
518 out.next_section();
519 writeln!(
520 out,
521 "inline {} operator&({} lhs, {} rhs) {{",
522 enm.name.cxx, enm.name.cxx, enm.name.cxx,
523 );
524 write!(out, " return static_cast<{}>(static_cast<", enm.name.cxx);
525 write_atom(out, enm.repr.atom);
526 write!(out, ">(lhs) & static_cast<");
527 write_atom(out, enm.repr.atom);
528 writeln!(out, ">(rhs));");
529 writeln!(out, "}}");
530 }
531
532 if derive::contains(&enm.derives, Trait::BitOr) {
533 out.next_section();
534 writeln!(
535 out,
536 "inline {} operator|({} lhs, {} rhs) {{",
537 enm.name.cxx, enm.name.cxx, enm.name.cxx,
538 );
539 write!(out, " return static_cast<{}>(static_cast<", enm.name.cxx);
540 write_atom(out, enm.repr.atom);
541 write!(out, ">(lhs) | static_cast<");
542 write_atom(out, enm.repr.atom);
543 writeln!(out, ">(rhs));");
544 writeln!(out, "}}");
545 }
546
547 if derive::contains(&enm.derives, Trait::BitXor) {
548 out.next_section();
549 writeln!(
550 out,
551 "inline {} operator^({} lhs, {} rhs) {{",
552 enm.name.cxx, enm.name.cxx, enm.name.cxx,
553 );
554 write!(out, " return static_cast<{}>(static_cast<", enm.name.cxx);
555 write_atom(out, enm.repr.atom);
556 write!(out, ">(lhs) ^ static_cast<");
557 write_atom(out, enm.repr.atom);
558 writeln!(out, ">(rhs));");
559 writeln!(out, "}}");
560 }
561}
562
563fn check_trivial_extern_type(out: &mut OutFile, alias: &TypeAlias, reasons: &[TrivialReason]) {
564 let id = alias.name.to_fully_qualified();
598 out.builtin.relocatable = true;
599
600 let mut rust_type_ok = true;
601 let mut array_ok = true;
602 for reason in reasons {
603 rust_type_ok &= match reason {
606 TrivialReason::BoxTarget { .. }
607 | TrivialReason::VecElement { .. }
608 | TrivialReason::SliceElement { .. } => true,
609 TrivialReason::StructField(_)
610 | TrivialReason::FunctionArgument(_)
611 | TrivialReason::FunctionReturn(_) => false,
612 };
613 array_ok &= match reason {
624 TrivialReason::StructField(_) | TrivialReason::VecElement { .. } => true,
625 TrivialReason::FunctionArgument(_)
626 | TrivialReason::FunctionReturn(_)
627 | TrivialReason::BoxTarget { .. }
628 | TrivialReason::SliceElement { .. } => false,
629 };
630 }
631
632 writeln!(out, "static_assert(");
633 write!(out, " ");
634 if rust_type_ok {
635 out.include.type_traits = true;
636 out.builtin.opaque = true;
637 write!(out, "::std::is_base_of<::rust::Opaque, {}>::value || ", id);
638 }
639 if array_ok {
640 out.builtin.relocatable_or_array = true;
641 writeln!(out, "::rust::IsRelocatableOrArray<{}>::value,", id);
642 } else {
643 writeln!(out, "::rust::IsRelocatable<{}>::value,", id);
644 }
645 writeln!(
646 out,
647 " \"type {} should be trivially move constructible and trivially destructible in C++ to be used as {} in Rust\");",
648 id.trim_start_matches("::"),
649 trivial::as_what(&alias.name, reasons),
650 );
651}
652
653fn write_struct_operator_decls<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
654 out.set_namespace(&strct.name.namespace);
655 out.begin_block(Block::ExternC);
656
657 if derive::contains(&strct.derives, Trait::PartialEq) {
658 out.pragma.dollar_in_identifier = true;
659 out.pragma.missing_declarations = true;
660 let link_name = mangle::operator(&strct.name, "eq");
661 writeln!(
662 out,
663 "bool {}({1} const &, {1} const &) noexcept;",
664 link_name, strct.name.cxx,
665 );
666
667 if !derive::contains(&strct.derives, Trait::Eq) {
668 let link_name = mangle::operator(&strct.name, "ne");
669 writeln!(
670 out,
671 "bool {}({1} const &, {1} const &) noexcept;",
672 link_name, strct.name.cxx,
673 );
674 }
675 }
676
677 if derive::contains(&strct.derives, Trait::PartialOrd) {
678 out.pragma.dollar_in_identifier = true;
679 out.pragma.missing_declarations = true;
680 let link_name = mangle::operator(&strct.name, "lt");
681 writeln!(
682 out,
683 "bool {}({1} const &, {1} const &) noexcept;",
684 link_name, strct.name.cxx,
685 );
686
687 let link_name = mangle::operator(&strct.name, "le");
688 writeln!(
689 out,
690 "bool {}({1} const &, {1} const &) noexcept;",
691 link_name, strct.name.cxx,
692 );
693
694 if !derive::contains(&strct.derives, Trait::Ord) {
695 let link_name = mangle::operator(&strct.name, "gt");
696 writeln!(
697 out,
698 "bool {}({1} const &, {1} const &) noexcept;",
699 link_name, strct.name.cxx,
700 );
701
702 let link_name = mangle::operator(&strct.name, "ge");
703 writeln!(
704 out,
705 "bool {}({1} const &, {1} const &) noexcept;",
706 link_name, strct.name.cxx,
707 );
708 }
709 }
710
711 if derive::contains(&strct.derives, Trait::Hash) {
712 out.include.cstddef = true;
713 out.pragma.dollar_in_identifier = true;
714 out.pragma.missing_declarations = true;
715 let link_name = mangle::operator(&strct.name, "hash");
716 writeln!(
717 out,
718 "::std::size_t {}({} const &) noexcept;",
719 link_name, strct.name.cxx,
720 );
721 }
722
723 out.end_block(Block::ExternC);
724}
725
726fn write_struct_operators<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
727 if out.header {
728 return;
729 }
730
731 out.set_namespace(&strct.name.namespace);
732
733 if derive::contains(&strct.derives, Trait::PartialEq) {
734 out.pragma.dollar_in_identifier = true;
735
736 out.next_section();
737 writeln!(
738 out,
739 "bool {0}::operator==({0} const &rhs) const noexcept {{",
740 strct.name.cxx,
741 );
742 let link_name = mangle::operator(&strct.name, "eq");
743 writeln!(out, " return {}(*this, rhs);", link_name);
744 writeln!(out, "}}");
745
746 out.next_section();
747 writeln!(
748 out,
749 "bool {0}::operator!=({0} const &rhs) const noexcept {{",
750 strct.name.cxx,
751 );
752 if derive::contains(&strct.derives, Trait::Eq) {
753 writeln!(out, " return !(*this == rhs);");
754 } else {
755 let link_name = mangle::operator(&strct.name, "ne");
756 writeln!(out, " return {}(*this, rhs);", link_name);
757 }
758 writeln!(out, "}}");
759 }
760
761 if derive::contains(&strct.derives, Trait::PartialOrd) {
762 out.pragma.dollar_in_identifier = true;
763
764 out.next_section();
765 writeln!(
766 out,
767 "bool {0}::operator<({0} const &rhs) const noexcept {{",
768 strct.name.cxx,
769 );
770 let link_name = mangle::operator(&strct.name, "lt");
771 writeln!(out, " return {}(*this, rhs);", link_name);
772 writeln!(out, "}}");
773
774 out.next_section();
775 writeln!(
776 out,
777 "bool {0}::operator<=({0} const &rhs) const noexcept {{",
778 strct.name.cxx,
779 );
780 let link_name = mangle::operator(&strct.name, "le");
781 writeln!(out, " return {}(*this, rhs);", link_name);
782 writeln!(out, "}}");
783
784 out.next_section();
785 writeln!(
786 out,
787 "bool {0}::operator>({0} const &rhs) const noexcept {{",
788 strct.name.cxx,
789 );
790 if derive::contains(&strct.derives, Trait::Ord) {
791 writeln!(out, " return !(*this <= rhs);");
792 } else {
793 let link_name = mangle::operator(&strct.name, "gt");
794 writeln!(out, " return {}(*this, rhs);", link_name);
795 }
796 writeln!(out, "}}");
797
798 out.next_section();
799 writeln!(
800 out,
801 "bool {0}::operator>=({0} const &rhs) const noexcept {{",
802 strct.name.cxx,
803 );
804 if derive::contains(&strct.derives, Trait::Ord) {
805 writeln!(out, " return !(*this < rhs);");
806 } else {
807 let link_name = mangle::operator(&strct.name, "ge");
808 writeln!(out, " return {}(*this, rhs);", link_name);
809 }
810 writeln!(out, "}}");
811 }
812}
813
814fn write_opaque_type_layout_decls<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
815 out.set_namespace(&ety.name.namespace);
816 out.begin_block(Block::ExternC);
817 out.pragma.dollar_in_identifier = true;
818 out.pragma.missing_declarations = true;
819
820 let link_name = mangle::operator(&ety.name, "sizeof");
821 writeln!(out, "::std::size_t {}() noexcept;", link_name);
822
823 let link_name = mangle::operator(&ety.name, "alignof");
824 writeln!(out, "::std::size_t {}() noexcept;", link_name);
825
826 out.end_block(Block::ExternC);
827}
828
829fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
830 if out.header {
831 return;
832 }
833
834 out.set_namespace(&ety.name.namespace);
835 out.pragma.dollar_in_identifier = true;
836
837 out.next_section();
838 let link_name = mangle::operator(&ety.name, "sizeof");
839 writeln!(
840 out,
841 "::std::size_t {}::layout::size() noexcept {{",
842 ety.name.cxx,
843 );
844 writeln!(out, " return {}();", link_name);
845 writeln!(out, "}}");
846
847 out.next_section();
848 let link_name = mangle::operator(&ety.name, "alignof");
849 writeln!(
850 out,
851 "::std::size_t {}::layout::align() noexcept {{",
852 ety.name.cxx,
853 );
854 writeln!(out, " return {}();", link_name);
855 writeln!(out, "}}");
856}
857
858fn begin_function_definition(out: &mut OutFile) {
859 if let Some(annotation) = &out.opt.cxx_impl_annotations {
860 write!(out, "{} ", annotation);
861 }
862}
863
864fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
865 out.pragma.dollar_in_identifier = true;
866 out.pragma.missing_declarations = true;
867 out.next_section();
868 out.set_namespace(&efn.name.namespace);
869 out.begin_block(Block::ExternC);
870 begin_function_definition(out);
871 if efn.throws {
872 out.builtin.ptr_len = true;
873 write!(out, "::rust::repr::PtrLen ");
874 } else {
875 write_extern_return_type_space(out, efn, efn.lang);
876 }
877 let mangled = mangle::extern_fn(efn, out.types);
878 write!(out, "{}(", mangled);
879 if let FnKind::Method(receiver) = &efn.kind {
880 write!(
881 out,
882 "{}",
883 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
884 );
885 if !receiver.mutable {
886 write!(out, " const");
887 }
888 write!(out, " &self");
889 }
890 for (i, arg) in efn.args.iter().enumerate() {
891 if i > 0 || matches!(efn.kind, FnKind::Method(_)) {
892 write!(out, ", ");
893 }
894 if arg.ty == RustString {
895 write_type_space(out, &arg.ty);
896 write!(out, "const *{}", arg.name.cxx);
897 } else if let Type::RustVec(_) = arg.ty {
898 write_type_space(out, &arg.ty);
899 write!(out, "const *{}", arg.name.cxx);
900 } else {
901 write_extern_arg(out, arg);
902 }
903 }
904 let indirect_return = indirect_return(efn, out.types, efn.lang);
905 if indirect_return {
906 if !efn.args.is_empty() || matches!(efn.kind, FnKind::Method(_)) {
907 write!(out, ", ");
908 }
909 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
910 write!(out, "*return$");
911 }
912 write!(out, ")");
913 match efn.lang {
914 Lang::Cxx => write!(out, " noexcept"),
915 Lang::CxxUnwind => {}
916 Lang::Rust => unreachable!(),
917 }
918 writeln!(out, " {{");
919 write!(out, " ");
920 write_return_type(out, &efn.ret);
921 match efn.receiver() {
922 None => write!(out, "(*{}$)(", efn.name.rust),
923 Some(receiver) => write!(
924 out,
925 "({}::*{}$)(",
926 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
927 efn.name.rust,
928 ),
929 }
930 for (i, arg) in efn.args.iter().enumerate() {
931 if i > 0 {
932 write!(out, ", ");
933 }
934 write_type(out, &arg.ty);
935 }
936 write!(out, ")");
937 if let Some(receiver) = efn.receiver() {
938 if !receiver.mutable {
939 write!(out, " const");
940 }
941 }
942 write!(out, " = ");
943 match efn.self_type() {
944 None => write!(out, "{}", efn.name.to_fully_qualified()),
945 Some(self_type) => write!(
946 out,
947 "&{}::{}",
948 out.types.resolve(self_type).name.to_fully_qualified(),
949 efn.name.cxx,
950 ),
951 }
952 writeln!(out, ";");
953 write!(out, " ");
954 if efn.throws {
955 out.builtin.ptr_len = true;
956 out.builtin.trycatch = true;
957 writeln!(out, "::rust::repr::PtrLen throw$;");
958 writeln!(out, " ::rust::behavior::trycatch(");
959 writeln!(out, " [&] {{");
960 write!(out, " ");
961 }
962 if indirect_return {
963 out.include.new = true;
964 write!(out, "new (return$) ");
965 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
966 write!(out, "(");
967 } else if efn.ret.is_some() {
968 write!(out, "return ");
969 }
970 match &efn.ret {
971 Some(Type::Ref(_)) => write!(out, "&"),
972 Some(Type::Str(_)) if !indirect_return => {
973 out.builtin.rust_str_repr = true;
974 write!(out, "::rust::impl<::rust::Str>::repr(");
975 }
976 Some(ty @ Type::SliceRef(_)) if !indirect_return => {
977 out.builtin.rust_slice_repr = true;
978 write!(out, "::rust::impl<");
979 write_type(out, ty);
980 write!(out, ">::repr(");
981 }
982 _ => {}
983 }
984 match efn.receiver() {
985 None => write!(out, "{}$(", efn.name.rust),
986 Some(_) => write!(out, "(self.*{}$)(", efn.name.rust),
987 }
988 for (i, arg) in efn.args.iter().enumerate() {
989 if i > 0 {
990 write!(out, ", ");
991 }
992 if let Type::RustBox(_) = &arg.ty {
993 write_type(out, &arg.ty);
994 write!(out, "::from_raw({})", arg.name.cxx);
995 } else if let Type::UniquePtr(_) = &arg.ty {
996 write_type(out, &arg.ty);
997 write!(out, "({})", arg.name.cxx);
998 } else if arg.ty == RustString {
999 out.builtin.unsafe_bitcopy = true;
1000 write!(
1001 out,
1002 "::rust::String(::rust::unsafe_bitcopy, *{})",
1003 arg.name.cxx,
1004 );
1005 } else if let Type::RustVec(_) = arg.ty {
1006 out.builtin.unsafe_bitcopy = true;
1007 write_type(out, &arg.ty);
1008 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.name.cxx);
1009 } else if out.types.needs_indirect_abi(&arg.ty) {
1010 out.include.utility = true;
1011 write!(out, "::std::move(*{})", arg.name.cxx);
1012 } else {
1013 write!(out, "{}", arg.name.cxx);
1014 }
1015 }
1016 write!(out, ")");
1017 match &efn.ret {
1018 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
1019 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
1020 Some(Type::Str(_) | Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
1021 _ => {}
1022 }
1023 if indirect_return {
1024 write!(out, ")");
1025 }
1026 writeln!(out, ";");
1027 if efn.throws {
1028 writeln!(out, " throw$.ptr = nullptr;");
1029 writeln!(out, " }},");
1030 writeln!(out, " ::rust::detail::Fail(throw$));");
1031 writeln!(out, " return throw$;");
1032 }
1033 writeln!(out, "}}");
1034 for arg in &efn.args {
1035 if let Type::Fn(f) = &arg.ty {
1036 let var = &arg.name;
1037 write_function_pointer_trampoline(out, efn, var, f);
1038 }
1039 }
1040 out.end_block(Block::ExternC);
1041}
1042
1043fn write_function_pointer_trampoline(out: &mut OutFile, efn: &ExternFn, var: &Pair, f: &Signature) {
1044 out.pragma.return_type_c_linkage = true;
1045
1046 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
1047 let indirect_call = true;
1048 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
1049
1050 out.next_section();
1051 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
1052 let doc = Doc::new();
1053 let main = false;
1054 write_rust_function_shim_impl(
1055 out,
1056 &c_trampoline,
1057 f,
1058 &doc,
1059 &r_trampoline,
1060 indirect_call,
1061 main,
1062 );
1063}
1064
1065fn write_rust_function_decl<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
1066 out.set_namespace(&efn.name.namespace);
1067 out.begin_block(Block::ExternC);
1068 let link_name = mangle::extern_fn(efn, out.types);
1069 let indirect_call = false;
1070 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
1071 out.end_block(Block::ExternC);
1072}
1073
1074fn write_rust_function_decl_impl(
1075 out: &mut OutFile,
1076 link_name: &Symbol,
1077 sig: &Signature,
1078 indirect_call: bool,
1079) {
1080 out.next_section();
1081 out.pragma.dollar_in_identifier = true;
1082 if sig.throws {
1083 out.builtin.ptr_len = true;
1084 write!(out, "::rust::repr::PtrLen ");
1085 } else {
1086 write_extern_return_type_space(out, sig, Lang::Rust);
1087 }
1088 write!(out, "{}(", link_name);
1089 let mut needs_comma = false;
1090 if let FnKind::Method(receiver) = &sig.kind {
1091 write!(
1092 out,
1093 "{}",
1094 out.types.resolve(&receiver.ty).name.to_fully_qualified(),
1095 );
1096 if !receiver.mutable {
1097 write!(out, " const");
1098 }
1099 write!(out, " &self");
1100 needs_comma = true;
1101 }
1102 for arg in &sig.args {
1103 if needs_comma {
1104 write!(out, ", ");
1105 }
1106 write_extern_arg(out, arg);
1107 needs_comma = true;
1108 }
1109 if indirect_return(sig, out.types, Lang::Rust) {
1110 if needs_comma {
1111 write!(out, ", ");
1112 }
1113 match sig.ret.as_ref().unwrap() {
1114 Type::Ref(ret) => {
1115 write_type_space(out, &ret.inner);
1116 if !ret.mutable {
1117 write!(out, "const ");
1118 }
1119 write!(out, "*");
1120 }
1121 ret => write_type_space(out, ret),
1122 }
1123 write!(out, "*return$");
1124 needs_comma = true;
1125 }
1126 if indirect_call {
1127 if needs_comma {
1128 write!(out, ", ");
1129 }
1130 write!(out, "void *");
1131 }
1132 writeln!(out, ") noexcept;");
1133}
1134
1135fn write_rust_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
1136 out.set_namespace(&efn.name.namespace);
1137 let local_name = match efn.self_type() {
1138 None => efn.name.cxx.to_string(),
1139 Some(self_type) => format!(
1140 "{}::{}",
1141 out.types.resolve(self_type).name.cxx,
1142 efn.name.cxx,
1143 ),
1144 };
1145 let doc = &efn.doc;
1146 let invoke = mangle::extern_fn(efn, out.types);
1147 let indirect_call = false;
1148 let main = efn.name.cxx == *"main"
1149 && efn.name.namespace == Namespace::ROOT
1150 && efn.sig.asyncness.is_none()
1151 && matches!(efn.kind, FnKind::Free)
1152 && efn.sig.args.is_empty()
1153 && efn.sig.ret.is_none()
1154 && !efn.sig.throws;
1155 write_rust_function_shim_impl(out, &local_name, efn, doc, &invoke, indirect_call, main);
1156}
1157
1158fn write_rust_function_shim_decl(
1159 out: &mut OutFile,
1160 local_name: &str,
1161 sig: &Signature,
1162 in_class: bool,
1163 indirect_call: bool,
1164 main: bool,
1165) {
1166 begin_function_definition(out);
1167 if matches!(sig.kind, FnKind::Assoc(_)) && in_class {
1168 write!(out, "static ");
1169 }
1170 if main {
1171 write!(out, "int ");
1172 } else {
1173 write_return_type(out, &sig.ret);
1174 }
1175 write!(out, "{}(", local_name);
1176 for (i, arg) in sig.args.iter().enumerate() {
1177 if i > 0 {
1178 write!(out, ", ");
1179 }
1180 write_type_space(out, &arg.ty);
1181 write!(out, "{}", arg.name.cxx);
1182 }
1183 if indirect_call {
1184 if !sig.args.is_empty() {
1185 write!(out, ", ");
1186 }
1187 write!(out, "void *extern$");
1188 }
1189 write!(out, ")");
1190 if let FnKind::Method(receiver) = &sig.kind {
1191 if !receiver.mutable {
1192 write!(out, " const");
1193 }
1194 }
1195 if !sig.throws {
1196 write!(out, " noexcept");
1197 }
1198}
1199
1200fn write_rust_function_shim_impl(
1201 out: &mut OutFile,
1202 local_name: &str,
1203 sig: &Signature,
1204 doc: &Doc,
1205 invoke: &Symbol,
1206 indirect_call: bool,
1207 main: bool,
1208) {
1209 if match sig.kind {
1210 FnKind::Free => false,
1211 FnKind::Method(_) | FnKind::Assoc(_) => out.header,
1212 } {
1213 return;
1215 }
1216 out.pragma.dollar_in_identifier = true;
1217 if matches!(sig.kind, FnKind::Free) {
1218 write_doc(out, "", doc);
1220 }
1221 let in_class = false;
1222 write_rust_function_shim_decl(out, local_name, sig, in_class, indirect_call, main);
1223 if out.header {
1224 writeln!(out, ";");
1225 return;
1226 }
1227 writeln!(out, " {{");
1228 for arg in &sig.args {
1229 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
1230 out.include.utility = true;
1231 out.builtin.manually_drop = true;
1232 write!(out, " ::rust::ManuallyDrop<");
1233 write_type(out, &arg.ty);
1234 writeln!(out, "> {}$(::std::move({0}));", arg.name.cxx);
1235 }
1236 }
1237 write!(out, " ");
1238 let indirect_return = indirect_return(sig, out.types, Lang::Rust);
1239 if indirect_return {
1240 out.builtin.maybe_uninit = true;
1241 write!(out, "::rust::MaybeUninit<");
1242 match sig.ret.as_ref().unwrap() {
1243 Type::Ref(ret) => {
1244 write_type_space(out, &ret.inner);
1245 if !ret.mutable {
1246 write!(out, "const ");
1247 }
1248 write!(out, "*");
1249 }
1250 ret => write_type(out, ret),
1251 }
1252 writeln!(out, "> return$;");
1253 write!(out, " ");
1254 } else if let Some(ret) = &sig.ret {
1255 write!(out, "return ");
1256 match ret {
1257 Type::RustBox(_) => {
1258 write_type(out, ret);
1259 write!(out, "::from_raw(");
1260 }
1261 Type::UniquePtr(_) => {
1262 write_type(out, ret);
1263 write!(out, "(");
1264 }
1265 Type::Ref(_) => write!(out, "*"),
1266 Type::Str(_) => {
1267 out.builtin.rust_str_new_unchecked = true;
1268 write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
1269 }
1270 Type::SliceRef(_) => {
1271 out.builtin.rust_slice_new = true;
1272 write!(out, "::rust::impl<");
1273 write_type(out, ret);
1274 write!(out, ">::slice(");
1275 }
1276 _ => {}
1277 }
1278 }
1279 if sig.throws {
1280 out.builtin.ptr_len = true;
1281 write!(out, "::rust::repr::PtrLen error$ = ");
1282 }
1283 write!(out, "{}(", invoke);
1284 let mut needs_comma = false;
1285 if matches!(sig.kind, FnKind::Method(_)) {
1286 write!(out, "*this");
1287 needs_comma = true;
1288 }
1289 for arg in &sig.args {
1290 if needs_comma {
1291 write!(out, ", ");
1292 }
1293 if out.types.needs_indirect_abi(&arg.ty) {
1294 write!(out, "&");
1295 }
1296 write!(out, "{}", arg.name.cxx);
1297 match &arg.ty {
1298 Type::RustBox(_) => write!(out, ".into_raw()"),
1299 Type::UniquePtr(_) => write!(out, ".release()"),
1300 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
1301 _ => {}
1302 }
1303 needs_comma = true;
1304 }
1305 if indirect_return {
1306 if needs_comma {
1307 write!(out, ", ");
1308 }
1309 write!(out, "&return$.value");
1310 needs_comma = true;
1311 }
1312 if indirect_call {
1313 if needs_comma {
1314 write!(out, ", ");
1315 }
1316 write!(out, "extern$");
1317 }
1318 write!(out, ")");
1319 if !indirect_return {
1320 if let Some(Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_)) =
1321 &sig.ret
1322 {
1323 write!(out, ")");
1324 }
1325 }
1326 writeln!(out, ";");
1327 if sig.throws {
1328 out.builtin.rust_error = true;
1329 writeln!(out, " if (error$.ptr) {{");
1330 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
1331 writeln!(out, " }}");
1332 }
1333 if indirect_return {
1334 write!(out, " return ");
1335 match sig.ret.as_ref().unwrap() {
1336 Type::Ref(_) => write!(out, "*return$.value"),
1337 _ => {
1338 out.include.utility = true;
1339 write!(out, "::std::move(return$.value)");
1340 }
1341 }
1342 writeln!(out, ";");
1343 }
1344 writeln!(out, "}}");
1345}
1346
1347fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
1348 match ty {
1349 None => write!(out, "void "),
1350 Some(ty) => write_type_space(out, ty),
1351 }
1352}
1353
1354fn indirect_return(sig: &Signature, types: &Types, lang: Lang) -> bool {
1355 sig.ret.as_ref().is_some_and(|ret| {
1356 sig.throws
1357 || types.needs_indirect_abi(ret)
1358 || match lang {
1359 Lang::Cxx | Lang::CxxUnwind => types.contains_elided_lifetime(ret),
1360 Lang::Rust => false,
1361 }
1362 })
1363}
1364
1365fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
1366 match ty {
1367 Type::RustBox(ty) | Type::UniquePtr(ty) => {
1368 write_type_space(out, &ty.inner);
1369 write!(out, "*");
1370 }
1371 Type::Ref(ty) => {
1372 write_type_space(out, &ty.inner);
1373 if !ty.mutable {
1374 write!(out, "const ");
1375 }
1376 write!(out, "*");
1377 }
1378 _ => write_type(out, ty),
1379 }
1380}
1381
1382fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
1383 write_indirect_return_type(out, ty);
1384 match ty {
1385 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
1386 Type::Str(_) | Type::SliceRef(_) => write!(out, " "),
1387 _ => write_space_after_type(out, ty),
1388 }
1389}
1390
1391fn write_extern_return_type_space(out: &mut OutFile, sig: &Signature, lang: Lang) {
1392 match &sig.ret {
1393 Some(_) if indirect_return(sig, out.types, lang) => write!(out, "void "),
1394 Some(Type::RustBox(ty) | Type::UniquePtr(ty)) => {
1395 write_type_space(out, &ty.inner);
1396 write!(out, "*");
1397 }
1398 Some(Type::Ref(ty)) => {
1399 write_type_space(out, &ty.inner);
1400 if !ty.mutable {
1401 write!(out, "const ");
1402 }
1403 write!(out, "*");
1404 }
1405 Some(Type::Str(_) | Type::SliceRef(_)) => {
1406 out.builtin.repr_fat = true;
1407 write!(out, "::rust::repr::Fat ");
1408 }
1409 ty => write_return_type(out, ty),
1410 }
1411}
1412
1413fn write_extern_arg(out: &mut OutFile, arg: &Var) {
1414 match &arg.ty {
1415 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
1416 write_type_space(out, &ty.inner);
1417 write!(out, "*");
1418 }
1419 _ => write_type_space(out, &arg.ty),
1420 }
1421 if out.types.needs_indirect_abi(&arg.ty) {
1422 write!(out, "*");
1423 }
1424 write!(out, "{}", arg.name.cxx);
1425}
1426
1427fn write_type(out: &mut OutFile, ty: &Type) {
1428 match ty {
1429 Type::Ident(ident) => match Atom::from(&ident.rust) {
1430 Some(atom) => write_atom(out, atom),
1431 None => write!(
1432 out,
1433 "{}",
1434 out.types.resolve(ident).name.to_fully_qualified(),
1435 ),
1436 },
1437 Type::RustBox(ty) => {
1438 write!(out, "::rust::Box<");
1439 write_type(out, &ty.inner);
1440 write!(out, ">");
1441 }
1442 Type::RustVec(ty) => {
1443 write!(out, "::rust::Vec<");
1444 write_type(out, &ty.inner);
1445 write!(out, ">");
1446 }
1447 Type::UniquePtr(ptr) => {
1448 write!(out, "::std::unique_ptr<");
1449 write_type(out, &ptr.inner);
1450 write!(out, ">");
1451 }
1452 Type::SharedPtr(ptr) => {
1453 write!(out, "::std::shared_ptr<");
1454 write_type(out, &ptr.inner);
1455 write!(out, ">");
1456 }
1457 Type::WeakPtr(ptr) => {
1458 write!(out, "::std::weak_ptr<");
1459 write_type(out, &ptr.inner);
1460 write!(out, ">");
1461 }
1462 Type::CxxVector(ty) => {
1463 write!(out, "::std::vector<");
1464 write_type(out, &ty.inner);
1465 write!(out, ">");
1466 }
1467 Type::Ref(r) => {
1468 write_type_space(out, &r.inner);
1469 if !r.mutable {
1470 write!(out, "const ");
1471 }
1472 write!(out, "&");
1473 }
1474 Type::Ptr(p) => {
1475 write_type_space(out, &p.inner);
1476 if !p.mutable {
1477 write!(out, "const ");
1478 }
1479 write!(out, "*");
1480 }
1481 Type::Str(_) => {
1482 write!(out, "::rust::Str");
1483 }
1484 Type::SliceRef(slice) => {
1485 write!(out, "::rust::Slice<");
1486 write_type_space(out, &slice.inner);
1487 if slice.mutability.is_none() {
1488 write!(out, "const");
1489 }
1490 write!(out, ">");
1491 }
1492 Type::Fn(f) => {
1493 write!(out, "::rust::Fn<");
1494 match &f.ret {
1495 Some(ret) => write_type(out, ret),
1496 None => write!(out, "void"),
1497 }
1498 write!(out, "(");
1499 for (i, arg) in f.args.iter().enumerate() {
1500 if i > 0 {
1501 write!(out, ", ");
1502 }
1503 write_type(out, &arg.ty);
1504 }
1505 write!(out, ")>");
1506 }
1507 Type::Array(a) => {
1508 write!(out, "::std::array<");
1509 write_type(out, &a.inner);
1510 write!(out, ", {}>", &a.len);
1511 }
1512 Type::Void(_) => unreachable!(),
1513 }
1514}
1515
1516fn write_atom(out: &mut OutFile, atom: Atom) {
1517 match atom {
1518 Bool => write!(out, "bool"),
1519 Char => write!(out, "char"),
1520 U8 => write!(out, "::std::uint8_t"),
1521 U16 => write!(out, "::std::uint16_t"),
1522 U32 => write!(out, "::std::uint32_t"),
1523 U64 => write!(out, "::std::uint64_t"),
1524 Usize => write!(out, "::std::size_t"),
1525 I8 => write!(out, "::std::int8_t"),
1526 I16 => write!(out, "::std::int16_t"),
1527 I32 => write!(out, "::std::int32_t"),
1528 I64 => write!(out, "::std::int64_t"),
1529 Isize => write!(out, "::rust::isize"),
1530 F32 => write!(out, "float"),
1531 F64 => write!(out, "double"),
1532 CxxString => write!(out, "::std::string"),
1533 RustString => write!(out, "::rust::String"),
1534 }
1535}
1536
1537fn write_type_space(out: &mut OutFile, ty: &Type) {
1538 write_type(out, ty);
1539 write_space_after_type(out, ty);
1540}
1541
1542fn write_space_after_type(out: &mut OutFile, ty: &Type) {
1543 match ty {
1544 Type::Ident(_)
1545 | Type::RustBox(_)
1546 | Type::UniquePtr(_)
1547 | Type::SharedPtr(_)
1548 | Type::WeakPtr(_)
1549 | Type::Str(_)
1550 | Type::CxxVector(_)
1551 | Type::RustVec(_)
1552 | Type::SliceRef(_)
1553 | Type::Fn(_)
1554 | Type::Array(_) => write!(out, " "),
1555 Type::Ref(_) | Type::Ptr(_) => {}
1556 Type::Void(_) => unreachable!(),
1557 }
1558}
1559
1560#[derive(Copy, Clone)]
1561enum UniquePtr<'a> {
1562 Ident(&'a Ident),
1563 CxxVector(&'a Ident),
1564}
1565
1566trait ToTypename {
1567 fn to_typename(&self, types: &Types) -> String;
1568}
1569
1570impl ToTypename for Ident {
1571 fn to_typename(&self, types: &Types) -> String {
1572 types.resolve(self).name.to_fully_qualified()
1573 }
1574}
1575
1576impl<'a> ToTypename for UniquePtr<'a> {
1577 fn to_typename(&self, types: &Types) -> String {
1578 match self {
1579 UniquePtr::Ident(ident) => ident.to_typename(types),
1580 UniquePtr::CxxVector(element) => {
1581 format!("::std::vector<{}>", element.to_typename(types))
1582 }
1583 }
1584 }
1585}
1586
1587trait ToMangled {
1588 fn to_mangled(&self, types: &Types) -> Symbol;
1589}
1590
1591impl ToMangled for Ident {
1592 fn to_mangled(&self, types: &Types) -> Symbol {
1593 types.resolve(self).name.to_symbol()
1594 }
1595}
1596
1597impl<'a> ToMangled for UniquePtr<'a> {
1598 fn to_mangled(&self, types: &Types) -> Symbol {
1599 match self {
1600 UniquePtr::Ident(ident) => ident.to_mangled(types),
1601 UniquePtr::CxxVector(element) => {
1602 symbol::join(&[&"std", &"vector", &element.to_mangled(types)])
1603 }
1604 }
1605 }
1606}
1607
1608fn write_generic_instantiations(out: &mut OutFile) {
1609 if out.header {
1610 return;
1611 }
1612
1613 out.next_section();
1614 out.set_namespace(Default::default());
1615 out.begin_block(Block::ExternC);
1616 for impl_key in out.types.impls.keys() {
1617 out.next_section();
1618 match impl_key {
1619 ImplKey::RustBox(ident) => write_rust_box_extern(out, ident),
1620 ImplKey::RustVec(ident) => write_rust_vec_extern(out, ident),
1621 ImplKey::UniquePtr(ident) => write_unique_ptr(out, ident),
1622 ImplKey::SharedPtr(ident) => write_shared_ptr(out, ident),
1623 ImplKey::WeakPtr(ident) => write_weak_ptr(out, ident),
1624 ImplKey::CxxVector(ident) => write_cxx_vector(out, ident),
1625 }
1626 }
1627 out.end_block(Block::ExternC);
1628
1629 out.begin_block(Block::Namespace("rust"));
1630 out.begin_block(Block::InlineNamespace("cxxbridge1"));
1631 for impl_key in out.types.impls.keys() {
1632 match impl_key {
1633 ImplKey::RustBox(ident) => write_rust_box_impl(out, ident),
1634 ImplKey::RustVec(ident) => write_rust_vec_impl(out, ident),
1635 _ => {}
1636 }
1637 }
1638 out.end_block(Block::InlineNamespace("cxxbridge1"));
1639 out.end_block(Block::Namespace("rust"));
1640}
1641
1642fn write_rust_box_extern(out: &mut OutFile, key: &NamedImplKey) {
1643 let resolve = out.types.resolve(key);
1644 let inner = resolve.name.to_fully_qualified();
1645 let instance = resolve.name.to_symbol();
1646
1647 out.pragma.dollar_in_identifier = true;
1648
1649 writeln!(
1650 out,
1651 "{} *cxxbridge1$box${}$alloc() noexcept;",
1652 inner, instance,
1653 );
1654 writeln!(
1655 out,
1656 "void cxxbridge1$box${}$dealloc({} *) noexcept;",
1657 instance, inner,
1658 );
1659 writeln!(
1660 out,
1661 "void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
1662 instance, inner,
1663 );
1664}
1665
1666fn write_rust_vec_extern(out: &mut OutFile, key: &NamedImplKey) {
1667 let element = key.rust;
1668 let inner = element.to_typename(out.types);
1669 let instance = element.to_mangled(out.types);
1670
1671 out.include.cstddef = true;
1672 out.pragma.dollar_in_identifier = true;
1673
1674 writeln!(
1675 out,
1676 "void cxxbridge1$rust_vec${}$new(::rust::Vec<{}> const *ptr) noexcept;",
1677 instance, inner,
1678 );
1679 writeln!(
1680 out,
1681 "void cxxbridge1$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
1682 instance, inner,
1683 );
1684 writeln!(
1685 out,
1686 "::std::size_t cxxbridge1$rust_vec${}$len(::rust::Vec<{}> const *ptr) noexcept;",
1687 instance, inner,
1688 );
1689 writeln!(
1690 out,
1691 "::std::size_t cxxbridge1$rust_vec${}$capacity(::rust::Vec<{}> const *ptr) noexcept;",
1692 instance, inner,
1693 );
1694 writeln!(
1695 out,
1696 "{} const *cxxbridge1$rust_vec${}$data(::rust::Vec<{0}> const *ptr) noexcept;",
1697 inner, instance,
1698 );
1699 writeln!(
1700 out,
1701 "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t new_cap) noexcept;",
1702 instance, inner,
1703 );
1704 writeln!(
1705 out,
1706 "void cxxbridge1$rust_vec${}$set_len(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
1707 instance, inner,
1708 );
1709 writeln!(
1710 out,
1711 "void cxxbridge1$rust_vec${}$truncate(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
1712 instance, inner,
1713 );
1714}
1715
1716fn write_rust_box_impl(out: &mut OutFile, key: &NamedImplKey) {
1717 let resolve = out.types.resolve(key);
1718 let inner = resolve.name.to_fully_qualified();
1719 let instance = resolve.name.to_symbol();
1720
1721 out.pragma.dollar_in_identifier = true;
1722
1723 writeln!(out, "template <>");
1724 begin_function_definition(out);
1725 writeln!(
1726 out,
1727 "{} *Box<{}>::allocation::alloc() noexcept {{",
1728 inner, inner,
1729 );
1730 writeln!(out, " return cxxbridge1$box${}$alloc();", instance);
1731 writeln!(out, "}}");
1732
1733 writeln!(out, "template <>");
1734 begin_function_definition(out);
1735 writeln!(
1736 out,
1737 "void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
1738 inner, inner,
1739 );
1740 writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
1741 writeln!(out, "}}");
1742
1743 writeln!(out, "template <>");
1744 begin_function_definition(out);
1745 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
1746 writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
1747 writeln!(out, "}}");
1748}
1749
1750fn write_rust_vec_impl(out: &mut OutFile, key: &NamedImplKey) {
1751 let element = key.rust;
1752 let inner = element.to_typename(out.types);
1753 let instance = element.to_mangled(out.types);
1754
1755 out.include.cstddef = true;
1756 out.pragma.dollar_in_identifier = true;
1757
1758 writeln!(out, "template <>");
1759 begin_function_definition(out);
1760 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
1761 writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
1762 writeln!(out, "}}");
1763
1764 writeln!(out, "template <>");
1765 begin_function_definition(out);
1766 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1767 writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
1768 writeln!(out, "}}");
1769
1770 writeln!(out, "template <>");
1771 begin_function_definition(out);
1772 writeln!(
1773 out,
1774 "::std::size_t Vec<{}>::size() const noexcept {{",
1775 inner,
1776 );
1777 writeln!(out, " return cxxbridge1$rust_vec${}$len(this);", instance);
1778 writeln!(out, "}}");
1779
1780 writeln!(out, "template <>");
1781 begin_function_definition(out);
1782 writeln!(
1783 out,
1784 "::std::size_t Vec<{}>::capacity() const noexcept {{",
1785 inner,
1786 );
1787 writeln!(
1788 out,
1789 " return cxxbridge1$rust_vec${}$capacity(this);",
1790 instance,
1791 );
1792 writeln!(out, "}}");
1793
1794 writeln!(out, "template <>");
1795 begin_function_definition(out);
1796 writeln!(out, "{} const *Vec<{0}>::data() const noexcept {{", inner);
1797 writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
1798 writeln!(out, "}}");
1799
1800 writeln!(out, "template <>");
1801 begin_function_definition(out);
1802 writeln!(
1803 out,
1804 "void Vec<{}>::reserve_total(::std::size_t new_cap) noexcept {{",
1805 inner,
1806 );
1807 writeln!(
1808 out,
1809 " return cxxbridge1$rust_vec${}$reserve_total(this, new_cap);",
1810 instance,
1811 );
1812 writeln!(out, "}}");
1813
1814 writeln!(out, "template <>");
1815 begin_function_definition(out);
1816 writeln!(
1817 out,
1818 "void Vec<{}>::set_len(::std::size_t len) noexcept {{",
1819 inner,
1820 );
1821 writeln!(
1822 out,
1823 " return cxxbridge1$rust_vec${}$set_len(this, len);",
1824 instance,
1825 );
1826 writeln!(out, "}}");
1827
1828 writeln!(out, "template <>");
1829 begin_function_definition(out);
1830 writeln!(out, "void Vec<{}>::truncate(::std::size_t len) {{", inner,);
1831 writeln!(
1832 out,
1833 " return cxxbridge1$rust_vec${}$truncate(this, len);",
1834 instance,
1835 );
1836 writeln!(out, "}}");
1837}
1838
1839fn write_unique_ptr(out: &mut OutFile, key: &NamedImplKey) {
1840 let ty = UniquePtr::Ident(key.rust);
1841 write_unique_ptr_common(out, ty);
1842}
1843
1844fn write_unique_ptr_common(out: &mut OutFile, ty: UniquePtr) {
1846 out.include.new = true;
1847 out.include.utility = true;
1848 out.pragma.dollar_in_identifier = true;
1849 out.pragma.missing_declarations = true;
1850
1851 let inner = ty.to_typename(out.types);
1852 let instance = ty.to_mangled(out.types);
1853
1854 let can_construct_from_value = match ty {
1855 UniquePtr::Ident(ident) => out.types.is_maybe_trivial(ident),
1860 UniquePtr::CxxVector(_) => false,
1861 };
1862
1863 out.builtin.is_complete = true;
1864 writeln!(
1865 out,
1866 "static_assert(::rust::detail::is_complete<::std::remove_extent<{}>::type>::value, \"definition of `{}` is required\");",
1867 inner, inner,
1868 );
1869 writeln!(
1870 out,
1871 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
1872 inner,
1873 );
1874 writeln!(
1875 out,
1876 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
1877 inner,
1878 );
1879
1880 begin_function_definition(out);
1881 writeln!(
1882 out,
1883 "void cxxbridge1$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
1884 instance, inner,
1885 );
1886 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>();", inner);
1887 writeln!(out, "}}");
1888
1889 if can_construct_from_value {
1890 out.builtin.maybe_uninit = true;
1891 begin_function_definition(out);
1892 writeln!(
1893 out,
1894 "{} *cxxbridge1$unique_ptr${}$uninit(::std::unique_ptr<{}> *ptr) noexcept {{",
1895 inner, instance, inner,
1896 );
1897 writeln!(
1898 out,
1899 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1900 inner, inner, inner,
1901 );
1902 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(uninit);", inner);
1903 writeln!(out, " return uninit;");
1904 writeln!(out, "}}");
1905 }
1906
1907 begin_function_definition(out);
1908 writeln!(
1909 out,
1910 "void cxxbridge1$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, ::std::unique_ptr<{}>::pointer raw) noexcept {{",
1911 instance, inner, inner,
1912 );
1913 writeln!(out, " ::new (ptr) ::std::unique_ptr<{}>(raw);", inner);
1914 writeln!(out, "}}");
1915
1916 begin_function_definition(out);
1917 writeln!(
1918 out,
1919 "::std::unique_ptr<{}>::element_type const *cxxbridge1$unique_ptr${}$get(::std::unique_ptr<{}> const &ptr) noexcept {{",
1920 inner, instance, inner,
1921 );
1922 writeln!(out, " return ptr.get();");
1923 writeln!(out, "}}");
1924
1925 begin_function_definition(out);
1926 writeln!(
1927 out,
1928 "::std::unique_ptr<{}>::pointer cxxbridge1$unique_ptr${}$release(::std::unique_ptr<{}> &ptr) noexcept {{",
1929 inner, instance, inner,
1930 );
1931 writeln!(out, " return ptr.release();");
1932 writeln!(out, "}}");
1933
1934 begin_function_definition(out);
1935 writeln!(
1936 out,
1937 "void cxxbridge1$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
1938 instance, inner,
1939 );
1940 out.builtin.deleter_if = true;
1941 writeln!(
1942 out,
1943 " ::rust::deleter_if<::rust::detail::is_complete<{}>::value>{{}}(ptr);",
1944 inner,
1945 );
1946 writeln!(out, "}}");
1947}
1948
1949fn write_shared_ptr(out: &mut OutFile, key: &NamedImplKey) {
1950 let ident = key.rust;
1951 let resolve = out.types.resolve(ident);
1952 let inner = resolve.name.to_fully_qualified();
1953 let instance = resolve.name.to_symbol();
1954
1955 out.include.new = true;
1956 out.include.utility = true;
1957 out.pragma.dollar_in_identifier = true;
1958 out.pragma.missing_declarations = true;
1959
1960 let can_construct_from_value = out.types.is_maybe_trivial(ident);
1965
1966 writeln!(
1967 out,
1968 "static_assert(sizeof(::std::shared_ptr<{}>) == 2 * sizeof(void *), \"\");",
1969 inner,
1970 );
1971 writeln!(
1972 out,
1973 "static_assert(alignof(::std::shared_ptr<{}>) == alignof(void *), \"\");",
1974 inner,
1975 );
1976
1977 begin_function_definition(out);
1978 writeln!(
1979 out,
1980 "void cxxbridge1$shared_ptr${}$null(::std::shared_ptr<{}> *ptr) noexcept {{",
1981 instance, inner,
1982 );
1983 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>();", inner);
1984 writeln!(out, "}}");
1985
1986 if can_construct_from_value {
1987 out.builtin.maybe_uninit = true;
1988 begin_function_definition(out);
1989 writeln!(
1990 out,
1991 "{} *cxxbridge1$shared_ptr${}$uninit(::std::shared_ptr<{}> *ptr) noexcept {{",
1992 inner, instance, inner,
1993 );
1994 writeln!(
1995 out,
1996 " {} *uninit = reinterpret_cast<{} *>(new ::rust::MaybeUninit<{}>);",
1997 inner, inner, inner,
1998 );
1999 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(uninit);", inner);
2000 writeln!(out, " return uninit;");
2001 writeln!(out, "}}");
2002 }
2003
2004 out.builtin.shared_ptr = true;
2005 begin_function_definition(out);
2006 writeln!(
2007 out,
2008 "bool cxxbridge1$shared_ptr${}$raw(::std::shared_ptr<{}> *ptr, ::std::shared_ptr<{}>::element_type *raw) noexcept {{",
2009 instance, inner, inner,
2010 );
2011 writeln!(
2012 out,
2013 " ::new (ptr) ::rust::shared_ptr_if_destructible<{}>(raw);",
2014 inner,
2015 );
2016 writeln!(out, " return ::rust::is_destructible<{}>::value;", inner);
2017 writeln!(out, "}}");
2018
2019 begin_function_definition(out);
2020 writeln!(
2021 out,
2022 "void cxxbridge1$shared_ptr${}$clone(::std::shared_ptr<{}> const &self, ::std::shared_ptr<{}> *ptr) noexcept {{",
2023 instance, inner, inner,
2024 );
2025 writeln!(out, " ::new (ptr) ::std::shared_ptr<{}>(self);", inner);
2026 writeln!(out, "}}");
2027
2028 begin_function_definition(out);
2029 writeln!(
2030 out,
2031 "::std::shared_ptr<{}>::element_type const *cxxbridge1$shared_ptr${}$get(::std::shared_ptr<{}> const &self) noexcept {{",
2032 inner, instance, inner,
2033 );
2034 writeln!(out, " return self.get();");
2035 writeln!(out, "}}");
2036
2037 begin_function_definition(out);
2038 writeln!(
2039 out,
2040 "void cxxbridge1$shared_ptr${}$drop(::std::shared_ptr<{}> *self) noexcept {{",
2041 instance, inner,
2042 );
2043 writeln!(out, " self->~shared_ptr();");
2044 writeln!(out, "}}");
2045}
2046
2047fn write_weak_ptr(out: &mut OutFile, key: &NamedImplKey) {
2048 let resolve = out.types.resolve(key);
2049 let inner = resolve.name.to_fully_qualified();
2050 let instance = resolve.name.to_symbol();
2051
2052 out.include.new = true;
2053 out.include.utility = true;
2054 out.pragma.dollar_in_identifier = true;
2055 out.pragma.missing_declarations = true;
2056
2057 writeln!(
2058 out,
2059 "static_assert(sizeof(::std::weak_ptr<{}>) == 2 * sizeof(void *), \"\");",
2060 inner,
2061 );
2062 writeln!(
2063 out,
2064 "static_assert(alignof(::std::weak_ptr<{}>) == alignof(void *), \"\");",
2065 inner,
2066 );
2067
2068 begin_function_definition(out);
2069 writeln!(
2070 out,
2071 "void cxxbridge1$weak_ptr${}$null(::std::weak_ptr<{}> *ptr) noexcept {{",
2072 instance, inner,
2073 );
2074 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>();", inner);
2075 writeln!(out, "}}");
2076
2077 begin_function_definition(out);
2078 writeln!(
2079 out,
2080 "void cxxbridge1$weak_ptr${}$clone(::std::weak_ptr<{}> const &self, ::std::weak_ptr<{}> *ptr) noexcept {{",
2081 instance, inner, inner,
2082 );
2083 writeln!(out, " ::new (ptr) ::std::weak_ptr<{}>(self);", inner);
2084 writeln!(out, "}}");
2085
2086 begin_function_definition(out);
2087 writeln!(
2088 out,
2089 "void cxxbridge1$weak_ptr${}$downgrade(::std::shared_ptr<{}> const &shared, ::std::weak_ptr<{}> *weak) noexcept {{",
2090 instance, inner, inner,
2091 );
2092 writeln!(out, " ::new (weak) ::std::weak_ptr<{}>(shared);", inner);
2093 writeln!(out, "}}");
2094
2095 begin_function_definition(out);
2096 writeln!(
2097 out,
2098 "void cxxbridge1$weak_ptr${}$upgrade(::std::weak_ptr<{}> const &weak, ::std::shared_ptr<{}> *shared) noexcept {{",
2099 instance, inner, inner,
2100 );
2101 writeln!(
2102 out,
2103 " ::new (shared) ::std::shared_ptr<{}>(weak.lock());",
2104 inner,
2105 );
2106 writeln!(out, "}}");
2107
2108 begin_function_definition(out);
2109 writeln!(
2110 out,
2111 "void cxxbridge1$weak_ptr${}$drop(::std::weak_ptr<{}> *self) noexcept {{",
2112 instance, inner,
2113 );
2114 writeln!(out, " self->~weak_ptr();");
2115 writeln!(out, "}}");
2116}
2117
2118fn write_cxx_vector(out: &mut OutFile, key: &NamedImplKey) {
2119 let element = key.rust;
2120 let inner = element.to_typename(out.types);
2121 let instance = element.to_mangled(out.types);
2122
2123 out.include.cstddef = true;
2124 out.include.utility = true;
2125 out.builtin.destroy = true;
2126 out.pragma.dollar_in_identifier = true;
2127 out.pragma.missing_declarations = true;
2128
2129 begin_function_definition(out);
2130 writeln!(
2131 out,
2132 "::std::vector<{}> *cxxbridge1$std$vector${}$new() noexcept {{",
2133 inner, instance,
2134 );
2135 writeln!(out, " return new ::std::vector<{}>();", inner);
2136 writeln!(out, "}}");
2137
2138 begin_function_definition(out);
2139 writeln!(
2140 out,
2141 "::std::size_t cxxbridge1$std$vector${}$size(::std::vector<{}> const &s) noexcept {{",
2142 instance, inner,
2143 );
2144 writeln!(out, " return s.size();");
2145 writeln!(out, "}}");
2146
2147 begin_function_definition(out);
2148 writeln!(
2149 out,
2150 "::std::size_t cxxbridge1$std$vector${}$capacity(::std::vector<{}> const &s) noexcept {{",
2151 instance, inner,
2152 );
2153 writeln!(out, " return s.capacity();");
2154 writeln!(out, "}}");
2155
2156 begin_function_definition(out);
2157 writeln!(
2158 out,
2159 "{} *cxxbridge1$std$vector${}$get_unchecked(::std::vector<{}> *s, ::std::size_t pos) noexcept {{",
2160 inner, instance, inner,
2161 );
2162 writeln!(out, " return &(*s)[pos];");
2163 writeln!(out, "}}");
2164
2165 begin_function_definition(out);
2166 writeln!(
2167 out,
2168 "void cxxbridge1$std$vector${}$reserve(::std::vector<{}> *s, ::std::size_t new_cap) noexcept {{",
2169 instance, inner,
2170 );
2171 writeln!(out, " s->reserve(new_cap);");
2172 writeln!(out, "}}");
2173
2174 if out.types.is_maybe_trivial(element) {
2175 begin_function_definition(out);
2176 writeln!(
2177 out,
2178 "void cxxbridge1$std$vector${}$push_back(::std::vector<{}> *v, {} *value) noexcept {{",
2179 instance, inner, inner,
2180 );
2181 writeln!(out, " v->push_back(::std::move(*value));");
2182 writeln!(out, " ::rust::destroy(value);");
2183 writeln!(out, "}}");
2184
2185 begin_function_definition(out);
2186 writeln!(
2187 out,
2188 "void cxxbridge1$std$vector${}$pop_back(::std::vector<{}> *v, {} *out) noexcept {{",
2189 instance, inner, inner,
2190 );
2191 writeln!(out, " ::new (out) {}(::std::move(v->back()));", inner);
2192 writeln!(out, " v->pop_back();");
2193 writeln!(out, "}}");
2194 }
2195
2196 out.include.memory = true;
2197 write_unique_ptr_common(out, UniquePtr::CxxVector(element));
2198}