blob: d43038b086a952a3a48ba685fe17458787c9ab79 [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 Tolnayee6ecfc2020-12-26 21:54:37 -080016 pub layout: bool,
David Tolnay3be0e1f2020-10-31 20:53:00 -070017 pub unsafe_bitcopy: bool,
18 pub rust_error: bool,
19 pub manually_drop: bool,
20 pub maybe_uninit: bool,
21 pub trycatch: bool,
David Tolnay919085c2020-10-31 22:32:22 -070022 pub ptr_len: bool,
David Tolnay3be0e1f2020-10-31 20:53:00 -070023 pub rust_str_new_unchecked: bool,
24 pub rust_str_repr: bool,
David Tolnay36aa9e02020-10-31 23:08:21 -070025 pub rust_slice_new: bool,
26 pub rust_slice_repr: bool,
David Tolnay1f010c62020-11-01 20:27:46 -080027 pub exception: bool,
David Tolnay174bd952020-11-02 09:23:12 -080028 pub relocatable: bool,
David Tolnay22664ae2020-11-04 16:30:45 -080029 pub friend_impl: bool,
David Tolnay53462762020-11-25 07:08:10 -080030 pub is_complete: bool,
31 pub deleter_if: bool,
David Tolnay97c5b862020-11-01 14:59:01 -080032 pub content: Content<'a>,
David Tolnay3be0e1f2020-10-31 20:53:00 -070033}
34
David Tolnay97c5b862020-11-01 14:59:01 -080035impl<'a> Builtins<'a> {
David Tolnay3be0e1f2020-10-31 20:53:00 -070036 pub fn new() -> Self {
37 Builtins::default()
38 }
39}
David Tolnay3374d8d2020-10-31 22:18:45 -070040
41pub(super) fn write(out: &mut OutFile) {
42 if out.builtin == Default::default() {
43 return;
44 }
45
46 let include = &mut out.include;
47 let builtin = &mut out.builtin;
48 let out = &mut builtin.content;
49
David Tolnaydcfa8e92020-11-02 09:50:06 -080050 if builtin.rust_string {
51 include.array = true;
52 include.cstdint = true;
53 include.string = true;
54 }
55
56 if builtin.rust_str {
57 include.cstdint = true;
58 include.string = true;
David Tolnay22664ae2020-11-04 16:30:45 -080059 builtin.friend_impl = true;
60 }
61
62 if builtin.rust_slice {
David Tolnay08679ae2020-12-12 22:10:01 -080063 include.cstddef = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080064 include.iterator = true;
David Tolnay450d8cd2020-11-25 20:36:06 -080065 include.type_traits = true;
David Tolnay22664ae2020-11-04 16:30:45 -080066 builtin.friend_impl = true;
David Tolnay78dd5352020-12-26 23:44:20 -080067 builtin.panic = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080068 }
69
70 if builtin.rust_box {
71 include.new = true;
72 include.type_traits = true;
David Tolnayea6f86c2020-11-10 15:08:57 -080073 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080074 }
75
76 if builtin.rust_vec {
David Tolnayf799b372020-11-29 23:57:42 -080077 include.algorithm = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080078 include.array = true;
David Tolnay08679ae2020-12-12 22:10:01 -080079 include.cstddef = true;
David Tolnayf799b372020-11-29 23:57:42 -080080 include.initializer_list = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080081 include.iterator = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080082 include.new = true;
83 include.type_traits = true;
David Tolnayfb6b73c2020-11-10 14:32:16 -080084 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080085 builtin.panic = true;
86 builtin.unsafe_bitcopy = true;
87 }
88
David Tolnaya5a13012020-11-10 15:03:09 -080089 if builtin.rust_fn {
90 include.utility = true;
91 }
92
David Tolnaydcfa8e92020-11-02 09:50:06 -080093 if builtin.rust_error {
94 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080095 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080096 }
97
98 if builtin.rust_isize {
99 include.basetsd = true;
David Tolnay74dd66f2020-12-12 22:03:47 -0800100 include.sys_types = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -0800101 }
102
David Tolnay174bd952020-11-02 09:23:12 -0800103 if builtin.relocatable {
104 include.type_traits = true;
105 }
106
David Tolnayee6ecfc2020-12-26 21:54:37 -0800107 if builtin.layout {
108 include.type_traits = true;
109 include.cstddef = true;
110 builtin.is_complete = true;
111 }
112
David Tolnay75068632020-12-26 22:15:17 -0800113 if builtin.is_complete {
114 include.cstddef = true;
115 include.type_traits = true;
116 }
117
David Tolnay0c033e32020-11-01 15:15:48 -0800118 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -0800119 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700120 writeln!(out, "// #include \"rust/cxx.h\"");
121
David Tolnay0f0162f2020-11-16 23:43:37 -0800122 ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC");
David Tolnay3374d8d2020-10-31 22:18:45 -0700123
124 if builtin.rust_string {
125 out.next_section();
126 writeln!(out, "struct unsafe_bitcopy_t;");
127 }
128
David Tolnay22664ae2020-11-04 16:30:45 -0800129 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800130 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700131 writeln!(out, "template <typename T>");
132 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800133 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700134 }
135
David Tolnayee6ecfc2020-12-26 21:54:37 -0800136 out.next_section();
David Tolnay828e5132020-11-29 20:40:40 -0800137 if builtin.rust_str && !builtin.rust_string {
David Tolnay828e5132020-11-29 20:40:40 -0800138 writeln!(out, "class String;");
139 }
David Tolnayee6ecfc2020-12-26 21:54:37 -0800140 if builtin.layout && !builtin.opaque {
141 writeln!(out, "class Opaque;");
142 }
David Tolnay828e5132020-11-29 20:40:40 -0800143
David Tolnay0f0162f2020-11-16 23:43:37 -0800144 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
145 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
146 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
147 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
148 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
149 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
150 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
151 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
152 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800153 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay75068632020-12-26 22:15:17 -0800154 ifndef::write(out, builtin.is_complete, "CXXBRIDGE1_IS_COMPLETE");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800155 ifndef::write(out, builtin.layout, "CXXBRIDGE1_LAYOUT");
156 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700157
David Tolnay718ca0f2020-12-09 23:01:11 -0800158 out.begin_block(Block::Namespace("detail"));
159
160 if builtin.maybe_uninit {
161 include.cstddef = true;
162 include.new = true;
163 out.next_section();
164 writeln!(out, "template <typename T, typename = void *>");
165 writeln!(out, "struct operator_new {{");
166 writeln!(
167 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800168 " void *operator()(::std::size_t sz) {{ return ::operator new(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800169 );
170 writeln!(out, "}};");
171 out.next_section();
172 writeln!(out, "template <typename T>");
173 writeln!(
174 out,
175 "struct operator_new<T, decltype(T::operator new(sizeof(T)))> {{",
176 );
177 writeln!(
178 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800179 " void *operator()(::std::size_t sz) {{ return T::operator new(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800180 );
181 writeln!(out, "}};");
182 }
183
184 out.end_block(Block::Namespace("detail"));
185
David Tolnay3374d8d2020-10-31 22:18:45 -0700186 if builtin.manually_drop {
187 out.next_section();
188 include.utility = true;
189 writeln!(out, "template <typename T>");
190 writeln!(out, "union ManuallyDrop {{");
191 writeln!(out, " T value;");
192 writeln!(
193 out,
194 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
195 );
196 writeln!(out, " ~ManuallyDrop() {{}}");
197 writeln!(out, "}};");
198 }
199
200 if builtin.maybe_uninit {
David Tolnay718ca0f2020-12-09 23:01:11 -0800201 include.cstddef = true;
David Tolnay3374d8d2020-10-31 22:18:45 -0700202 out.next_section();
203 writeln!(out, "template <typename T>");
204 writeln!(out, "union MaybeUninit {{");
205 writeln!(out, " T value;");
David Tolnay718ca0f2020-12-09 23:01:11 -0800206 writeln!(
207 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800208 " void *operator new(::std::size_t sz) {{ return detail::operator_new<T>{{}}(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800209 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700210 writeln!(out, " MaybeUninit() {{}}");
211 writeln!(out, " ~MaybeUninit() {{}}");
212 writeln!(out, "}};");
213 }
214
David Tolnay0c033e32020-11-01 15:15:48 -0800215 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700216
David Tolnay919085c2020-10-31 22:32:22 -0700217 if builtin.ptr_len {
David Tolnay12320e12020-12-09 23:09:36 -0800218 include.cstddef = true;
David Tolnay0c033e32020-11-01 15:15:48 -0800219 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700220 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800221 writeln!(out, " void *ptr;");
David Tolnaybe3cbf72020-12-12 22:12:07 -0800222 writeln!(out, " ::std::size_t len;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700223 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800224 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700225 }
226
227 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
228 out.next_section();
229 writeln!(out, "template <>");
230 writeln!(out, "class impl<Str> final {{");
231 writeln!(out, "public:");
232 if builtin.rust_str_new_unchecked {
233 writeln!(
234 out,
235 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
236 );
237 writeln!(out, " Str str;");
238 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
239 writeln!(out, " str.len = repr.len;");
240 writeln!(out, " return str;");
241 writeln!(out, " }}");
242 }
243 if builtin.rust_str_repr {
244 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800245 writeln!(
246 out,
247 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
248 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700249 writeln!(out, " }}");
250 }
251 writeln!(out, "}};");
252 }
253
David Tolnay36aa9e02020-10-31 23:08:21 -0700254 if builtin.rust_slice_new || builtin.rust_slice_repr {
255 out.next_section();
256 writeln!(out, "template <typename T>");
257 writeln!(out, "class impl<Slice<T>> final {{");
258 writeln!(out, "public:");
259 if builtin.rust_slice_new {
260 writeln!(
261 out,
262 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
263 );
David Tolnayc5629f02020-11-23 18:32:46 -0800264 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700265 writeln!(out, " }}");
266 }
267 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800268 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700269 writeln!(
270 out,
271 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
272 );
David Tolnayc5629f02020-11-23 18:32:46 -0800273 writeln!(out, " return repr::PtrLen{{");
274 writeln!(
275 out,
276 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
277 );
278 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700279 writeln!(out, " }}");
280 }
281 writeln!(out, "}};");
282 }
283
David Tolnay3374d8d2020-10-31 22:18:45 -0700284 if builtin.rust_error {
285 out.next_section();
286 writeln!(out, "template <>");
287 writeln!(out, "class impl<Error> final {{");
288 writeln!(out, "public:");
289 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
290 writeln!(out, " Error error;");
291 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
292 writeln!(out, " error.len = repr.len;");
293 writeln!(out, " return error;");
294 writeln!(out, " }}");
295 writeln!(out, "}};");
296 }
297
David Tolnay53462762020-11-25 07:08:10 -0800298 if builtin.deleter_if {
299 out.next_section();
300 writeln!(out, "template <bool> struct deleter_if {{");
301 writeln!(out, " template <typename T> void operator()(T *) {{}}");
302 writeln!(out, "}};");
303 out.next_section();
304 writeln!(out, "template <> struct deleter_if<true> {{");
305 writeln!(
306 out,
307 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
308 );
309 writeln!(out, "}};");
310 }
311
David Tolnay0c033e32020-11-01 15:15:48 -0800312 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800313 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700314
315 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800316 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700317 include.exception = true;
318 include.type_traits = true;
319 include.utility = true;
320 writeln!(out, "class missing {{}};");
321 writeln!(out, "missing trycatch(...);");
322 writeln!(out);
323 writeln!(out, "template <typename Try, typename Fail>");
324 writeln!(out, "static typename ::std::enable_if<");
325 writeln!(
326 out,
327 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
328 );
329 writeln!(out, " missing>::value>::type");
330 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
331 writeln!(out, " func();");
332 writeln!(out, "}} catch (const ::std::exception &e) {{");
333 writeln!(out, " fail(e.what());");
334 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800335 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700336 }
337
David Tolnay0c033e32020-11-01 15:15:48 -0800338 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800339
340 if builtin.exception {
David Tolnay12320e12020-12-09 23:09:36 -0800341 include.cstddef = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800342 out.begin_block(Block::ExternC);
343 writeln!(
344 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800345 "const char *cxxbridge1$exception(const char *, ::std::size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800346 );
347 out.end_block(Block::ExternC);
348 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700349}