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