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