| 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 | 450d8cd | 2020-11-25 20:36:06 -0800 | [diff] [blame^] | 62 | include.type_traits = true; |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 63 | builtin.friend_impl = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if builtin.rust_box { |
| 67 | include.new = true; |
| 68 | include.type_traits = true; |
| David Tolnay | ea6f86c | 2020-11-10 15:08:57 -0800 | [diff] [blame] | 69 | include.utility = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if builtin.rust_vec { |
| 73 | include.array = true; |
| 74 | include.new = true; |
| 75 | include.type_traits = true; |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 76 | include.utility = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 77 | builtin.panic = true; |
| 78 | builtin.unsafe_bitcopy = true; |
| 79 | } |
| 80 | |
| David Tolnay | a5a1301 | 2020-11-10 15:03:09 -0800 | [diff] [blame] | 81 | if builtin.rust_fn { |
| 82 | include.utility = true; |
| 83 | } |
| 84 | |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 85 | if builtin.rust_error { |
| 86 | include.exception = true; |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 87 | builtin.friend_impl = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if builtin.rust_isize { |
| 91 | include.basetsd = true; |
| 92 | } |
| 93 | |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 94 | if builtin.relocatable { |
| 95 | include.type_traits = true; |
| 96 | } |
| 97 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 98 | out.begin_block(Block::Namespace("rust")); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 99 | out.begin_block(Block::InlineNamespace("cxxbridge1")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 100 | writeln!(out, "// #include \"rust/cxx.h\""); |
| 101 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 102 | ifndef::write(out, builtin.panic, "CXXBRIDGE1_PANIC"); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 103 | |
| 104 | if builtin.rust_string { |
| 105 | out.next_section(); |
| 106 | writeln!(out, "struct unsafe_bitcopy_t;"); |
| 107 | } |
| 108 | |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 109 | if builtin.friend_impl { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 110 | out.begin_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 111 | writeln!(out, "template <typename T>"); |
| 112 | writeln!(out, "class impl;"); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 113 | out.end_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 116 | ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING"); |
| 117 | ifndef::write(out, builtin.rust_str, "CXXBRIDGE1_RUST_STR"); |
| 118 | ifndef::write(out, builtin.rust_slice, "CXXBRIDGE1_RUST_SLICE"); |
| 119 | ifndef::write(out, builtin.rust_box, "CXXBRIDGE1_RUST_BOX"); |
| 120 | ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE1_RUST_BITCOPY"); |
| 121 | ifndef::write(out, builtin.rust_vec, "CXXBRIDGE1_RUST_VEC"); |
| 122 | ifndef::write(out, builtin.rust_fn, "CXXBRIDGE1_RUST_FN"); |
| 123 | ifndef::write(out, builtin.rust_error, "CXXBRIDGE1_RUST_ERROR"); |
| 124 | ifndef::write(out, builtin.rust_isize, "CXXBRIDGE1_RUST_ISIZE"); |
| David Tolnay | 365fc7c | 2020-11-25 16:08:13 -0800 | [diff] [blame] | 125 | ifndef::write(out, builtin.opaque, "CXXBRIDGE1_RUST_OPAQUE"); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 126 | ifndef::write(out, builtin.relocatable, "CXXBRIDGE1_RELOCATABLE"); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 127 | |
| 128 | if builtin.manually_drop { |
| 129 | out.next_section(); |
| 130 | include.utility = true; |
| 131 | writeln!(out, "template <typename T>"); |
| 132 | writeln!(out, "union ManuallyDrop {{"); |
| 133 | writeln!(out, " T value;"); |
| 134 | writeln!( |
| 135 | out, |
| 136 | " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}", |
| 137 | ); |
| 138 | writeln!(out, " ~ManuallyDrop() {{}}"); |
| 139 | writeln!(out, "}};"); |
| 140 | } |
| 141 | |
| 142 | if builtin.maybe_uninit { |
| 143 | out.next_section(); |
| 144 | writeln!(out, "template <typename T>"); |
| 145 | writeln!(out, "union MaybeUninit {{"); |
| 146 | writeln!(out, " T value;"); |
| 147 | writeln!(out, " MaybeUninit() {{}}"); |
| 148 | writeln!(out, " ~MaybeUninit() {{}}"); |
| 149 | writeln!(out, "}};"); |
| 150 | } |
| 151 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 152 | out.begin_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 153 | |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 154 | if builtin.ptr_len { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 155 | out.begin_block(Block::Namespace("repr")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 156 | writeln!(out, "struct PtrLen final {{"); |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 157 | writeln!(out, " void *ptr;"); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 158 | writeln!(out, " size_t len;"); |
| 159 | writeln!(out, "}};"); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 160 | out.end_block(Block::Namespace("repr")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | if builtin.rust_str_new_unchecked || builtin.rust_str_repr { |
| 164 | out.next_section(); |
| 165 | writeln!(out, "template <>"); |
| 166 | writeln!(out, "class impl<Str> final {{"); |
| 167 | writeln!(out, "public:"); |
| 168 | if builtin.rust_str_new_unchecked { |
| 169 | writeln!( |
| 170 | out, |
| 171 | " static Str new_unchecked(repr::PtrLen repr) noexcept {{", |
| 172 | ); |
| 173 | writeln!(out, " Str str;"); |
| 174 | writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);"); |
| 175 | writeln!(out, " str.len = repr.len;"); |
| 176 | writeln!(out, " return str;"); |
| 177 | writeln!(out, " }}"); |
| 178 | } |
| 179 | if builtin.rust_str_repr { |
| 180 | writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{"); |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 181 | writeln!( |
| 182 | out, |
| 183 | " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};", |
| 184 | ); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 185 | writeln!(out, " }}"); |
| 186 | } |
| 187 | writeln!(out, "}};"); |
| 188 | } |
| 189 | |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 190 | if builtin.rust_slice_new || builtin.rust_slice_repr { |
| 191 | out.next_section(); |
| 192 | writeln!(out, "template <typename T>"); |
| 193 | writeln!(out, "class impl<Slice<T>> final {{"); |
| 194 | writeln!(out, "public:"); |
| 195 | if builtin.rust_slice_new { |
| 196 | writeln!( |
| 197 | out, |
| 198 | " static Slice<T> slice(repr::PtrLen repr) noexcept {{", |
| 199 | ); |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 200 | writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};"); |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 201 | writeln!(out, " }}"); |
| 202 | } |
| 203 | if builtin.rust_slice_repr { |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 204 | include.type_traits = true; |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 205 | writeln!( |
| 206 | out, |
| 207 | " static repr::PtrLen repr(Slice<T> slice) noexcept {{", |
| 208 | ); |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 209 | writeln!(out, " return repr::PtrLen{{"); |
| 210 | writeln!( |
| 211 | out, |
| 212 | " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),", |
| 213 | ); |
| 214 | writeln!(out, " slice.len}};"); |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 215 | writeln!(out, " }}"); |
| 216 | } |
| 217 | writeln!(out, "}};"); |
| 218 | } |
| 219 | |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 220 | if builtin.rust_error { |
| 221 | out.next_section(); |
| 222 | writeln!(out, "template <>"); |
| 223 | writeln!(out, "class impl<Error> final {{"); |
| 224 | writeln!(out, "public:"); |
| 225 | writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{"); |
| 226 | writeln!(out, " Error error;"); |
| 227 | writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);"); |
| 228 | writeln!(out, " error.len = repr.len;"); |
| 229 | writeln!(out, " return error;"); |
| 230 | writeln!(out, " }}"); |
| 231 | writeln!(out, "}};"); |
| 232 | } |
| 233 | |
| David Tolnay | 5346276 | 2020-11-25 07:08:10 -0800 | [diff] [blame] | 234 | if builtin.is_complete { |
| 235 | include.type_traits = true; |
| 236 | out.next_section(); |
| 237 | writeln!(out, "template <typename T, typename = size_t>"); |
| 238 | writeln!(out, "struct is_complete : std::false_type {{}};"); |
| 239 | out.next_section(); |
| 240 | writeln!(out, "template <typename T>"); |
| 241 | writeln!( |
| 242 | out, |
| 243 | "struct is_complete<T, decltype(sizeof(T))> : std::true_type {{}};", |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | if builtin.deleter_if { |
| 248 | out.next_section(); |
| 249 | writeln!(out, "template <bool> struct deleter_if {{"); |
| 250 | writeln!(out, " template <typename T> void operator()(T *) {{}}"); |
| 251 | writeln!(out, "}};"); |
| 252 | out.next_section(); |
| 253 | writeln!(out, "template <> struct deleter_if<true> {{"); |
| 254 | writeln!( |
| 255 | out, |
| 256 | " template <typename T> void operator()(T *ptr) {{ ptr->~T(); }}", |
| 257 | ); |
| 258 | writeln!(out, "}};"); |
| 259 | } |
| 260 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 261 | out.end_block(Block::AnonymousNamespace); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 262 | out.end_block(Block::InlineNamespace("cxxbridge1")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 263 | |
| 264 | if builtin.trycatch { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 265 | out.begin_block(Block::Namespace("behavior")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 266 | include.exception = true; |
| 267 | include.type_traits = true; |
| 268 | include.utility = true; |
| 269 | writeln!(out, "class missing {{}};"); |
| 270 | writeln!(out, "missing trycatch(...);"); |
| 271 | writeln!(out); |
| 272 | writeln!(out, "template <typename Try, typename Fail>"); |
| 273 | writeln!(out, "static typename ::std::enable_if<"); |
| 274 | writeln!( |
| 275 | out, |
| 276 | " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),", |
| 277 | ); |
| 278 | writeln!(out, " missing>::value>::type"); |
| 279 | writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); |
| 280 | writeln!(out, " func();"); |
| 281 | writeln!(out, "}} catch (const ::std::exception &e) {{"); |
| 282 | writeln!(out, " fail(e.what());"); |
| 283 | writeln!(out, "}}"); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 284 | out.end_block(Block::Namespace("behavior")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 287 | out.end_block(Block::Namespace("rust")); |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 288 | |
| 289 | if builtin.exception { |
| 290 | out.begin_block(Block::ExternC); |
| 291 | writeln!( |
| 292 | out, |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 293 | "const char *cxxbridge1$exception(const char *, size_t);", |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 294 | ); |
| 295 | out.end_block(Block::ExternC); |
| 296 | } |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 297 | } |