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