blob: b32443c863a8fcff9169c4ac2dcd37506e502e68 [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 Tolnay08679ae2020-12-12 22:10:01 -080062 include.cstddef = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080063 include.iterator = true;
David Tolnay450d8cd2020-11-25 20:36:06 -080064 include.type_traits = true;
David Tolnay22664ae2020-11-04 16:30:45 -080065 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080066 }
67
68 if builtin.rust_box {
69 include.new = true;
70 include.type_traits = true;
David Tolnayea6f86c2020-11-10 15:08:57 -080071 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080072 }
73
74 if builtin.rust_vec {
David Tolnayf799b372020-11-29 23:57:42 -080075 include.algorithm = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080076 include.array = true;
David Tolnay08679ae2020-12-12 22:10:01 -080077 include.cstddef = true;
David Tolnayf799b372020-11-29 23:57:42 -080078 include.initializer_list = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080079 include.iterator = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080080 include.new = true;
81 include.type_traits = true;
David Tolnayfb6b73c2020-11-10 14:32:16 -080082 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080083 builtin.panic = true;
84 builtin.unsafe_bitcopy = true;
85 }
86
David Tolnaya5a13012020-11-10 15:03:09 -080087 if builtin.rust_fn {
88 include.utility = true;
89 }
90
David Tolnaydcfa8e92020-11-02 09:50:06 -080091 if builtin.rust_error {
92 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080093 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080094 }
95
96 if builtin.rust_isize {
97 include.basetsd = true;
David Tolnay74dd66f2020-12-12 22:03:47 -080098 include.sys_types = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080099 }
100
David Tolnay174bd952020-11-02 09:23:12 -0800101 if builtin.relocatable {
102 include.type_traits = true;
103 }
104
David Tolnay0c033e32020-11-01 15:15:48 -0800105 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -0800106 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700107 writeln!(out, "// #include \"rust/cxx.h\"");
108
David Tolnay0f0162f2020-11-16 23:43:37 -0800109 ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC");
David Tolnay3374d8d2020-10-31 22:18:45 -0700110
111 if builtin.rust_string {
112 out.next_section();
113 writeln!(out, "struct unsafe_bitcopy_t;");
114 }
115
David Tolnay22664ae2020-11-04 16:30:45 -0800116 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800117 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700118 writeln!(out, "template <typename T>");
119 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800120 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700121 }
122
David Tolnay828e5132020-11-29 20:40:40 -0800123 if builtin.rust_str && !builtin.rust_string {
124 out.next_section();
125 writeln!(out, "class String;");
126 }
127
David Tolnay0f0162f2020-11-16 23:43:37 -0800128 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
129 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
130 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
131 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
132 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
133 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
134 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
135 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
136 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800137 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay0f0162f2020-11-16 23:43:37 -0800138 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700139
David Tolnay718ca0f2020-12-09 23:01:11 -0800140 out.begin_block(Block::Namespace("detail"));
141
142 if builtin.maybe_uninit {
143 include.cstddef = true;
144 include.new = true;
145 out.next_section();
146 writeln!(out, "template <typename T, typename = void *>");
147 writeln!(out, "struct operator_new {{");
148 writeln!(
149 out,
150 " void *operator()(size_t sz) {{ return ::operator new(sz); }}",
151 );
152 writeln!(out, "}};");
153 out.next_section();
154 writeln!(out, "template <typename T>");
155 writeln!(
156 out,
157 "struct operator_new<T, decltype(T::operator new(sizeof(T)))> {{",
158 );
159 writeln!(
160 out,
161 " void *operator()(size_t sz) {{ return T::operator new(sz); }}",
162 );
163 writeln!(out, "}};");
164 }
165
166 out.end_block(Block::Namespace("detail"));
167
David Tolnay3374d8d2020-10-31 22:18:45 -0700168 if builtin.manually_drop {
169 out.next_section();
170 include.utility = true;
171 writeln!(out, "template <typename T>");
172 writeln!(out, "union ManuallyDrop {{");
173 writeln!(out, " T value;");
174 writeln!(
175 out,
176 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
177 );
178 writeln!(out, " ~ManuallyDrop() {{}}");
179 writeln!(out, "}};");
180 }
181
182 if builtin.maybe_uninit {
David Tolnay718ca0f2020-12-09 23:01:11 -0800183 include.cstddef = true;
David Tolnay3374d8d2020-10-31 22:18:45 -0700184 out.next_section();
185 writeln!(out, "template <typename T>");
186 writeln!(out, "union MaybeUninit {{");
187 writeln!(out, " T value;");
David Tolnay718ca0f2020-12-09 23:01:11 -0800188 writeln!(
189 out,
190 " void *operator new(size_t sz) {{ return detail::operator_new<T>{{}}(sz); }}",
191 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700192 writeln!(out, " MaybeUninit() {{}}");
193 writeln!(out, " ~MaybeUninit() {{}}");
194 writeln!(out, "}};");
195 }
196
David Tolnay0c033e32020-11-01 15:15:48 -0800197 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700198
David Tolnay919085c2020-10-31 22:32:22 -0700199 if builtin.ptr_len {
David Tolnay12320e12020-12-09 23:09:36 -0800200 include.cstddef = true;
David Tolnay0c033e32020-11-01 15:15:48 -0800201 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700202 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800203 writeln!(out, " void *ptr;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700204 writeln!(out, " size_t len;");
205 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800206 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700207 }
208
209 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
210 out.next_section();
211 writeln!(out, "template <>");
212 writeln!(out, "class impl<Str> final {{");
213 writeln!(out, "public:");
214 if builtin.rust_str_new_unchecked {
215 writeln!(
216 out,
217 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
218 );
219 writeln!(out, " Str str;");
220 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
221 writeln!(out, " str.len = repr.len;");
222 writeln!(out, " return str;");
223 writeln!(out, " }}");
224 }
225 if builtin.rust_str_repr {
226 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800227 writeln!(
228 out,
229 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
230 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700231 writeln!(out, " }}");
232 }
233 writeln!(out, "}};");
234 }
235
David Tolnay36aa9e02020-10-31 23:08:21 -0700236 if builtin.rust_slice_new || builtin.rust_slice_repr {
237 out.next_section();
238 writeln!(out, "template <typename T>");
239 writeln!(out, "class impl<Slice<T>> final {{");
240 writeln!(out, "public:");
241 if builtin.rust_slice_new {
242 writeln!(
243 out,
244 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
245 );
David Tolnayc5629f02020-11-23 18:32:46 -0800246 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700247 writeln!(out, " }}");
248 }
249 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800250 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700251 writeln!(
252 out,
253 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
254 );
David Tolnayc5629f02020-11-23 18:32:46 -0800255 writeln!(out, " return repr::PtrLen{{");
256 writeln!(
257 out,
258 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
259 );
260 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700261 writeln!(out, " }}");
262 }
263 writeln!(out, "}};");
264 }
265
David Tolnay3374d8d2020-10-31 22:18:45 -0700266 if builtin.rust_error {
267 out.next_section();
268 writeln!(out, "template <>");
269 writeln!(out, "class impl<Error> final {{");
270 writeln!(out, "public:");
271 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
272 writeln!(out, " Error error;");
273 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
274 writeln!(out, " error.len = repr.len;");
275 writeln!(out, " return error;");
276 writeln!(out, " }}");
277 writeln!(out, "}};");
278 }
279
David Tolnay53462762020-11-25 07:08:10 -0800280 if builtin.is_complete {
David Tolnay12320e12020-12-09 23:09:36 -0800281 include.cstddef = true;
David Tolnay53462762020-11-25 07:08:10 -0800282 include.type_traits = true;
283 out.next_section();
284 writeln!(out, "template <typename T, typename = size_t>");
285 writeln!(out, "struct is_complete : std::false_type {{}};");
286 out.next_section();
287 writeln!(out, "template <typename T>");
288 writeln!(
289 out,
290 "struct is_complete<T, decltype(sizeof(T))> : std::true_type {{}};",
291 );
292 }
293
294 if builtin.deleter_if {
295 out.next_section();
296 writeln!(out, "template <bool> struct deleter_if {{");
297 writeln!(out, " template <typename T> void operator()(T *) {{}}");
298 writeln!(out, "}};");
299 out.next_section();
300 writeln!(out, "template <> struct deleter_if<true> {{");
301 writeln!(
302 out,
303 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
304 );
305 writeln!(out, "}};");
306 }
307
David Tolnay0c033e32020-11-01 15:15:48 -0800308 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800309 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700310
311 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800312 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700313 include.exception = true;
314 include.type_traits = true;
315 include.utility = true;
316 writeln!(out, "class missing {{}};");
317 writeln!(out, "missing trycatch(...);");
318 writeln!(out);
319 writeln!(out, "template <typename Try, typename Fail>");
320 writeln!(out, "static typename ::std::enable_if<");
321 writeln!(
322 out,
323 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
324 );
325 writeln!(out, " missing>::value>::type");
326 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
327 writeln!(out, " func();");
328 writeln!(out, "}} catch (const ::std::exception &e) {{");
329 writeln!(out, " fail(e.what());");
330 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800331 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700332 }
333
David Tolnay0c033e32020-11-01 15:15:48 -0800334 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800335
336 if builtin.exception {
David Tolnay12320e12020-12-09 23:09:36 -0800337 include.cstddef = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800338 out.begin_block(Block::ExternC);
339 writeln!(
340 out,
David Tolnay0f0162f2020-11-16 23:43:37 -0800341 "const char *cxxbridge1$exception(const char *, size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800342 );
343 out.end_block(Block::ExternC);
344 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700345}