blob: 8a25769768acd0c3cf4f14eb3485228509f286c8 [file] [log] [blame]
David Tolnay0c033e32020-11-01 15:15:48 -08001use crate::gen::block::Block;
David Tolnay3374d8d2020-10-31 22:18:45 -07002use crate::gen::ifndef;
3use crate::gen::out::{Content, OutFile};
David Tolnay8c14d9a2020-10-31 21:52:38 -07004
David Tolnay3be0e1f2020-10-31 20:53:00 -07005#[derive(Default, PartialEq)]
David Tolnay97c5b862020-11-01 14:59:01 -08006pub struct Builtins<'a> {
David Tolnay3be0e1f2020-10-31 20:53:00 -07007 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,
15 pub unsafe_bitcopy: bool,
16 pub rust_error: bool,
17 pub manually_drop: bool,
18 pub maybe_uninit: bool,
19 pub trycatch: bool,
David Tolnay919085c2020-10-31 22:32:22 -070020 pub ptr_len: bool,
David Tolnay3be0e1f2020-10-31 20:53:00 -070021 pub rust_str_new_unchecked: bool,
22 pub rust_str_repr: bool,
David Tolnay36aa9e02020-10-31 23:08:21 -070023 pub rust_slice_new: bool,
24 pub rust_slice_repr: bool,
David Tolnay1f010c62020-11-01 20:27:46 -080025 pub exception: bool,
David Tolnay174bd952020-11-02 09:23:12 -080026 pub relocatable: bool,
David Tolnay22664ae2020-11-04 16:30:45 -080027 pub friend_impl: bool,
David Tolnay97c5b862020-11-01 14:59:01 -080028 pub content: Content<'a>,
David Tolnay3be0e1f2020-10-31 20:53:00 -070029}
30
David Tolnay97c5b862020-11-01 14:59:01 -080031impl<'a> Builtins<'a> {
David Tolnay3be0e1f2020-10-31 20:53:00 -070032 pub fn new() -> Self {
33 Builtins::default()
34 }
35}
David Tolnay3374d8d2020-10-31 22:18:45 -070036
37pub(super) fn write(out: &mut OutFile) {
38 if out.builtin == Default::default() {
39 return;
40 }
41
42 let include = &mut out.include;
43 let builtin = &mut out.builtin;
44 let out = &mut builtin.content;
45
David Tolnaydcfa8e92020-11-02 09:50:06 -080046 if builtin.rust_string {
47 include.array = true;
48 include.cstdint = true;
49 include.string = true;
50 }
51
52 if builtin.rust_str {
53 include.cstdint = true;
54 include.string = true;
David Tolnay22664ae2020-11-04 16:30:45 -080055 builtin.friend_impl = true;
56 }
57
58 if builtin.rust_slice {
59 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080060 }
61
62 if builtin.rust_box {
63 include.new = true;
64 include.type_traits = true;
65 }
66
67 if builtin.rust_vec {
68 include.array = true;
69 include.new = true;
70 include.type_traits = true;
71 builtin.panic = true;
72 builtin.unsafe_bitcopy = true;
73 }
74
David Tolnaya5a13012020-11-10 15:03:09 -080075 if builtin.rust_fn {
76 include.utility = true;
77 }
78
David Tolnaydcfa8e92020-11-02 09:50:06 -080079 if builtin.rust_error {
80 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080081 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080082 }
83
84 if builtin.rust_isize {
85 include.basetsd = true;
86 }
87
David Tolnay174bd952020-11-02 09:23:12 -080088 if builtin.relocatable {
89 include.type_traits = true;
90 }
91
David Tolnay0c033e32020-11-01 15:15:48 -080092 out.begin_block(Block::Namespace("rust"));
93 out.begin_block(Block::InlineNamespace("cxxbridge05"));
David Tolnay3374d8d2020-10-31 22:18:45 -070094 writeln!(out, "// #include \"rust/cxx.h\"");
95
96 ifndef::write(out, builtin.panic, "CXXBRIDGE05_PANIC");
97
98 if builtin.rust_string {
99 out.next_section();
100 writeln!(out, "struct unsafe_bitcopy_t;");
101 }
102
David Tolnay22664ae2020-11-04 16:30:45 -0800103 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800104 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700105 writeln!(out, "template <typename T>");
106 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800107 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700108 }
109
110 ifndef::write(out, builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
111 ifndef::write(out, builtin.rust_str, "CXXBRIDGE05_RUST_STR");
112 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
113 ifndef::write(out, builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
114 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
115 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
116 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
117 ifndef::write(out, builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
118 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnay174bd952020-11-02 09:23:12 -0800119 ifndef::write(out, builtin.relocatable, "CXXBRIDGE05_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700120
121 if builtin.manually_drop {
122 out.next_section();
123 include.utility = true;
124 writeln!(out, "template <typename T>");
125 writeln!(out, "union ManuallyDrop {{");
126 writeln!(out, " T value;");
127 writeln!(
128 out,
129 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
130 );
131 writeln!(out, " ~ManuallyDrop() {{}}");
132 writeln!(out, "}};");
133 }
134
135 if builtin.maybe_uninit {
136 out.next_section();
137 writeln!(out, "template <typename T>");
138 writeln!(out, "union MaybeUninit {{");
139 writeln!(out, " T value;");
140 writeln!(out, " MaybeUninit() {{}}");
141 writeln!(out, " ~MaybeUninit() {{}}");
142 writeln!(out, "}};");
143 }
144
David Tolnay0c033e32020-11-01 15:15:48 -0800145 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700146
David Tolnay919085c2020-10-31 22:32:22 -0700147 if builtin.ptr_len {
David Tolnay0c033e32020-11-01 15:15:48 -0800148 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700149 writeln!(out, "struct PtrLen final {{");
150 writeln!(out, " const void *ptr;");
151 writeln!(out, " size_t len;");
152 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800153 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700154 }
155
156 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
157 out.next_section();
158 writeln!(out, "template <>");
159 writeln!(out, "class impl<Str> final {{");
160 writeln!(out, "public:");
161 if builtin.rust_str_new_unchecked {
162 writeln!(
163 out,
164 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
165 );
166 writeln!(out, " Str str;");
167 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
168 writeln!(out, " str.len = repr.len;");
169 writeln!(out, " return str;");
170 writeln!(out, " }}");
171 }
172 if builtin.rust_str_repr {
173 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
174 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
175 writeln!(out, " }}");
176 }
177 writeln!(out, "}};");
178 }
179
David Tolnay36aa9e02020-10-31 23:08:21 -0700180 if builtin.rust_slice_new || builtin.rust_slice_repr {
181 out.next_section();
182 writeln!(out, "template <typename T>");
183 writeln!(out, "class impl<Slice<T>> final {{");
184 writeln!(out, "public:");
185 if builtin.rust_slice_new {
186 writeln!(
187 out,
188 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
189 );
190 writeln!(
191 out,
192 " return {{static_cast<const T *>(repr.ptr), repr.len}};",
193 );
194 writeln!(out, " }}");
195 }
196 if builtin.rust_slice_repr {
197 writeln!(
198 out,
199 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
200 );
201 writeln!(out, " return repr::PtrLen{{slice.ptr, slice.len}};");
202 writeln!(out, " }}");
203 }
204 writeln!(out, "}};");
205 }
206
David Tolnay3374d8d2020-10-31 22:18:45 -0700207 if builtin.rust_error {
208 out.next_section();
209 writeln!(out, "template <>");
210 writeln!(out, "class impl<Error> final {{");
211 writeln!(out, "public:");
212 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
213 writeln!(out, " Error error;");
214 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
215 writeln!(out, " error.len = repr.len;");
216 writeln!(out, " return error;");
217 writeln!(out, " }}");
218 writeln!(out, "}};");
219 }
220
David Tolnay0c033e32020-11-01 15:15:48 -0800221 out.end_block(Block::AnonymousNamespace);
222 out.end_block(Block::InlineNamespace("cxxbridge05"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700223
224 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800225 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700226 include.exception = true;
227 include.type_traits = true;
228 include.utility = true;
229 writeln!(out, "class missing {{}};");
230 writeln!(out, "missing trycatch(...);");
231 writeln!(out);
232 writeln!(out, "template <typename Try, typename Fail>");
233 writeln!(out, "static typename ::std::enable_if<");
234 writeln!(
235 out,
236 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
237 );
238 writeln!(out, " missing>::value>::type");
239 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
240 writeln!(out, " func();");
241 writeln!(out, "}} catch (const ::std::exception &e) {{");
242 writeln!(out, " fail(e.what());");
243 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800244 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700245 }
246
David Tolnay0c033e32020-11-01 15:15:48 -0800247 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800248
249 if builtin.exception {
250 out.begin_block(Block::ExternC);
251 writeln!(
252 out,
253 "const char *cxxbridge05$exception(const char *, size_t);",
254 );
255 out.end_block(Block::ExternC);
256 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700257}