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