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