blob: cee188d5e7dcc133ebc083144ff1fd957e4e27cb [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 Tolnayd1df4c72020-11-25 20:38:05 -080062 include.iterator = true;
David Tolnay450d8cd2020-11-25 20:36:06 -080063 include.type_traits = true;
David Tolnay22664ae2020-11-04 16:30:45 -080064 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080065 }
66
67 if builtin.rust_box {
68 include.new = true;
69 include.type_traits = true;
David Tolnayea6f86c2020-11-10 15:08:57 -080070 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080071 }
72
73 if builtin.rust_vec {
David Tolnayf799b372020-11-29 23:57:42 -080074 include.algorithm = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080075 include.array = true;
David Tolnayf799b372020-11-29 23:57:42 -080076 include.initializer_list = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080077 include.iterator = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080078 include.new = true;
79 include.type_traits = true;
David Tolnayfb6b73c2020-11-10 14:32:16 -080080 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080081 builtin.panic = true;
82 builtin.unsafe_bitcopy = true;
83 }
84
David Tolnaya5a13012020-11-10 15:03:09 -080085 if builtin.rust_fn {
86 include.utility = true;
87 }
88
David Tolnaydcfa8e92020-11-02 09:50:06 -080089 if builtin.rust_error {
90 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080091 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080092 }
93
94 if builtin.rust_isize {
95 include.basetsd = true;
David Tolnay74dd66f2020-12-12 22:03:47 -080096 include.sys_types = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080097 }
98
David Tolnay174bd952020-11-02 09:23:12 -080099 if builtin.relocatable {
100 include.type_traits = true;
101 }
102
David Tolnay0c033e32020-11-01 15:15:48 -0800103 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -0800104 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700105 writeln!(out, "// #include \"rust/cxx.h\"");
106
David Tolnay0f0162f2020-11-16 23:43:37 -0800107 ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC");
David Tolnay3374d8d2020-10-31 22:18:45 -0700108
109 if builtin.rust_string {
110 out.next_section();
111 writeln!(out, "struct unsafe_bitcopy_t;");
112 }
113
David Tolnay22664ae2020-11-04 16:30:45 -0800114 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800115 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700116 writeln!(out, "template <typename T>");
117 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800118 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700119 }
120
David Tolnay828e5132020-11-29 20:40:40 -0800121 if builtin.rust_str && !builtin.rust_string {
122 out.next_section();
123 writeln!(out, "class String;");
124 }
125
David Tolnay0f0162f2020-11-16 23:43:37 -0800126 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
127 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
128 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
129 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
130 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
131 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
132 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
133 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
134 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800135 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay0f0162f2020-11-16 23:43:37 -0800136 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700137
David Tolnay718ca0f2020-12-09 23:01:11 -0800138 out.begin_block(Block::Namespace("detail"));
139
140 if builtin.maybe_uninit {
141 include.cstddef = true;
142 include.new = true;
143 out.next_section();
144 writeln!(out, "template <typename T, typename = void *>");
145 writeln!(out, "struct operator_new {{");
146 writeln!(
147 out,
148 " void *operator()(size_t sz) {{ return ::operator new(sz); }}",
149 );
150 writeln!(out, "}};");
151 out.next_section();
152 writeln!(out, "template <typename T>");
153 writeln!(
154 out,
155 "struct operator_new<T, decltype(T::operator new(sizeof(T)))> {{",
156 );
157 writeln!(
158 out,
159 " void *operator()(size_t sz) {{ return T::operator new(sz); }}",
160 );
161 writeln!(out, "}};");
162 }
163
164 out.end_block(Block::Namespace("detail"));
165
David Tolnay3374d8d2020-10-31 22:18:45 -0700166 if builtin.manually_drop {
167 out.next_section();
168 include.utility = true;
169 writeln!(out, "template <typename T>");
170 writeln!(out, "union ManuallyDrop {{");
171 writeln!(out, " T value;");
172 writeln!(
173 out,
174 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
175 );
176 writeln!(out, " ~ManuallyDrop() {{}}");
177 writeln!(out, "}};");
178 }
179
180 if builtin.maybe_uninit {
David Tolnay718ca0f2020-12-09 23:01:11 -0800181 include.cstddef = true;
David Tolnay3374d8d2020-10-31 22:18:45 -0700182 out.next_section();
183 writeln!(out, "template <typename T>");
184 writeln!(out, "union MaybeUninit {{");
185 writeln!(out, " T value;");
David Tolnay718ca0f2020-12-09 23:01:11 -0800186 writeln!(
187 out,
188 " void *operator new(size_t sz) {{ return detail::operator_new<T>{{}}(sz); }}",
189 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700190 writeln!(out, " MaybeUninit() {{}}");
191 writeln!(out, " ~MaybeUninit() {{}}");
192 writeln!(out, "}};");
193 }
194
David Tolnay0c033e32020-11-01 15:15:48 -0800195 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700196
David Tolnay919085c2020-10-31 22:32:22 -0700197 if builtin.ptr_len {
David Tolnay12320e12020-12-09 23:09:36 -0800198 include.cstddef = true;
David Tolnay0c033e32020-11-01 15:15:48 -0800199 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700200 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800201 writeln!(out, " void *ptr;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700202 writeln!(out, " size_t len;");
203 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800204 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700205 }
206
207 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
208 out.next_section();
209 writeln!(out, "template <>");
210 writeln!(out, "class impl<Str> final {{");
211 writeln!(out, "public:");
212 if builtin.rust_str_new_unchecked {
213 writeln!(
214 out,
215 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
216 );
217 writeln!(out, " Str str;");
218 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
219 writeln!(out, " str.len = repr.len;");
220 writeln!(out, " return str;");
221 writeln!(out, " }}");
222 }
223 if builtin.rust_str_repr {
224 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800225 writeln!(
226 out,
227 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
228 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700229 writeln!(out, " }}");
230 }
231 writeln!(out, "}};");
232 }
233
David Tolnay36aa9e02020-10-31 23:08:21 -0700234 if builtin.rust_slice_new || builtin.rust_slice_repr {
235 out.next_section();
236 writeln!(out, "template <typename T>");
237 writeln!(out, "class impl<Slice<T>> final {{");
238 writeln!(out, "public:");
239 if builtin.rust_slice_new {
240 writeln!(
241 out,
242 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
243 );
David Tolnayc5629f02020-11-23 18:32:46 -0800244 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700245 writeln!(out, " }}");
246 }
247 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800248 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700249 writeln!(
250 out,
251 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
252 );
David Tolnayc5629f02020-11-23 18:32:46 -0800253 writeln!(out, " return repr::PtrLen{{");
254 writeln!(
255 out,
256 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
257 );
258 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700259 writeln!(out, " }}");
260 }
261 writeln!(out, "}};");
262 }
263
David Tolnay3374d8d2020-10-31 22:18:45 -0700264 if builtin.rust_error {
265 out.next_section();
266 writeln!(out, "template <>");
267 writeln!(out, "class impl<Error> final {{");
268 writeln!(out, "public:");
269 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
270 writeln!(out, " Error error;");
271 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
272 writeln!(out, " error.len = repr.len;");
273 writeln!(out, " return error;");
274 writeln!(out, " }}");
275 writeln!(out, "}};");
276 }
277
David Tolnay53462762020-11-25 07:08:10 -0800278 if builtin.is_complete {
David Tolnay12320e12020-12-09 23:09:36 -0800279 include.cstddef = true;
David Tolnay53462762020-11-25 07:08:10 -0800280 include.type_traits = true;
281 out.next_section();
282 writeln!(out, "template <typename T, typename = size_t>");
283 writeln!(out, "struct is_complete : std::false_type {{}};");
284 out.next_section();
285 writeln!(out, "template <typename T>");
286 writeln!(
287 out,
288 "struct is_complete<T, decltype(sizeof(T))> : std::true_type {{}};",
289 );
290 }
291
292 if builtin.deleter_if {
293 out.next_section();
294 writeln!(out, "template <bool> struct deleter_if {{");
295 writeln!(out, " template <typename T> void operator()(T *) {{}}");
296 writeln!(out, "}};");
297 out.next_section();
298 writeln!(out, "template <> struct deleter_if<true> {{");
299 writeln!(
300 out,
301 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
302 );
303 writeln!(out, "}};");
304 }
305
David Tolnay0c033e32020-11-01 15:15:48 -0800306 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800307 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700308
309 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800310 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700311 include.exception = true;
312 include.type_traits = true;
313 include.utility = true;
314 writeln!(out, "class missing {{}};");
315 writeln!(out, "missing trycatch(...);");
316 writeln!(out);
317 writeln!(out, "template <typename Try, typename Fail>");
318 writeln!(out, "static typename ::std::enable_if<");
319 writeln!(
320 out,
321 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
322 );
323 writeln!(out, " missing>::value>::type");
324 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
325 writeln!(out, " func();");
326 writeln!(out, "}} catch (const ::std::exception &e) {{");
327 writeln!(out, " fail(e.what());");
328 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800329 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700330 }
331
David Tolnay0c033e32020-11-01 15:15:48 -0800332 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800333
334 if builtin.exception {
David Tolnay12320e12020-12-09 23:09:36 -0800335 include.cstddef = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800336 out.begin_block(Block::ExternC);
337 writeln!(
338 out,
David Tolnay0f0162f2020-11-16 23:43:37 -0800339 "const char *cxxbridge1$exception(const char *, size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800340 );
341 out.end_block(Block::ExternC);
342 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700343}