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