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