blob: 8741e2d3d99011b97174cc56ad0e9e7ebd5f361e [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 Tolnay0f0162f2020-11-16 23:43:37 -0800118 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
119 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
120 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
121 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
122 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
123 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
124 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
125 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
126 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800127 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay0f0162f2020-11-16 23:43:37 -0800128 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700129
130 if builtin.manually_drop {
131 out.next_section();
132 include.utility = true;
133 writeln!(out, "template <typename T>");
134 writeln!(out, "union ManuallyDrop {{");
135 writeln!(out, " T value;");
136 writeln!(
137 out,
138 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
139 );
140 writeln!(out, " ~ManuallyDrop() {{}}");
141 writeln!(out, "}};");
142 }
143
144 if builtin.maybe_uninit {
145 out.next_section();
146 writeln!(out, "template <typename T>");
147 writeln!(out, "union MaybeUninit {{");
148 writeln!(out, " T value;");
149 writeln!(out, " MaybeUninit() {{}}");
150 writeln!(out, " ~MaybeUninit() {{}}");
151 writeln!(out, "}};");
152 }
153
David Tolnay0c033e32020-11-01 15:15:48 -0800154 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700155
David Tolnay919085c2020-10-31 22:32:22 -0700156 if builtin.ptr_len {
David Tolnay0c033e32020-11-01 15:15:48 -0800157 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700158 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800159 writeln!(out, " void *ptr;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700160 writeln!(out, " size_t len;");
161 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800162 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700163 }
164
165 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
166 out.next_section();
167 writeln!(out, "template <>");
168 writeln!(out, "class impl<Str> final {{");
169 writeln!(out, "public:");
170 if builtin.rust_str_new_unchecked {
171 writeln!(
172 out,
173 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
174 );
175 writeln!(out, " Str str;");
176 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
177 writeln!(out, " str.len = repr.len;");
178 writeln!(out, " return str;");
179 writeln!(out, " }}");
180 }
181 if builtin.rust_str_repr {
182 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800183 writeln!(
184 out,
185 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
186 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700187 writeln!(out, " }}");
188 }
189 writeln!(out, "}};");
190 }
191
David Tolnay36aa9e02020-10-31 23:08:21 -0700192 if builtin.rust_slice_new || builtin.rust_slice_repr {
193 out.next_section();
194 writeln!(out, "template <typename T>");
195 writeln!(out, "class impl<Slice<T>> final {{");
196 writeln!(out, "public:");
197 if builtin.rust_slice_new {
198 writeln!(
199 out,
200 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
201 );
David Tolnayc5629f02020-11-23 18:32:46 -0800202 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700203 writeln!(out, " }}");
204 }
205 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800206 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700207 writeln!(
208 out,
209 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
210 );
David Tolnayc5629f02020-11-23 18:32:46 -0800211 writeln!(out, " return repr::PtrLen{{");
212 writeln!(
213 out,
214 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
215 );
216 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700217 writeln!(out, " }}");
218 }
219 writeln!(out, "}};");
220 }
221
David Tolnay3374d8d2020-10-31 22:18:45 -0700222 if builtin.rust_error {
223 out.next_section();
224 writeln!(out, "template <>");
225 writeln!(out, "class impl<Error> final {{");
226 writeln!(out, "public:");
227 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
228 writeln!(out, " Error error;");
229 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
230 writeln!(out, " error.len = repr.len;");
231 writeln!(out, " return error;");
232 writeln!(out, " }}");
233 writeln!(out, "}};");
234 }
235
David Tolnay53462762020-11-25 07:08:10 -0800236 if builtin.is_complete {
237 include.type_traits = true;
238 out.next_section();
239 writeln!(out, "template <typename T, typename = size_t>");
240 writeln!(out, "struct is_complete : std::false_type {{}};");
241 out.next_section();
242 writeln!(out, "template <typename T>");
243 writeln!(
244 out,
245 "struct is_complete<T, decltype(sizeof(T))> : std::true_type {{}};",
246 );
247 }
248
249 if builtin.deleter_if {
250 out.next_section();
251 writeln!(out, "template <bool> struct deleter_if {{");
252 writeln!(out, " template <typename T> void operator()(T *) {{}}");
253 writeln!(out, "}};");
254 out.next_section();
255 writeln!(out, "template <> struct deleter_if<true> {{");
256 writeln!(
257 out,
258 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
259 );
260 writeln!(out, "}};");
261 }
262
David Tolnay0c033e32020-11-01 15:15:48 -0800263 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800264 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700265
266 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800267 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700268 include.exception = true;
269 include.type_traits = true;
270 include.utility = true;
271 writeln!(out, "class missing {{}};");
272 writeln!(out, "missing trycatch(...);");
273 writeln!(out);
274 writeln!(out, "template <typename Try, typename Fail>");
275 writeln!(out, "static typename ::std::enable_if<");
276 writeln!(
277 out,
278 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
279 );
280 writeln!(out, " missing>::value>::type");
281 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
282 writeln!(out, " func();");
283 writeln!(out, "}} catch (const ::std::exception &e) {{");
284 writeln!(out, " fail(e.what());");
285 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800286 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700287 }
288
David Tolnay0c033e32020-11-01 15:15:48 -0800289 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800290
291 if builtin.exception {
292 out.begin_block(Block::ExternC);
293 writeln!(
294 out,
David Tolnay0f0162f2020-11-16 23:43:37 -0800295 "const char *cxxbridge1$exception(const char *, size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800296 );
297 out.end_block(Block::ExternC);
298 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700299}