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