| 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, |
| 15 | pub unsafe_bitcopy: bool, |
| 16 | pub rust_error: bool, |
| 17 | pub manually_drop: bool, |
| 18 | pub maybe_uninit: bool, |
| 19 | pub trycatch: bool, |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 20 | pub ptr_len: bool, |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 21 | pub rust_str_new_unchecked: bool, |
| 22 | pub rust_str_repr: bool, |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 23 | pub rust_slice_new: bool, |
| 24 | pub rust_slice_repr: bool, |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 25 | pub exception: bool, |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 26 | pub relocatable: bool, |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 27 | pub friend_impl: bool, |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 28 | pub content: Content<'a>, |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 31 | impl<'a> Builtins<'a> { |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 32 | pub fn new() -> Self { |
| 33 | Builtins::default() |
| 34 | } |
| 35 | } |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 36 | |
| 37 | pub(super) fn write(out: &mut OutFile) { |
| 38 | if out.builtin == Default::default() { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | let include = &mut out.include; |
| 43 | let builtin = &mut out.builtin; |
| 44 | let out = &mut builtin.content; |
| 45 | |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 46 | if builtin.rust_string { |
| 47 | include.array = true; |
| 48 | include.cstdint = true; |
| 49 | include.string = true; |
| 50 | } |
| 51 | |
| 52 | if builtin.rust_str { |
| 53 | include.cstdint = true; |
| 54 | include.string = true; |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 55 | builtin.friend_impl = true; |
| 56 | } |
| 57 | |
| 58 | if builtin.rust_slice { |
| 59 | builtin.friend_impl = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if builtin.rust_box { |
| 63 | include.new = true; |
| 64 | include.type_traits = true; |
| 65 | } |
| 66 | |
| 67 | if builtin.rust_vec { |
| 68 | include.array = true; |
| 69 | include.new = true; |
| 70 | include.type_traits = true; |
| 71 | builtin.panic = true; |
| 72 | builtin.unsafe_bitcopy = true; |
| 73 | } |
| 74 | |
| 75 | if builtin.rust_error { |
| 76 | include.exception = true; |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 77 | builtin.friend_impl = true; |
| David Tolnay | dcfa8e9 | 2020-11-02 09:50:06 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if builtin.rust_isize { |
| 81 | include.basetsd = true; |
| 82 | } |
| 83 | |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 84 | if builtin.relocatable { |
| 85 | include.type_traits = true; |
| 86 | } |
| 87 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 88 | out.begin_block(Block::Namespace("rust")); |
| 89 | out.begin_block(Block::InlineNamespace("cxxbridge05")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 90 | writeln!(out, "// #include \"rust/cxx.h\""); |
| 91 | |
| 92 | ifndef::write(out, builtin.panic, "CXXBRIDGE05_PANIC"); |
| 93 | |
| 94 | if builtin.rust_string { |
| 95 | out.next_section(); |
| 96 | writeln!(out, "struct unsafe_bitcopy_t;"); |
| 97 | } |
| 98 | |
| David Tolnay | 22664ae | 2020-11-04 16:30:45 -0800 | [diff] [blame] | 99 | if builtin.friend_impl { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 100 | out.begin_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 101 | writeln!(out, "template <typename T>"); |
| 102 | writeln!(out, "class impl;"); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 103 | out.end_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ifndef::write(out, builtin.rust_string, "CXXBRIDGE05_RUST_STRING"); |
| 107 | ifndef::write(out, builtin.rust_str, "CXXBRIDGE05_RUST_STR"); |
| 108 | ifndef::write(out, builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE"); |
| 109 | ifndef::write(out, builtin.rust_box, "CXXBRIDGE05_RUST_BOX"); |
| 110 | ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY"); |
| 111 | ifndef::write(out, builtin.rust_vec, "CXXBRIDGE05_RUST_VEC"); |
| 112 | ifndef::write(out, builtin.rust_fn, "CXXBRIDGE05_RUST_FN"); |
| 113 | ifndef::write(out, builtin.rust_error, "CXXBRIDGE05_RUST_ERROR"); |
| 114 | ifndef::write(out, builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE"); |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 115 | ifndef::write(out, builtin.relocatable, "CXXBRIDGE05_RELOCATABLE"); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 116 | |
| 117 | if builtin.manually_drop { |
| 118 | out.next_section(); |
| 119 | include.utility = true; |
| 120 | writeln!(out, "template <typename T>"); |
| 121 | writeln!(out, "union ManuallyDrop {{"); |
| 122 | writeln!(out, " T value;"); |
| 123 | writeln!( |
| 124 | out, |
| 125 | " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}", |
| 126 | ); |
| 127 | writeln!(out, " ~ManuallyDrop() {{}}"); |
| 128 | writeln!(out, "}};"); |
| 129 | } |
| 130 | |
| 131 | if builtin.maybe_uninit { |
| 132 | out.next_section(); |
| 133 | writeln!(out, "template <typename T>"); |
| 134 | writeln!(out, "union MaybeUninit {{"); |
| 135 | writeln!(out, " T value;"); |
| 136 | writeln!(out, " MaybeUninit() {{}}"); |
| 137 | writeln!(out, " ~MaybeUninit() {{}}"); |
| 138 | writeln!(out, "}};"); |
| 139 | } |
| 140 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 141 | out.begin_block(Block::AnonymousNamespace); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 142 | |
| David Tolnay | 919085c | 2020-10-31 22:32:22 -0700 | [diff] [blame] | 143 | if builtin.ptr_len { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 144 | out.begin_block(Block::Namespace("repr")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 145 | writeln!(out, "struct PtrLen final {{"); |
| 146 | writeln!(out, " const void *ptr;"); |
| 147 | writeln!(out, " size_t len;"); |
| 148 | writeln!(out, "}};"); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 149 | out.end_block(Block::Namespace("repr")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if builtin.rust_str_new_unchecked || builtin.rust_str_repr { |
| 153 | out.next_section(); |
| 154 | writeln!(out, "template <>"); |
| 155 | writeln!(out, "class impl<Str> final {{"); |
| 156 | writeln!(out, "public:"); |
| 157 | if builtin.rust_str_new_unchecked { |
| 158 | writeln!( |
| 159 | out, |
| 160 | " static Str new_unchecked(repr::PtrLen repr) noexcept {{", |
| 161 | ); |
| 162 | writeln!(out, " Str str;"); |
| 163 | writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);"); |
| 164 | writeln!(out, " str.len = repr.len;"); |
| 165 | writeln!(out, " return str;"); |
| 166 | writeln!(out, " }}"); |
| 167 | } |
| 168 | if builtin.rust_str_repr { |
| 169 | writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{"); |
| 170 | writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};"); |
| 171 | writeln!(out, " }}"); |
| 172 | } |
| 173 | writeln!(out, "}};"); |
| 174 | } |
| 175 | |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 176 | if builtin.rust_slice_new || builtin.rust_slice_repr { |
| 177 | out.next_section(); |
| 178 | writeln!(out, "template <typename T>"); |
| 179 | writeln!(out, "class impl<Slice<T>> final {{"); |
| 180 | writeln!(out, "public:"); |
| 181 | if builtin.rust_slice_new { |
| 182 | writeln!( |
| 183 | out, |
| 184 | " static Slice<T> slice(repr::PtrLen repr) noexcept {{", |
| 185 | ); |
| 186 | writeln!( |
| 187 | out, |
| 188 | " return {{static_cast<const T *>(repr.ptr), repr.len}};", |
| 189 | ); |
| 190 | writeln!(out, " }}"); |
| 191 | } |
| 192 | if builtin.rust_slice_repr { |
| 193 | writeln!( |
| 194 | out, |
| 195 | " static repr::PtrLen repr(Slice<T> slice) noexcept {{", |
| 196 | ); |
| 197 | writeln!(out, " return repr::PtrLen{{slice.ptr, slice.len}};"); |
| 198 | writeln!(out, " }}"); |
| 199 | } |
| 200 | writeln!(out, "}};"); |
| 201 | } |
| 202 | |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 203 | if builtin.rust_error { |
| 204 | out.next_section(); |
| 205 | writeln!(out, "template <>"); |
| 206 | writeln!(out, "class impl<Error> final {{"); |
| 207 | writeln!(out, "public:"); |
| 208 | writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{"); |
| 209 | writeln!(out, " Error error;"); |
| 210 | writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);"); |
| 211 | writeln!(out, " error.len = repr.len;"); |
| 212 | writeln!(out, " return error;"); |
| 213 | writeln!(out, " }}"); |
| 214 | writeln!(out, "}};"); |
| 215 | } |
| 216 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 217 | out.end_block(Block::AnonymousNamespace); |
| 218 | out.end_block(Block::InlineNamespace("cxxbridge05")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 219 | |
| 220 | if builtin.trycatch { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 221 | out.begin_block(Block::Namespace("behavior")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 222 | include.exception = true; |
| 223 | include.type_traits = true; |
| 224 | include.utility = true; |
| 225 | writeln!(out, "class missing {{}};"); |
| 226 | writeln!(out, "missing trycatch(...);"); |
| 227 | writeln!(out); |
| 228 | writeln!(out, "template <typename Try, typename Fail>"); |
| 229 | writeln!(out, "static typename ::std::enable_if<"); |
| 230 | writeln!( |
| 231 | out, |
| 232 | " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),", |
| 233 | ); |
| 234 | writeln!(out, " missing>::value>::type"); |
| 235 | writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); |
| 236 | writeln!(out, " func();"); |
| 237 | writeln!(out, "}} catch (const ::std::exception &e) {{"); |
| 238 | writeln!(out, " fail(e.what());"); |
| 239 | writeln!(out, "}}"); |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 240 | out.end_block(Block::Namespace("behavior")); |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 243 | out.end_block(Block::Namespace("rust")); |
| David Tolnay | 1f010c6 | 2020-11-01 20:27:46 -0800 | [diff] [blame] | 244 | |
| 245 | if builtin.exception { |
| 246 | out.begin_block(Block::ExternC); |
| 247 | writeln!( |
| 248 | out, |
| 249 | "const char *cxxbridge05$exception(const char *, size_t);", |
| 250 | ); |
| 251 | out.end_block(Block::ExternC); |
| 252 | } |
| David Tolnay | 3374d8d | 2020-10-31 22:18:45 -0700 | [diff] [blame] | 253 | } |