| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 1 | use crate::gen::block::Block; |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 2 | use crate::gen::ifndef; |
| 3 | use crate::gen::out::{Content, OutFile}; |
| David Tolnay | 8c14d9a | 2020-10-31 21:52:38 -0700 | [diff] [blame] | 4 | |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 5 | #[derive(Default, PartialEq)] |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 6 | pub struct Builtins<'a> { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 7 | 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 Tolnay | 365fc7c | 2020-11-25 16:08:13 -0800 | [diff] [blame] | 15 | pub opaque: bool, |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 16 | pub unsafe_bitcopy: bool, |
| 17 | pub rust_error: bool, |
| 18 | pub manually_drop: bool, |
| 19 | pub maybe_uninit: bool, |
| 20 | pub trycatch: bool, |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 21 | pub ptr_len: bool, |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 22 | pub rust_str_new_unchecked: bool, |
| 23 | pub rust_str_repr: bool, |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 24 | pub rust_slice_new: bool, |
| 25 | pub rust_slice_repr: bool, |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 26 | pub exception: bool, |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 27 | pub relocatable: bool, |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 28 | pub friend_impl: bool, |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 29 | pub is_complete: bool, |
| 30 | pub deleter_if: bool, |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 31 | pub content: Content<'a>, |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 34 | impl<'a> Builtins<'a> { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 35 | pub fn new() -> Self { |
| 36 | Builtins::default() |
| 37 | } |
| 38 | } |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 39 | |
| 40 | pub(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 Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 49 | 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 Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 58 | builtin.friend_impl = true; |
| 59 | } |
| 60 | |
| 61 | if builtin.rust_slice { |
| David Tolnay | d1df4c7 | 2020-11-25 20:38:05 -0800 | [diff] [blame] | 62 | include.iterator = true; |
| David Tolnay | 450d8cd | 2020-11-25 20:36:06 -0800 | [diff] [blame] | 63 | include.type_traits = true; |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 64 | builtin.friend_impl = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if builtin.rust_box { |
| 68 | include.new = true; |
| 69 | include.type_traits = true; |
| David Tolnay | ea6f86c | 2020-11-10 15:08:57 -0800 | [diff] [blame] | 70 | include.utility = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if builtin.rust_vec { |
| David Tolnay | f799b37 | 2020-11-29 23:57:42 -0800 | [diff] [blame] | 74 | include.algorithm = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 75 | include.array = true; |
| David Tolnay | f799b37 | 2020-11-29 23:57:42 -0800 | [diff] [blame] | 76 | include.initializer_list = true; |
| David Tolnay | d1df4c7 | 2020-11-25 20:38:05 -0800 | [diff] [blame] | 77 | include.iterator = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 78 | include.new = true; |
| 79 | include.type_traits = true; |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 80 | include.utility = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 81 | builtin.panic = true; |
| 82 | builtin.unsafe_bitcopy = true; |
| 83 | } |
| 84 | |
| David Tolnay | a5a1301 | 2020-11-10 15:03:09 -0800 | [diff] [blame] | 85 | if builtin.rust_fn { |
| 86 | include.utility = true; |
| 87 | } |
| 88 | |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 89 | if builtin.rust_error { |
| 90 | include.exception = true; |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 91 | builtin.friend_impl = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if builtin.rust_isize { |
| 95 | include.basetsd = true; |
| 96 | } |
| 97 | |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 98 | if builtin.relocatable { |
| 99 | include.type_traits = true; |
| 100 | } |
| 101 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 102 | out.begin_block(Block::Namespace("rust")); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 103 | out.begin_block(Block::InlineNamespace("cxxbridge1")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 104 | writeln!(out, "// #include \"rust/cxx.h\""); |
| 105 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 106 | ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC"); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 107 | |
| 108 | if builtin.rust_string { |
| 109 | out.next_section(); |
| 110 | writeln!(out, "struct unsafe_bitcopy_t;"); |
| 111 | } |
| 112 | |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 113 | if builtin.friend_impl { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 114 | out.begin_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 115 | writeln!(out, "template <typename T>"); |
| 116 | writeln!(out, "class impl;"); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 117 | out.end_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| David Tolnay | 828e513 | 2020-11-29 20:40:40 -0800 | [diff] [blame] | 120 | if builtin.rust_str && !builtin.rust_string { |
| 121 | out.next_section(); |
| 122 | writeln!(out, "class String;"); |
| 123 | } |
| 124 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 125 | 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 Tolnay | 365fc7c | 2020-11-25 16:08:13 -0800 | [diff] [blame] | 134 | ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE"); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 135 | ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE"); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 136 | |
| 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 Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 161 | out.begin_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 162 | |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 163 | if builtin.ptr_len { |
| David Tolnay | 12320e1 | 2020-12-09 23:09:36 -0800 | [diff] [blame^] | 164 | include.cstddef = true; |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 165 | out.begin_block(Block::Namespace("repr")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 166 | writeln!(out, "struct PtrLen final {{"); |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 167 | writeln!(out, " void *ptr;"); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 168 | writeln!(out, " size_t len;"); |
| 169 | writeln!(out, "}};"); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 170 | out.end_block(Block::Namespace("repr")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 171 | } |
| 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 Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 191 | writeln!( |
| 192 | out, |
| 193 | " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};", |
| 194 | ); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 195 | writeln!(out, " }}"); |
| 196 | } |
| 197 | writeln!(out, "}};"); |
| 198 | } |
| 199 | |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 200 | 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 Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 210 | writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};"); |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 211 | writeln!(out, " }}"); |
| 212 | } |
| 213 | if builtin.rust_slice_repr { |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 214 | include.type_traits = true; |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 215 | writeln!( |
| 216 | out, |
| 217 | " static repr::PtrLen repr(Slice<T> slice) noexcept {{", |
| 218 | ); |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 219 | 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 Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 225 | writeln!(out, " }}"); |
| 226 | } |
| 227 | writeln!(out, "}};"); |
| 228 | } |
| 229 | |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 230 | 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 Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 244 | if builtin.is_complete { |
| David Tolnay | 12320e1 | 2020-12-09 23:09:36 -0800 | [diff] [blame^] | 245 | include.cstddef = true; |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 246 | 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 Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 272 | out.end_block(Block::AnonymousNamespace); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 273 | out.end_block(Block::InlineNamespace("cxxbridge1")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 274 | |
| 275 | if builtin.trycatch { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 276 | out.begin_block(Block::Namespace("behavior")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 277 | 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 Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 295 | out.end_block(Block::Namespace("behavior")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 298 | out.end_block(Block::Namespace("rust")); |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 299 | |
| 300 | if builtin.exception { |
| David Tolnay | 12320e1 | 2020-12-09 23:09:36 -0800 | [diff] [blame^] | 301 | include.cstddef = true; |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 302 | out.begin_block(Block::ExternC); |
| 303 | writeln!( |
| 304 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 305 | "const char *cxxbridge1$exception(const char *, size_t);", |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 306 | ); |
| 307 | out.end_block(Block::ExternC); |
| 308 | } |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 309 | } |