blob: 8ce89f805ed54191d65c10b3814d952f39979ab8 [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 Tolnaydcfa8e92020-11-02 09:50:06 -080067 }
68
69 if builtin.rust_box {
70 include.new = true;
71 include.type_traits = true;
David Tolnayea6f86c2020-11-10 15:08:57 -080072 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080073 }
74
75 if builtin.rust_vec {
David Tolnayf799b372020-11-29 23:57:42 -080076 include.algorithm = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080077 include.array = true;
David Tolnay08679ae2020-12-12 22:10:01 -080078 include.cstddef = true;
David Tolnayf799b372020-11-29 23:57:42 -080079 include.initializer_list = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080080 include.iterator = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080081 include.new = true;
82 include.type_traits = true;
David Tolnayfb6b73c2020-11-10 14:32:16 -080083 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080084 builtin.panic = true;
85 builtin.unsafe_bitcopy = true;
86 }
87
David Tolnaya5a13012020-11-10 15:03:09 -080088 if builtin.rust_fn {
89 include.utility = true;
90 }
91
David Tolnaydcfa8e92020-11-02 09:50:06 -080092 if builtin.rust_error {
93 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080094 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080095 }
96
97 if builtin.rust_isize {
98 include.basetsd = true;
David Tolnay74dd66f2020-12-12 22:03:47 -080099 include.sys_types = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -0800100 }
101
David Tolnay174bd952020-11-02 09:23:12 -0800102 if builtin.relocatable {
103 include.type_traits = true;
104 }
105
David Tolnayee6ecfc2020-12-26 21:54:37 -0800106 if builtin.layout {
107 include.type_traits = true;
108 include.cstddef = true;
109 builtin.is_complete = true;
110 }
111
David Tolnay75068632020-12-26 22:15:17 -0800112 if builtin.is_complete {
113 include.cstddef = true;
114 include.type_traits = true;
115 }
116
David Tolnay0c033e32020-11-01 15:15:48 -0800117 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -0800118 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700119 writeln!(out, "// #include \"rust/cxx.h\"");
120
David Tolnay0f0162f2020-11-16 23:43:37 -0800121 ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC");
David Tolnay3374d8d2020-10-31 22:18:45 -0700122
123 if builtin.rust_string {
124 out.next_section();
125 writeln!(out, "struct unsafe_bitcopy_t;");
126 }
127
David Tolnay22664ae2020-11-04 16:30:45 -0800128 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800129 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700130 writeln!(out, "template <typename T>");
131 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800132 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700133 }
134
David Tolnayee6ecfc2020-12-26 21:54:37 -0800135 out.next_section();
David Tolnay828e5132020-11-29 20:40:40 -0800136 if builtin.rust_str && !builtin.rust_string {
David Tolnay828e5132020-11-29 20:40:40 -0800137 writeln!(out, "class String;");
138 }
David Tolnayee6ecfc2020-12-26 21:54:37 -0800139 if builtin.layout && !builtin.opaque {
140 writeln!(out, "class Opaque;");
141 }
David Tolnay828e5132020-11-29 20:40:40 -0800142
David Tolnay0f0162f2020-11-16 23:43:37 -0800143 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
144 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
145 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
146 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
147 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
148 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
149 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
150 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
151 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800152 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay75068632020-12-26 22:15:17 -0800153 ifndef::write(out, builtin.is_complete, "CXXBRIDGE1_IS_COMPLETE");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800154 ifndef::write(out, builtin.layout, "CXXBRIDGE1_LAYOUT");
155 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700156
David Tolnay718ca0f2020-12-09 23:01:11 -0800157 out.begin_block(Block::Namespace("detail"));
158
159 if builtin.maybe_uninit {
160 include.cstddef = true;
161 include.new = true;
162 out.next_section();
163 writeln!(out, "template <typename T, typename = void *>");
164 writeln!(out, "struct operator_new {{");
165 writeln!(
166 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800167 " void *operator()(::std::size_t sz) {{ return ::operator new(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800168 );
169 writeln!(out, "}};");
170 out.next_section();
171 writeln!(out, "template <typename T>");
172 writeln!(
173 out,
174 "struct operator_new<T, decltype(T::operator new(sizeof(T)))> {{",
175 );
176 writeln!(
177 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800178 " void *operator()(::std::size_t sz) {{ return T::operator new(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800179 );
180 writeln!(out, "}};");
181 }
182
183 out.end_block(Block::Namespace("detail"));
184
David Tolnay3374d8d2020-10-31 22:18:45 -0700185 if builtin.manually_drop {
186 out.next_section();
187 include.utility = true;
188 writeln!(out, "template <typename T>");
189 writeln!(out, "union ManuallyDrop {{");
190 writeln!(out, " T value;");
191 writeln!(
192 out,
193 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
194 );
195 writeln!(out, " ~ManuallyDrop() {{}}");
196 writeln!(out, "}};");
197 }
198
199 if builtin.maybe_uninit {
David Tolnay718ca0f2020-12-09 23:01:11 -0800200 include.cstddef = true;
David Tolnay3374d8d2020-10-31 22:18:45 -0700201 out.next_section();
202 writeln!(out, "template <typename T>");
203 writeln!(out, "union MaybeUninit {{");
204 writeln!(out, " T value;");
David Tolnay718ca0f2020-12-09 23:01:11 -0800205 writeln!(
206 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800207 " void *operator new(::std::size_t sz) {{ return detail::operator_new<T>{{}}(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800208 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700209 writeln!(out, " MaybeUninit() {{}}");
210 writeln!(out, " ~MaybeUninit() {{}}");
211 writeln!(out, "}};");
212 }
213
David Tolnay0c033e32020-11-01 15:15:48 -0800214 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700215
David Tolnay919085c2020-10-31 22:32:22 -0700216 if builtin.ptr_len {
David Tolnay12320e12020-12-09 23:09:36 -0800217 include.cstddef = true;
David Tolnay0c033e32020-11-01 15:15:48 -0800218 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700219 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800220 writeln!(out, " void *ptr;");
David Tolnaybe3cbf72020-12-12 22:12:07 -0800221 writeln!(out, " ::std::size_t len;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700222 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800223 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700224 }
225
226 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
227 out.next_section();
228 writeln!(out, "template <>");
229 writeln!(out, "class impl<Str> final {{");
230 writeln!(out, "public:");
231 if builtin.rust_str_new_unchecked {
232 writeln!(
233 out,
234 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
235 );
236 writeln!(out, " Str str;");
237 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
238 writeln!(out, " str.len = repr.len;");
239 writeln!(out, " return str;");
240 writeln!(out, " }}");
241 }
242 if builtin.rust_str_repr {
243 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800244 writeln!(
245 out,
246 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
247 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700248 writeln!(out, " }}");
249 }
250 writeln!(out, "}};");
251 }
252
David Tolnay36aa9e02020-10-31 23:08:21 -0700253 if builtin.rust_slice_new || builtin.rust_slice_repr {
254 out.next_section();
255 writeln!(out, "template <typename T>");
256 writeln!(out, "class impl<Slice<T>> final {{");
257 writeln!(out, "public:");
258 if builtin.rust_slice_new {
259 writeln!(
260 out,
261 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
262 );
David Tolnayc5629f02020-11-23 18:32:46 -0800263 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700264 writeln!(out, " }}");
265 }
266 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800267 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700268 writeln!(
269 out,
270 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
271 );
David Tolnayc5629f02020-11-23 18:32:46 -0800272 writeln!(out, " return repr::PtrLen{{");
273 writeln!(
274 out,
275 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
276 );
277 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700278 writeln!(out, " }}");
279 }
280 writeln!(out, "}};");
281 }
282
David Tolnay3374d8d2020-10-31 22:18:45 -0700283 if builtin.rust_error {
284 out.next_section();
285 writeln!(out, "template <>");
286 writeln!(out, "class impl<Error> final {{");
287 writeln!(out, "public:");
288 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
289 writeln!(out, " Error error;");
290 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
291 writeln!(out, " error.len = repr.len;");
292 writeln!(out, " return error;");
293 writeln!(out, " }}");
294 writeln!(out, "}};");
295 }
296
David Tolnay53462762020-11-25 07:08:10 -0800297 if builtin.deleter_if {
298 out.next_section();
299 writeln!(out, "template <bool> struct deleter_if {{");
300 writeln!(out, " template <typename T> void operator()(T *) {{}}");
301 writeln!(out, "}};");
302 out.next_section();
303 writeln!(out, "template <> struct deleter_if<true> {{");
304 writeln!(
305 out,
306 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
307 );
308 writeln!(out, "}};");
309 }
310
David Tolnay0c033e32020-11-01 15:15:48 -0800311 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800312 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700313
314 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800315 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700316 include.exception = true;
317 include.type_traits = true;
318 include.utility = true;
319 writeln!(out, "class missing {{}};");
320 writeln!(out, "missing trycatch(...);");
321 writeln!(out);
322 writeln!(out, "template <typename Try, typename Fail>");
323 writeln!(out, "static typename ::std::enable_if<");
324 writeln!(
325 out,
326 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
327 );
328 writeln!(out, " missing>::value>::type");
329 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
330 writeln!(out, " func();");
331 writeln!(out, "}} catch (const ::std::exception &e) {{");
332 writeln!(out, " fail(e.what());");
333 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800334 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700335 }
336
David Tolnay0c033e32020-11-01 15:15:48 -0800337 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800338
339 if builtin.exception {
David Tolnay12320e12020-12-09 23:09:36 -0800340 include.cstddef = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800341 out.begin_block(Block::ExternC);
342 writeln!(
343 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800344 "const char *cxxbridge1$exception(const char *, ::std::size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800345 );
346 out.end_block(Block::ExternC);
347 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700348}