blob: 5182e0185add6e4b85f47782dcaaef16fcd7cfdb [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 {
74 include.array = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080075 include.iterator = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080076 include.new = true;
77 include.type_traits = true;
David Tolnayfb6b73c2020-11-10 14:32:16 -080078 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080079 builtin.panic = true;
80 builtin.unsafe_bitcopy = true;
81 }
82
David Tolnaya5a13012020-11-10 15:03:09 -080083 if builtin.rust_fn {
84 include.utility = true;
85 }
86
David Tolnaydcfa8e92020-11-02 09:50:06 -080087 if builtin.rust_error {
88 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080089 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080090 }
91
92 if builtin.rust_isize {
93 include.basetsd = true;
94 }
95
David Tolnay174bd952020-11-02 09:23:12 -080096 if builtin.relocatable {
97 include.type_traits = true;
98 }
99
David Tolnay0c033e32020-11-01 15:15:48 -0800100 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -0800101 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700102 writeln!(out, "// #include \"rust/cxx.h\"");
103
David Tolnay0f0162f2020-11-16 23:43:37 -0800104 ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC");
David Tolnay3374d8d2020-10-31 22:18:45 -0700105
106 if builtin.rust_string {
107 out.next_section();
108 writeln!(out, "struct unsafe_bitcopy_t;");
109 }
110
David Tolnay22664ae2020-11-04 16:30:45 -0800111 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800112 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700113 writeln!(out, "template <typename T>");
114 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800115 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700116 }
117
David Tolnay828e5132020-11-29 20:40:40 -0800118 if builtin.rust_str && !builtin.rust_string {
119 out.next_section();
120 writeln!(out, "class String;");
121 }
122
David Tolnay0f0162f2020-11-16 23:43:37 -0800123 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
124 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
125 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
126 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
127 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
128 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
129 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
130 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
131 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800132 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay0f0162f2020-11-16 23:43:37 -0800133 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700134
135 if builtin.manually_drop {
136 out.next_section();
137 include.utility = true;
138 writeln!(out, "template <typename T>");
139 writeln!(out, "union ManuallyDrop {{");
140 writeln!(out, " T value;");
141 writeln!(
142 out,
143 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
144 );
145 writeln!(out, " ~ManuallyDrop() {{}}");
146 writeln!(out, "}};");
147 }
148
149 if builtin.maybe_uninit {
150 out.next_section();
151 writeln!(out, "template <typename T>");
152 writeln!(out, "union MaybeUninit {{");
153 writeln!(out, " T value;");
154 writeln!(out, " MaybeUninit() {{}}");
155 writeln!(out, " ~MaybeUninit() {{}}");
156 writeln!(out, "}};");
157 }
158
David Tolnay0c033e32020-11-01 15:15:48 -0800159 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700160
David Tolnay919085c2020-10-31 22:32:22 -0700161 if builtin.ptr_len {
David Tolnay0c033e32020-11-01 15:15:48 -0800162 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700163 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800164 writeln!(out, " void *ptr;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700165 writeln!(out, " size_t len;");
166 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800167 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700168 }
169
170 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
171 out.next_section();
172 writeln!(out, "template <>");
173 writeln!(out, "class impl<Str> final {{");
174 writeln!(out, "public:");
175 if builtin.rust_str_new_unchecked {
176 writeln!(
177 out,
178 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
179 );
180 writeln!(out, " Str str;");
181 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
182 writeln!(out, " str.len = repr.len;");
183 writeln!(out, " return str;");
184 writeln!(out, " }}");
185 }
186 if builtin.rust_str_repr {
187 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800188 writeln!(
189 out,
190 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
191 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700192 writeln!(out, " }}");
193 }
194 writeln!(out, "}};");
195 }
196
David Tolnay36aa9e02020-10-31 23:08:21 -0700197 if builtin.rust_slice_new || builtin.rust_slice_repr {
198 out.next_section();
199 writeln!(out, "template <typename T>");
200 writeln!(out, "class impl<Slice<T>> final {{");
201 writeln!(out, "public:");
202 if builtin.rust_slice_new {
203 writeln!(
204 out,
205 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
206 );
David Tolnayc5629f02020-11-23 18:32:46 -0800207 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700208 writeln!(out, " }}");
209 }
210 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800211 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700212 writeln!(
213 out,
214 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
215 );
David Tolnayc5629f02020-11-23 18:32:46 -0800216 writeln!(out, " return repr::PtrLen{{");
217 writeln!(
218 out,
219 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
220 );
221 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700222 writeln!(out, " }}");
223 }
224 writeln!(out, "}};");
225 }
226
David Tolnay3374d8d2020-10-31 22:18:45 -0700227 if builtin.rust_error {
228 out.next_section();
229 writeln!(out, "template <>");
230 writeln!(out, "class impl<Error> final {{");
231 writeln!(out, "public:");
232 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
233 writeln!(out, " Error error;");
234 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
235 writeln!(out, " error.len = repr.len;");
236 writeln!(out, " return error;");
237 writeln!(out, " }}");
238 writeln!(out, "}};");
239 }
240
David Tolnay53462762020-11-25 07:08:10 -0800241 if builtin.is_complete {
242 include.type_traits = true;
243 out.next_section();
244 writeln!(out, "template <typename T, typename = size_t>");
245 writeln!(out, "struct is_complete : std::false_type {{}};");
246 out.next_section();
247 writeln!(out, "template <typename T>");
248 writeln!(
249 out,
250 "struct is_complete<T, decltype(sizeof(T))> : std::true_type {{}};",
251 );
252 }
253
254 if builtin.deleter_if {
255 out.next_section();
256 writeln!(out, "template <bool> struct deleter_if {{");
257 writeln!(out, " template <typename T> void operator()(T *) {{}}");
258 writeln!(out, "}};");
259 out.next_section();
260 writeln!(out, "template <> struct deleter_if<true> {{");
261 writeln!(
262 out,
263 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
264 );
265 writeln!(out, "}};");
266 }
267
David Tolnay0c033e32020-11-01 15:15:48 -0800268 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800269 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700270
271 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800272 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700273 include.exception = true;
274 include.type_traits = true;
275 include.utility = true;
276 writeln!(out, "class missing {{}};");
277 writeln!(out, "missing trycatch(...);");
278 writeln!(out);
279 writeln!(out, "template <typename Try, typename Fail>");
280 writeln!(out, "static typename ::std::enable_if<");
281 writeln!(
282 out,
283 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
284 );
285 writeln!(out, " missing>::value>::type");
286 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
287 writeln!(out, " func();");
288 writeln!(out, "}} catch (const ::std::exception &e) {{");
289 writeln!(out, " fail(e.what());");
290 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800291 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700292 }
293
David Tolnay0c033e32020-11-01 15:15:48 -0800294 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800295
296 if builtin.exception {
297 out.begin_block(Block::ExternC);
298 writeln!(
299 out,
David Tolnay0f0162f2020-11-16 23:43:37 -0800300 "const char *cxxbridge1$exception(const char *, size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800301 );
302 out.end_block(Block::ExternC);
303 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700304}