blob: b5c6e4dda73eb5dd51969a9183ae838c4e86d9eb [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
David Tolnay725bf502020-12-27 02:47:40 -080062 if builtin.rust_vec {
63 include.algorithm = true;
64 include.array = true;
65 include.cstddef = true;
66 include.initializer_list = true;
67 include.iterator = true;
68 include.new = true;
69 include.type_traits = true;
70 include.utility = true;
71 builtin.panic = true;
72 builtin.rust_slice = true;
73 builtin.unsafe_bitcopy = true;
74 }
75
David Tolnay22664ae2020-11-04 16:30:45 -080076 if builtin.rust_slice {
David Tolnay08679ae2020-12-12 22:10:01 -080077 include.cstddef = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080078 include.iterator = true;
David Tolnay450d8cd2020-11-25 20:36:06 -080079 include.type_traits = true;
David Tolnay22664ae2020-11-04 16:30:45 -080080 builtin.friend_impl = true;
David Tolnay6f92baa2020-12-27 01:09:26 -080081 builtin.layout = true;
David Tolnay78dd5352020-12-26 23:44:20 -080082 builtin.panic = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080083 }
84
85 if builtin.rust_box {
86 include.new = true;
87 include.type_traits = true;
David Tolnayea6f86c2020-11-10 15:08:57 -080088 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080089 }
90
David Tolnaya5a13012020-11-10 15:03:09 -080091 if builtin.rust_fn {
92 include.utility = true;
93 }
94
David Tolnaydcfa8e92020-11-02 09:50:06 -080095 if builtin.rust_error {
96 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080097 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080098 }
99
100 if builtin.rust_isize {
101 include.basetsd = true;
David Tolnay74dd66f2020-12-12 22:03:47 -0800102 include.sys_types = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -0800103 }
104
David Tolnay174bd952020-11-02 09:23:12 -0800105 if builtin.relocatable {
106 include.type_traits = true;
107 }
108
David Tolnayee6ecfc2020-12-26 21:54:37 -0800109 if builtin.layout {
110 include.type_traits = true;
111 include.cstddef = true;
112 builtin.is_complete = true;
113 }
114
David Tolnay75068632020-12-26 22:15:17 -0800115 if builtin.is_complete {
116 include.cstddef = true;
117 include.type_traits = true;
118 }
119
David Tolnay0c033e32020-11-01 15:15:48 -0800120 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -0800121 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700122 writeln!(out, "// #include \"rust/cxx.h\"");
123
David Tolnay0f0162f2020-11-16 23:43:37 -0800124 ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC");
David Tolnay3374d8d2020-10-31 22:18:45 -0700125
126 if builtin.rust_string {
127 out.next_section();
128 writeln!(out, "struct unsafe_bitcopy_t;");
129 }
130
David Tolnay22664ae2020-11-04 16:30:45 -0800131 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800132 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700133 writeln!(out, "template <typename T>");
134 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800135 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700136 }
137
David Tolnayee6ecfc2020-12-26 21:54:37 -0800138 out.next_section();
David Tolnay828e5132020-11-29 20:40:40 -0800139 if builtin.rust_str && !builtin.rust_string {
David Tolnay828e5132020-11-29 20:40:40 -0800140 writeln!(out, "class String;");
141 }
David Tolnayee6ecfc2020-12-26 21:54:37 -0800142 if builtin.layout && !builtin.opaque {
143 writeln!(out, "class Opaque;");
144 }
David Tolnay828e5132020-11-29 20:40:40 -0800145
David Tolnay6f92baa2020-12-27 01:09:26 -0800146 if builtin.rust_slice {
147 out.next_section();
148 writeln!(out, "template <typename T>");
149 writeln!(out, "::std::size_t size_of();");
David Tolnayeee622c2020-12-27 01:26:17 -0800150 writeln!(out, "template <typename T>");
151 writeln!(out, "::std::size_t align_of();");
David Tolnay6f92baa2020-12-27 01:09:26 -0800152 }
153
David Tolnay0f0162f2020-11-16 23:43:37 -0800154 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
155 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
156 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
157 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
158 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
159 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
160 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
161 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
162 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800163 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay75068632020-12-26 22:15:17 -0800164 ifndef::write(out, builtin.is_complete, "CXXBRIDGE1_IS_COMPLETE");
David Tolnayee6ecfc2020-12-26 21:54:37 -0800165 ifndef::write(out, builtin.layout, "CXXBRIDGE1_LAYOUT");
166 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700167
David Tolnay718ca0f2020-12-09 23:01:11 -0800168 out.begin_block(Block::Namespace("detail"));
169
170 if builtin.maybe_uninit {
171 include.cstddef = true;
172 include.new = true;
173 out.next_section();
174 writeln!(out, "template <typename T, typename = void *>");
175 writeln!(out, "struct operator_new {{");
176 writeln!(
177 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800178 " void *operator()(::std::size_t sz) {{ return ::operator new(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800179 );
180 writeln!(out, "}};");
181 out.next_section();
182 writeln!(out, "template <typename T>");
183 writeln!(
184 out,
185 "struct operator_new<T, decltype(T::operator new(sizeof(T)))> {{",
186 );
187 writeln!(
188 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800189 " void *operator()(::std::size_t sz) {{ return T::operator new(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800190 );
191 writeln!(out, "}};");
192 }
193
194 out.end_block(Block::Namespace("detail"));
195
David Tolnay3374d8d2020-10-31 22:18:45 -0700196 if builtin.manually_drop {
197 out.next_section();
198 include.utility = true;
199 writeln!(out, "template <typename T>");
200 writeln!(out, "union ManuallyDrop {{");
201 writeln!(out, " T value;");
202 writeln!(
203 out,
204 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
205 );
206 writeln!(out, " ~ManuallyDrop() {{}}");
207 writeln!(out, "}};");
208 }
209
210 if builtin.maybe_uninit {
David Tolnay718ca0f2020-12-09 23:01:11 -0800211 include.cstddef = true;
David Tolnay3374d8d2020-10-31 22:18:45 -0700212 out.next_section();
213 writeln!(out, "template <typename T>");
214 writeln!(out, "union MaybeUninit {{");
215 writeln!(out, " T value;");
David Tolnay718ca0f2020-12-09 23:01:11 -0800216 writeln!(
217 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800218 " void *operator new(::std::size_t sz) {{ return detail::operator_new<T>{{}}(sz); }}",
David Tolnay718ca0f2020-12-09 23:01:11 -0800219 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700220 writeln!(out, " MaybeUninit() {{}}");
221 writeln!(out, " ~MaybeUninit() {{}}");
222 writeln!(out, "}};");
223 }
224
David Tolnay0c033e32020-11-01 15:15:48 -0800225 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700226
David Tolnay919085c2020-10-31 22:32:22 -0700227 if builtin.ptr_len {
David Tolnay12320e12020-12-09 23:09:36 -0800228 include.cstddef = true;
David Tolnay0c033e32020-11-01 15:15:48 -0800229 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700230 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800231 writeln!(out, " void *ptr;");
David Tolnaybe3cbf72020-12-12 22:12:07 -0800232 writeln!(out, " ::std::size_t len;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700233 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800234 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700235 }
236
237 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
238 out.next_section();
239 writeln!(out, "template <>");
240 writeln!(out, "class impl<Str> final {{");
241 writeln!(out, "public:");
242 if builtin.rust_str_new_unchecked {
243 writeln!(
244 out,
245 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
246 );
247 writeln!(out, " Str str;");
248 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
249 writeln!(out, " str.len = repr.len;");
250 writeln!(out, " return str;");
251 writeln!(out, " }}");
252 }
253 if builtin.rust_str_repr {
254 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800255 writeln!(
256 out,
257 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
258 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700259 writeln!(out, " }}");
260 }
261 writeln!(out, "}};");
262 }
263
David Tolnay36aa9e02020-10-31 23:08:21 -0700264 if builtin.rust_slice_new || builtin.rust_slice_repr {
265 out.next_section();
266 writeln!(out, "template <typename T>");
267 writeln!(out, "class impl<Slice<T>> final {{");
268 writeln!(out, "public:");
269 if builtin.rust_slice_new {
270 writeln!(
271 out,
272 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
273 );
David Tolnayc5629f02020-11-23 18:32:46 -0800274 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700275 writeln!(out, " }}");
276 }
277 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800278 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700279 writeln!(
280 out,
281 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
282 );
David Tolnay5ce110e2020-12-27 02:45:53 -0800283 writeln!(out, " return repr::PtrLen{{slice.ptr, slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700284 writeln!(out, " }}");
285 }
286 writeln!(out, "}};");
287 }
288
David Tolnay3374d8d2020-10-31 22:18:45 -0700289 if builtin.rust_error {
290 out.next_section();
291 writeln!(out, "template <>");
292 writeln!(out, "class impl<Error> final {{");
293 writeln!(out, "public:");
294 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
295 writeln!(out, " Error error;");
296 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
297 writeln!(out, " error.len = repr.len;");
298 writeln!(out, " return error;");
299 writeln!(out, " }}");
300 writeln!(out, "}};");
301 }
302
David Tolnay53462762020-11-25 07:08:10 -0800303 if builtin.deleter_if {
304 out.next_section();
305 writeln!(out, "template <bool> struct deleter_if {{");
306 writeln!(out, " template <typename T> void operator()(T *) {{}}");
307 writeln!(out, "}};");
308 out.next_section();
309 writeln!(out, "template <> struct deleter_if<true> {{");
310 writeln!(
311 out,
312 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
313 );
314 writeln!(out, "}};");
315 }
316
David Tolnay0c033e32020-11-01 15:15:48 -0800317 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800318 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700319
320 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800321 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700322 include.exception = true;
323 include.type_traits = true;
324 include.utility = true;
325 writeln!(out, "class missing {{}};");
326 writeln!(out, "missing trycatch(...);");
327 writeln!(out);
328 writeln!(out, "template <typename Try, typename Fail>");
329 writeln!(out, "static typename ::std::enable_if<");
330 writeln!(
331 out,
332 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
333 );
334 writeln!(out, " missing>::value>::type");
335 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
336 writeln!(out, " func();");
337 writeln!(out, "}} catch (const ::std::exception &e) {{");
338 writeln!(out, " fail(e.what());");
339 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800340 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700341 }
342
David Tolnay0c033e32020-11-01 15:15:48 -0800343 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800344
345 if builtin.exception {
David Tolnay12320e12020-12-09 23:09:36 -0800346 include.cstddef = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800347 out.begin_block(Block::ExternC);
348 writeln!(
349 out,
David Tolnaybe3cbf72020-12-12 22:12:07 -0800350 "const char *cxxbridge1$exception(const char *, ::std::size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800351 );
352 out.end_block(Block::ExternC);
353 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700354}