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