blob: 8e6744ee4ab1629047c2aeae00b4b91f9a726a0d [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,
David Tolnay365fc7c2020-11-25 16:08:13 -080015 pub opaque: bool,
David Tolnay3be0e1f2020-10-31 20:53:00 -070016 pub unsafe_bitcopy: bool,
17 pub rust_error: bool,
18 pub manually_drop: bool,
19 pub maybe_uninit: bool,
20 pub trycatch: bool,
David Tolnay919085c2020-10-31 22:32:22 -070021 pub ptr_len: bool,
David Tolnay3be0e1f2020-10-31 20:53:00 -070022 pub rust_str_new_unchecked: bool,
23 pub rust_str_repr: bool,
David Tolnay36aa9e02020-10-31 23:08:21 -070024 pub rust_slice_new: bool,
25 pub rust_slice_repr: bool,
David Tolnay1f010c62020-11-01 20:27:46 -080026 pub exception: bool,
David Tolnay174bd952020-11-02 09:23:12 -080027 pub relocatable: bool,
David Tolnay22664ae2020-11-04 16:30:45 -080028 pub friend_impl: bool,
David Tolnay53462762020-11-25 07:08:10 -080029 pub is_complete: bool,
30 pub deleter_if: bool,
David Tolnay97c5b862020-11-01 14:59:01 -080031 pub content: Content<'a>,
David Tolnay3be0e1f2020-10-31 20:53:00 -070032}
33
David Tolnay97c5b862020-11-01 14:59:01 -080034impl<'a> Builtins<'a> {
David Tolnay3be0e1f2020-10-31 20:53:00 -070035 pub fn new() -> Self {
36 Builtins::default()
37 }
38}
David Tolnay3374d8d2020-10-31 22:18:45 -070039
40pub(super) fn write(out: &mut OutFile) {
41 if out.builtin == Default::default() {
42 return;
43 }
44
45 let include = &mut out.include;
46 let builtin = &mut out.builtin;
47 let out = &mut builtin.content;
48
David Tolnaydcfa8e92020-11-02 09:50:06 -080049 if builtin.rust_string {
50 include.array = true;
51 include.cstdint = true;
52 include.string = true;
53 }
54
55 if builtin.rust_str {
56 include.cstdint = true;
57 include.string = true;
David Tolnay22664ae2020-11-04 16:30:45 -080058 builtin.friend_impl = true;
59 }
60
61 if builtin.rust_slice {
David Tolnay450d8cd2020-11-25 20:36:06 -080062 include.type_traits = true;
David Tolnay22664ae2020-11-04 16:30:45 -080063 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080064 }
65
66 if builtin.rust_box {
67 include.new = true;
68 include.type_traits = true;
David Tolnayea6f86c2020-11-10 15:08:57 -080069 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080070 }
71
72 if builtin.rust_vec {
73 include.array = true;
74 include.new = true;
75 include.type_traits = true;
David Tolnayfb6b73c2020-11-10 14:32:16 -080076 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080077 builtin.panic = true;
78 builtin.unsafe_bitcopy = true;
79 }
80
David Tolnaya5a13012020-11-10 15:03:09 -080081 if builtin.rust_fn {
82 include.utility = true;
83 }
84
David Tolnaydcfa8e92020-11-02 09:50:06 -080085 if builtin.rust_error {
86 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080087 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080088 }
89
90 if builtin.rust_isize {
91 include.basetsd = true;
92 }
93
David Tolnay174bd952020-11-02 09:23:12 -080094 if builtin.relocatable {
95 include.type_traits = true;
96 }
97
David Tolnay0c033e32020-11-01 15:15:48 -080098 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -080099 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700100 writeln!(out, "// #include \"rust/cxx.h\"");
101
David Tolnay0f0162f2020-11-16 23:43:37 -0800102 ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC");
David Tolnay3374d8d2020-10-31 22:18:45 -0700103
104 if builtin.rust_string {
105 out.next_section();
106 writeln!(out, "struct unsafe_bitcopy_t;");
107 }
108
David Tolnay22664ae2020-11-04 16:30:45 -0800109 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800110 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700111 writeln!(out, "template <typename T>");
112 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800113 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700114 }
115
David Tolnay0f0162f2020-11-16 23:43:37 -0800116 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
117 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
118 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
119 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
120 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
121 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
122 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
123 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
124 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800125 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay0f0162f2020-11-16 23:43:37 -0800126 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700127
128 if builtin.manually_drop {
129 out.next_section();
130 include.utility = true;
131 writeln!(out, "template <typename T>");
132 writeln!(out, "union ManuallyDrop {{");
133 writeln!(out, " T value;");
134 writeln!(
135 out,
136 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
137 );
138 writeln!(out, " ~ManuallyDrop() {{}}");
139 writeln!(out, "}};");
140 }
141
142 if builtin.maybe_uninit {
143 out.next_section();
144 writeln!(out, "template <typename T>");
145 writeln!(out, "union MaybeUninit {{");
146 writeln!(out, " T value;");
147 writeln!(out, " MaybeUninit() {{}}");
148 writeln!(out, " ~MaybeUninit() {{}}");
149 writeln!(out, "}};");
150 }
151
David Tolnay0c033e32020-11-01 15:15:48 -0800152 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700153
David Tolnay919085c2020-10-31 22:32:22 -0700154 if builtin.ptr_len {
David Tolnay0c033e32020-11-01 15:15:48 -0800155 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700156 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800157 writeln!(out, " void *ptr;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700158 writeln!(out, " size_t len;");
159 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800160 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700161 }
162
163 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
164 out.next_section();
165 writeln!(out, "template <>");
166 writeln!(out, "class impl<Str> final {{");
167 writeln!(out, "public:");
168 if builtin.rust_str_new_unchecked {
169 writeln!(
170 out,
171 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
172 );
173 writeln!(out, " Str str;");
174 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
175 writeln!(out, " str.len = repr.len;");
176 writeln!(out, " return str;");
177 writeln!(out, " }}");
178 }
179 if builtin.rust_str_repr {
180 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800181 writeln!(
182 out,
183 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
184 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700185 writeln!(out, " }}");
186 }
187 writeln!(out, "}};");
188 }
189
David Tolnay36aa9e02020-10-31 23:08:21 -0700190 if builtin.rust_slice_new || builtin.rust_slice_repr {
191 out.next_section();
192 writeln!(out, "template <typename T>");
193 writeln!(out, "class impl<Slice<T>> final {{");
194 writeln!(out, "public:");
195 if builtin.rust_slice_new {
196 writeln!(
197 out,
198 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
199 );
David Tolnayc5629f02020-11-23 18:32:46 -0800200 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700201 writeln!(out, " }}");
202 }
203 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800204 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700205 writeln!(
206 out,
207 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
208 );
David Tolnayc5629f02020-11-23 18:32:46 -0800209 writeln!(out, " return repr::PtrLen{{");
210 writeln!(
211 out,
212 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
213 );
214 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700215 writeln!(out, " }}");
216 }
217 writeln!(out, "}};");
218 }
219
David Tolnay3374d8d2020-10-31 22:18:45 -0700220 if builtin.rust_error {
221 out.next_section();
222 writeln!(out, "template <>");
223 writeln!(out, "class impl<Error> final {{");
224 writeln!(out, "public:");
225 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
226 writeln!(out, " Error error;");
227 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
228 writeln!(out, " error.len = repr.len;");
229 writeln!(out, " return error;");
230 writeln!(out, " }}");
231 writeln!(out, "}};");
232 }
233
David Tolnay53462762020-11-25 07:08:10 -0800234 if builtin.is_complete {
235 include.type_traits = true;
236 out.next_section();
237 writeln!(out, "template <typename T, typename = size_t>");
238 writeln!(out, "struct is_complete : std::false_type {{}};");
239 out.next_section();
240 writeln!(out, "template <typename T>");
241 writeln!(
242 out,
243 "struct is_complete<T, decltype(sizeof(T))> : std::true_type {{}};",
244 );
245 }
246
247 if builtin.deleter_if {
248 out.next_section();
249 writeln!(out, "template <bool> struct deleter_if {{");
250 writeln!(out, " template <typename T> void operator()(T *) {{}}");
251 writeln!(out, "}};");
252 out.next_section();
253 writeln!(out, "template <> struct deleter_if<true> {{");
254 writeln!(
255 out,
256 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
257 );
258 writeln!(out, "}};");
259 }
260
David Tolnay0c033e32020-11-01 15:15:48 -0800261 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800262 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700263
264 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800265 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700266 include.exception = true;
267 include.type_traits = true;
268 include.utility = true;
269 writeln!(out, "class missing {{}};");
270 writeln!(out, "missing trycatch(...);");
271 writeln!(out);
272 writeln!(out, "template <typename Try, typename Fail>");
273 writeln!(out, "static typename ::std::enable_if<");
274 writeln!(
275 out,
276 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
277 );
278 writeln!(out, " missing>::value>::type");
279 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
280 writeln!(out, " func();");
281 writeln!(out, "}} catch (const ::std::exception &e) {{");
282 writeln!(out, " fail(e.what());");
283 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800284 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700285 }
286
David Tolnay0c033e32020-11-01 15:15:48 -0800287 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800288
289 if builtin.exception {
290 out.begin_block(Block::ExternC);
291 writeln!(
292 out,
David Tolnay0f0162f2020-11-16 23:43:37 -0800293 "const char *cxxbridge1$exception(const char *, size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800294 );
295 out.end_block(Block::ExternC);
296 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700297}