blob: 4b8dce59ecb6f194c75123b5a4069fc8bd419848 [file] [log] [blame]
David Tolnay0c033e32020-11-01 15:15:48 -08001use crate::gen::block::Block;
David Tolnay3374d8d2020-10-31 22:18:45 -07002use crate::gen::ifndef;
3use crate::gen::out::{Content, OutFile};
David Tolnay8c14d9a2020-10-31 21:52:38 -07004
David Tolnay3be0e1f2020-10-31 20:53:00 -07005#[derive(Default, PartialEq)]
David Tolnay97c5b862020-11-01 14:59:01 -08006pub struct Builtins<'a> {
David Tolnay3be0e1f2020-10-31 20:53:00 -07007 pub panic: bool,
8 pub rust_string: bool,
9 pub rust_str: bool,
10 pub rust_slice: bool,
11 pub rust_box: bool,
12 pub rust_vec: bool,
13 pub rust_fn: bool,
14 pub rust_isize: bool,
15 pub unsafe_bitcopy: bool,
16 pub rust_error: bool,
17 pub manually_drop: bool,
18 pub maybe_uninit: bool,
19 pub trycatch: bool,
David Tolnay919085c2020-10-31 22:32:22 -070020 pub ptr_len: bool,
David Tolnay3be0e1f2020-10-31 20:53:00 -070021 pub rust_str_new_unchecked: bool,
22 pub rust_str_repr: bool,
David Tolnay36aa9e02020-10-31 23:08:21 -070023 pub rust_slice_new: bool,
24 pub rust_slice_repr: bool,
David Tolnay97c5b862020-11-01 14:59:01 -080025 pub content: Content<'a>,
David Tolnay3be0e1f2020-10-31 20:53:00 -070026}
27
David Tolnay97c5b862020-11-01 14:59:01 -080028impl<'a> Builtins<'a> {
David Tolnay3be0e1f2020-10-31 20:53:00 -070029 pub fn new() -> Self {
30 Builtins::default()
31 }
32}
David Tolnay3374d8d2020-10-31 22:18:45 -070033
34pub(super) fn write(out: &mut OutFile) {
35 if out.builtin == Default::default() {
36 return;
37 }
38
39 let include = &mut out.include;
40 let builtin = &mut out.builtin;
41 let out = &mut builtin.content;
42
David Tolnay0c033e32020-11-01 15:15:48 -080043 out.begin_block(Block::Namespace("rust"));
44 out.begin_block(Block::InlineNamespace("cxxbridge05"));
David Tolnay3374d8d2020-10-31 22:18:45 -070045 writeln!(out, "// #include \"rust/cxx.h\"");
46
47 ifndef::write(out, builtin.panic, "CXXBRIDGE05_PANIC");
48
49 if builtin.rust_string {
50 out.next_section();
51 writeln!(out, "struct unsafe_bitcopy_t;");
52 }
53
54 if builtin.rust_error {
David Tolnay0c033e32020-11-01 15:15:48 -080055 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -070056 writeln!(out, "template <typename T>");
57 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -080058 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -070059 }
60
61 ifndef::write(out, builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
62 ifndef::write(out, builtin.rust_str, "CXXBRIDGE05_RUST_STR");
63 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
64 ifndef::write(out, builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
65 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
66 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
67 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
68 ifndef::write(out, builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
69 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
70
71 if builtin.manually_drop {
72 out.next_section();
73 include.utility = true;
74 writeln!(out, "template <typename T>");
75 writeln!(out, "union ManuallyDrop {{");
76 writeln!(out, " T value;");
77 writeln!(
78 out,
79 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
80 );
81 writeln!(out, " ~ManuallyDrop() {{}}");
82 writeln!(out, "}};");
83 }
84
85 if builtin.maybe_uninit {
86 out.next_section();
87 writeln!(out, "template <typename T>");
88 writeln!(out, "union MaybeUninit {{");
89 writeln!(out, " T value;");
90 writeln!(out, " MaybeUninit() {{}}");
91 writeln!(out, " ~MaybeUninit() {{}}");
92 writeln!(out, "}};");
93 }
94
David Tolnay0c033e32020-11-01 15:15:48 -080095 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -070096
David Tolnay919085c2020-10-31 22:32:22 -070097 if builtin.ptr_len {
David Tolnay0c033e32020-11-01 15:15:48 -080098 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -070099 writeln!(out, "struct PtrLen final {{");
100 writeln!(out, " const void *ptr;");
101 writeln!(out, " size_t len;");
102 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800103 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700104 }
105
106 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
107 out.next_section();
108 writeln!(out, "template <>");
109 writeln!(out, "class impl<Str> final {{");
110 writeln!(out, "public:");
111 if builtin.rust_str_new_unchecked {
112 writeln!(
113 out,
114 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
115 );
116 writeln!(out, " Str str;");
117 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
118 writeln!(out, " str.len = repr.len;");
119 writeln!(out, " return str;");
120 writeln!(out, " }}");
121 }
122 if builtin.rust_str_repr {
123 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
124 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
125 writeln!(out, " }}");
126 }
127 writeln!(out, "}};");
128 }
129
David Tolnay36aa9e02020-10-31 23:08:21 -0700130 if builtin.rust_slice_new || builtin.rust_slice_repr {
131 out.next_section();
132 writeln!(out, "template <typename T>");
133 writeln!(out, "class impl<Slice<T>> final {{");
134 writeln!(out, "public:");
135 if builtin.rust_slice_new {
136 writeln!(
137 out,
138 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
139 );
140 writeln!(
141 out,
142 " return {{static_cast<const T *>(repr.ptr), repr.len}};",
143 );
144 writeln!(out, " }}");
145 }
146 if builtin.rust_slice_repr {
147 writeln!(
148 out,
149 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
150 );
151 writeln!(out, " return repr::PtrLen{{slice.ptr, slice.len}};");
152 writeln!(out, " }}");
153 }
154 writeln!(out, "}};");
155 }
156
David Tolnay3374d8d2020-10-31 22:18:45 -0700157 if builtin.rust_error {
158 out.next_section();
159 writeln!(out, "template <>");
160 writeln!(out, "class impl<Error> final {{");
161 writeln!(out, "public:");
162 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
163 writeln!(out, " Error error;");
164 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
165 writeln!(out, " error.len = repr.len;");
166 writeln!(out, " return error;");
167 writeln!(out, " }}");
168 writeln!(out, "}};");
169 }
170
David Tolnay0c033e32020-11-01 15:15:48 -0800171 out.end_block(Block::AnonymousNamespace);
172 out.end_block(Block::InlineNamespace("cxxbridge05"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700173
174 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800175 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700176 include.exception = true;
177 include.type_traits = true;
178 include.utility = true;
179 writeln!(out, "class missing {{}};");
180 writeln!(out, "missing trycatch(...);");
181 writeln!(out);
182 writeln!(out, "template <typename Try, typename Fail>");
183 writeln!(out, "static typename ::std::enable_if<");
184 writeln!(
185 out,
186 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
187 );
188 writeln!(out, " missing>::value>::type");
189 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
190 writeln!(out, " func();");
191 writeln!(out, "}} catch (const ::std::exception &e) {{");
192 writeln!(out, " fail(e.what());");
193 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800194 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700195 }
196
David Tolnay0c033e32020-11-01 15:15:48 -0800197 out.end_block(Block::Namespace("rust"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700198}