blob: 62c352717ffc4baa67ae215e35dbaaafcd7d35ee [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 Tolnay12320e12020-12-09 23:09:36 -0800164 include.cstddef = true;
David Tolnay0c033e32020-11-01 15:15:48 -0800165 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700166 writeln!(out, "struct PtrLen final {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800167 writeln!(out, " void *ptr;");
David Tolnay3374d8d2020-10-31 22:18:45 -0700168 writeln!(out, " size_t len;");
169 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800170 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700171 }
172
173 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
174 out.next_section();
175 writeln!(out, "template <>");
176 writeln!(out, "class impl<Str> final {{");
177 writeln!(out, "public:");
178 if builtin.rust_str_new_unchecked {
179 writeln!(
180 out,
181 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
182 );
183 writeln!(out, " Str str;");
184 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
185 writeln!(out, " str.len = repr.len;");
186 writeln!(out, " return str;");
187 writeln!(out, " }}");
188 }
189 if builtin.rust_str_repr {
190 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
David Tolnayc5629f02020-11-23 18:32:46 -0800191 writeln!(
192 out,
193 " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
194 );
David Tolnay3374d8d2020-10-31 22:18:45 -0700195 writeln!(out, " }}");
196 }
197 writeln!(out, "}};");
198 }
199
David Tolnay36aa9e02020-10-31 23:08:21 -0700200 if builtin.rust_slice_new || builtin.rust_slice_repr {
201 out.next_section();
202 writeln!(out, "template <typename T>");
203 writeln!(out, "class impl<Slice<T>> final {{");
204 writeln!(out, "public:");
205 if builtin.rust_slice_new {
206 writeln!(
207 out,
208 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
209 );
David Tolnayc5629f02020-11-23 18:32:46 -0800210 writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700211 writeln!(out, " }}");
212 }
213 if builtin.rust_slice_repr {
David Tolnayc5629f02020-11-23 18:32:46 -0800214 include.type_traits = true;
David Tolnay36aa9e02020-10-31 23:08:21 -0700215 writeln!(
216 out,
217 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
218 );
David Tolnayc5629f02020-11-23 18:32:46 -0800219 writeln!(out, " return repr::PtrLen{{");
220 writeln!(
221 out,
222 " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
223 );
224 writeln!(out, " slice.len}};");
David Tolnay36aa9e02020-10-31 23:08:21 -0700225 writeln!(out, " }}");
226 }
227 writeln!(out, "}};");
228 }
229
David Tolnay3374d8d2020-10-31 22:18:45 -0700230 if builtin.rust_error {
231 out.next_section();
232 writeln!(out, "template <>");
233 writeln!(out, "class impl<Error> final {{");
234 writeln!(out, "public:");
235 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
236 writeln!(out, " Error error;");
237 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
238 writeln!(out, " error.len = repr.len;");
239 writeln!(out, " return error;");
240 writeln!(out, " }}");
241 writeln!(out, "}};");
242 }
243
David Tolnay53462762020-11-25 07:08:10 -0800244 if builtin.is_complete {
David Tolnay12320e12020-12-09 23:09:36 -0800245 include.cstddef = true;
David Tolnay53462762020-11-25 07:08:10 -0800246 include.type_traits = true;
247 out.next_section();
248 writeln!(out, "template <typename T, typename = size_t>");
249 writeln!(out, "struct is_complete : std::false_type {{}};");
250 out.next_section();
251 writeln!(out, "template <typename T>");
252 writeln!(
253 out,
254 "struct is_complete<T, decltype(sizeof(T))> : std::true_type {{}};",
255 );
256 }
257
258 if builtin.deleter_if {
259 out.next_section();
260 writeln!(out, "template <bool> struct deleter_if {{");
261 writeln!(out, " template <typename T> void operator()(T *) {{}}");
262 writeln!(out, "}};");
263 out.next_section();
264 writeln!(out, "template <> struct deleter_if<true> {{");
265 writeln!(
266 out,
267 " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}",
268 );
269 writeln!(out, "}};");
270 }
271
David Tolnay0c033e32020-11-01 15:15:48 -0800272 out.end_block(Block::AnonymousNamespace);
David Tolnay0f0162f2020-11-16 23:43:37 -0800273 out.end_block(Block::InlineNamespace("cxxbridge1"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700274
275 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800276 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700277 include.exception = true;
278 include.type_traits = true;
279 include.utility = true;
280 writeln!(out, "class missing {{}};");
281 writeln!(out, "missing trycatch(...);");
282 writeln!(out);
283 writeln!(out, "template <typename Try, typename Fail>");
284 writeln!(out, "static typename ::std::enable_if<");
285 writeln!(
286 out,
287 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
288 );
289 writeln!(out, " missing>::value>::type");
290 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
291 writeln!(out, " func();");
292 writeln!(out, "}} catch (const ::std::exception &e) {{");
293 writeln!(out, " fail(e.what());");
294 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800295 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700296 }
297
David Tolnay0c033e32020-11-01 15:15:48 -0800298 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800299
300 if builtin.exception {
David Tolnay12320e12020-12-09 23:09:36 -0800301 include.cstddef = true;
David Tolnay1f010c62020-11-01 20:27:46 -0800302 out.begin_block(Block::ExternC);
303 writeln!(
304 out,
David Tolnay0f0162f2020-11-16 23:43:37 -0800305 "const char *cxxbridge1$exception(const char *, size_t);",
David Tolnay1f010c62020-11-01 20:27:46 -0800306 );
307 out.end_block(Block::ExternC);
308 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700309}