blob: 837761d12e3f32ff08494f947fa5b0aa10d205fb [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 {
David Tolnayf799b372020-11-29 23:57:42 -080074 include.algorithm = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080075 include.array = true;
David Tolnayf799b372020-11-29 23:57:42 -080076 include.initializer_list = true;
David Tolnayd1df4c72020-11-25 20:38:05 -080077 include.iterator = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080078 include.new = true;
79 include.type_traits = true;
David Tolnayfb6b73c2020-11-10 14:32:16 -080080 include.utility = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080081 builtin.panic = true;
82 builtin.unsafe_bitcopy = true;
83 }
84
David Tolnaya5a13012020-11-10 15:03:09 -080085 if builtin.rust_fn {
86 include.utility = true;
87 }
88
David Tolnaydcfa8e92020-11-02 09:50:06 -080089 if builtin.rust_error {
90 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080091 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080092 }
93
94 if builtin.rust_isize {
95 include.basetsd = true;
96 }
97
David Tolnay174bd952020-11-02 09:23:12 -080098 if builtin.relocatable {
99 include.type_traits = true;
100 }
101
David Tolnay0c033e32020-11-01 15:15:48 -0800102 out.begin_block(Block::Namespace("rust"));
David Tolnay0f0162f2020-11-16 23:43:37 -0800103 out.begin_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700104 writeln!(out, "// #include \"rust/cxx.h\"");
105
David Tolnay0f0162f2020-11-16 23:43:37 -0800106 ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC");
David Tolnay3374d8d2020-10-31 22:18:45 -0700107
108 if builtin.rust_string {
109 out.next_section();
110 writeln!(out, "struct unsafe_bitcopy_t;");
111 }
112
David Tolnay22664ae2020-11-04 16:30:45 -0800113 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800114 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700115 writeln!(out, "template <typename T>");
116 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800117 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700118 }
119
David Tolnay828e5132020-11-29 20:40:40 -0800120 if builtin.rust_str && !builtin.rust_string {
121 out.next_section();
122 writeln!(out, "class String;");
123 }
124
David Tolnay0f0162f2020-11-16 23:43:37 -0800125 ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");
126 ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR");
127 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE");
128 ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX");
129 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY");
130 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC");
131 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN");
132 ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR");
133 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE");
David Tolnay365fc7c2020-11-25 16:08:13 -0800134 ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE");
David Tolnay0f0162f2020-11-16 23:43:37 -0800135 ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700136
137 if builtin.manually_drop {
138 out.next_section();
139 include.utility = true;
140 writeln!(out, "template <typename T>");
141 writeln!(out, "union ManuallyDrop {{");
142 writeln!(out, " T value;");
143 writeln!(
144 out,
145 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
146 );
147 writeln!(out, " ~ManuallyDrop() {{}}");
148 writeln!(out, "}};");
149 }
150
151 if builtin.maybe_uninit {
152 out.next_section();
153 writeln!(out, "template <typename T>");
154 writeln!(out, "union MaybeUninit {{");
155 writeln!(out, " T value;");
156 writeln!(out, " MaybeUninit() {{}}");
157 writeln!(out, " ~MaybeUninit() {{}}");
158 writeln!(out, "}};");
159 }
160
David Tolnay0c033e32020-11-01 15:15:48 -0800161 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700162
David Tolnay919085c2020-10-31 22:32:22 -0700163 if builtin.ptr_len {
David Tolnay0c033e32020-11-01 15:15:48 -0800164 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700165 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800166 writeln!(out, " void *ptr;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700167 writeln!(out, " size_t len;");
168 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800169 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700170 }
171
172 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
173 out.next_section();
174 writeln!(out, "template <>");
175 writeln!(out, "class impl<Str> final {{");
176 writeln!(out, "public:");
177 if builtin.rust_str_new_unchecked {
178 writeln!(
179 out,
180 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
181 );
182 writeln!(out, " Str str;");
183 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
184 writeln!(out, " str.len = repr.len;");
185 writeln!(out, " return str;");
186 writeln!(out, " }}");
187 }
188 if builtin.rust_str_repr {
189 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800190 writeln!(
191 out,
192 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
193 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700194 writeln!(out, " }}");
195 }
196 writeln!(out, "}};");
197 }
198
David Tolnay36aa9e02020-10-31 23:08:21 -0700199 if builtin.rust_slice_new || builtin.rust_slice_repr {
200 out.next_section();
201 writeln!(out, "template <typename T>");
202 writeln!(out, "class impl<Slice<T>> final {{");
203 writeln!(out, "public:");
204 if builtin.rust_slice_new {
205 writeln!(
206 out,
207 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
208 );
David Tolnayc5629f02020-11-23 18:32:46 -0800209 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700210 writeln!(out, " }}");
211 }
212 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800213 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700214 writeln!(
215 out,
216 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
217 );
David Tolnayc5629f02020-11-23 18:32:46 -0800218 writeln!(out, " return repr::PtrLen{{");
219 writeln!(
220 out,
221 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
222 );
223 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700224 writeln!(out, " }}");
225 }
226 writeln!(out, "}};");
227 }
228
David Tolnay3374d8d2020-10-31 22:18:45 -0700229 if builtin.rust_error {
230 out.next_section();
231 writeln!(out, "template <>");
232 writeln!(out, "class impl<Error> final {{");
233 writeln!(out, "public:");
234 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
235 writeln!(out, " Error error;");
236 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
237 writeln!(out, " error.len = repr.len;");
238 writeln!(out, " return error;");
239 writeln!(out, " }}");
240 writeln!(out, "}};");
241 }
242
David Tolnay53462762020-11-25 07:08:10 -0800243 if builtin.is_complete {
244 include.type_traits = true;
245 out.next_section();
246 writeln!(out, "template <typename T, typename = size_t>");
247 writeln!(out, "struct is_complete : std::false_type {{}};");
248 out.next_section();
249 writeln!(out, "template <typename T>");
250 writeln!(
251 out,
252 "struct is_complete<T, decltype(sizeof(T))> : std::true_type {{}};",
253 );
254 }
255
256 if builtin.deleter_if {
257 out.next_section();
258 writeln!(out, "template <bool> struct deleter_if {{");
259 writeln!(out, " template <typename T> void operator()(T *) {{}}");
260 writeln!(out, "}};");
261 out.next_section();
262 writeln!(out, "template <> struct deleter_if<true> {{");
263 writeln!(
264 out,
265 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
266 );
267 writeln!(out, "}};");
268 }
269
David Tolnay0c033e32020-11-01 15:15:48 -0800270 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800271 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700272
273 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800274 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700275 include.exception = true;
276 include.type_traits = true;
277 include.utility = true;
278 writeln!(out, "class missing {{}};");
279 writeln!(out, "missing trycatch(...);");
280 writeln!(out);
281 writeln!(out, "template <typename Try, typename Fail>");
282 writeln!(out, "static typename ::std::enable_if<");
283 writeln!(
284 out,
285 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
286 );
287 writeln!(out, " missing>::value>::type");
288 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
289 writeln!(out, " func();");
290 writeln!(out, "}} catch (const ::std::exception &e) {{");
291 writeln!(out, " fail(e.what());");
292 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800293 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700294 }
295
David Tolnay0c033e32020-11-01 15:15:48 -0800296 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800297
298 if builtin.exception {
299 out.begin_block(Block::ExternC);
300 writeln!(
301 out,
David Tolnay0f0162f2020-11-16 23:43:37 -0800302 "const char *cxxbridge1$exception(const char *, size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800303 );
304 out.end_block(Block::ExternC);
305 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700306}