blob: f3240d031ed7369822fc34a1c19fde576619e5c8 [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
75 if builtin.rust_error {
76 include.exception = true;
David Tolnay22664ae2020-11-04 16:30:45 -080077 builtin.friend_impl = true;
David Tolnaydcfa8e92020-11-02 09:50:06 -080078 }
79
80 if builtin.rust_isize {
81 include.basetsd = true;
82 }
83
David Tolnay174bd952020-11-02 09:23:12 -080084 if builtin.relocatable {
85 include.type_traits = true;
86 }
87
David Tolnay0c033e32020-11-01 15:15:48 -080088 out.begin_block(Block::Namespace("rust"));
89 out.begin_block(Block::InlineNamespace("cxxbridge05"));
David Tolnay3374d8d2020-10-31 22:18:45 -070090 writeln!(out, "// #include \"rust/cxx.h\"");
91
92 ifndef::write(out, builtin.panic, "CXXBRIDGE05_PANIC");
93
94 if builtin.rust_string {
95 out.next_section();
96 writeln!(out, "struct unsafe_bitcopy_t;");
97 }
98
David Tolnay22664ae2020-11-04 16:30:45 -080099 if builtin.friend_impl {
David Tolnay0c033e32020-11-01 15:15:48 -0800100 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700101 writeln!(out, "template <typename T>");
102 writeln!(out, "class impl;");
David Tolnay0c033e32020-11-01 15:15:48 -0800103 out.end_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700104 }
105
106 ifndef::write(out, builtin.rust_string, "CXXBRIDGE05_RUST_STRING");
107 ifndef::write(out, builtin.rust_str, "CXXBRIDGE05_RUST_STR");
108 ifndef::write(out, builtin.rust_slice, "CXXBRIDGE05_RUST_SLICE");
109 ifndef::write(out, builtin.rust_box, "CXXBRIDGE05_RUST_BOX");
110 ifndef::write(out, builtin.unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
111 ifndef::write(out, builtin.rust_vec, "CXXBRIDGE05_RUST_VEC");
112 ifndef::write(out, builtin.rust_fn, "CXXBRIDGE05_RUST_FN");
113 ifndef::write(out, builtin.rust_error, "CXXBRIDGE05_RUST_ERROR");
114 ifndef::write(out, builtin.rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnay174bd952020-11-02 09:23:12 -0800115 ifndef::write(out, builtin.relocatable, "CXXBRIDGE05_RELOCATABLE");
David Tolnay3374d8d2020-10-31 22:18:45 -0700116
117 if builtin.manually_drop {
118 out.next_section();
119 include.utility = true;
120 writeln!(out, "template <typename T>");
121 writeln!(out, "union ManuallyDrop {{");
122 writeln!(out, " T value;");
123 writeln!(
124 out,
125 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
126 );
127 writeln!(out, " ~ManuallyDrop() {{}}");
128 writeln!(out, "}};");
129 }
130
131 if builtin.maybe_uninit {
132 out.next_section();
133 writeln!(out, "template <typename T>");
134 writeln!(out, "union MaybeUninit {{");
135 writeln!(out, " T value;");
136 writeln!(out, " MaybeUninit() {{}}");
137 writeln!(out, " ~MaybeUninit() {{}}");
138 writeln!(out, "}};");
139 }
140
David Tolnay0c033e32020-11-01 15:15:48 -0800141 out.begin_block(Block::AnonymousNamespace);
David Tolnay3374d8d2020-10-31 22:18:45 -0700142
David Tolnay919085c2020-10-31 22:32:22 -0700143 if builtin.ptr_len {
David Tolnay0c033e32020-11-01 15:15:48 -0800144 out.begin_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700145 writeln!(out, "struct PtrLen final {{");
146 writeln!(out, " const void *ptr;");
147 writeln!(out, " size_t len;");
148 writeln!(out, "}};");
David Tolnay0c033e32020-11-01 15:15:48 -0800149 out.end_block(Block::Namespace("repr"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700150 }
151
152 if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
153 out.next_section();
154 writeln!(out, "template <>");
155 writeln!(out, "class impl<Str> final {{");
156 writeln!(out, "public:");
157 if builtin.rust_str_new_unchecked {
158 writeln!(
159 out,
160 " static Str new_unchecked(repr::PtrLen repr) noexcept {{",
161 );
162 writeln!(out, " Str str;");
163 writeln!(out, " str.ptr = static_cast<const char *>(repr.ptr);");
164 writeln!(out, " str.len = repr.len;");
165 writeln!(out, " return str;");
166 writeln!(out, " }}");
167 }
168 if builtin.rust_str_repr {
169 writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
170 writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
171 writeln!(out, " }}");
172 }
173 writeln!(out, "}};");
174 }
175
David Tolnay36aa9e02020-10-31 23:08:21 -0700176 if builtin.rust_slice_new || builtin.rust_slice_repr {
177 out.next_section();
178 writeln!(out, "template <typename T>");
179 writeln!(out, "class impl<Slice<T>> final {{");
180 writeln!(out, "public:");
181 if builtin.rust_slice_new {
182 writeln!(
183 out,
184 " static Slice<T> slice(repr::PtrLen repr) noexcept {{",
185 );
186 writeln!(
187 out,
188 " return {{static_cast<const T *>(repr.ptr), repr.len}};",
189 );
190 writeln!(out, " }}");
191 }
192 if builtin.rust_slice_repr {
193 writeln!(
194 out,
195 " static repr::PtrLen repr(Slice<T> slice) noexcept {{",
196 );
197 writeln!(out, " return repr::PtrLen{{slice.ptr, slice.len}};");
198 writeln!(out, " }}");
199 }
200 writeln!(out, "}};");
201 }
202
David Tolnay3374d8d2020-10-31 22:18:45 -0700203 if builtin.rust_error {
204 out.next_section();
205 writeln!(out, "template <>");
206 writeln!(out, "class impl<Error> final {{");
207 writeln!(out, "public:");
208 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
209 writeln!(out, " Error error;");
210 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
211 writeln!(out, " error.len = repr.len;");
212 writeln!(out, " return error;");
213 writeln!(out, " }}");
214 writeln!(out, "}};");
215 }
216
David Tolnay0c033e32020-11-01 15:15:48 -0800217 out.end_block(Block::AnonymousNamespace);
218 out.end_block(Block::InlineNamespace("cxxbridge05"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700219
220 if builtin.trycatch {
David Tolnay0c033e32020-11-01 15:15:48 -0800221 out.begin_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700222 include.exception = true;
223 include.type_traits = true;
224 include.utility = true;
225 writeln!(out, "class missing {{}};");
226 writeln!(out, "missing trycatch(...);");
227 writeln!(out);
228 writeln!(out, "template <typename Try, typename Fail>");
229 writeln!(out, "static typename ::std::enable_if<");
230 writeln!(
231 out,
232 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
233 );
234 writeln!(out, " missing>::value>::type");
235 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
236 writeln!(out, " func();");
237 writeln!(out, "}} catch (const ::std::exception &e) {{");
238 writeln!(out, " fail(e.what());");
239 writeln!(out, "}}");
David Tolnay0c033e32020-11-01 15:15:48 -0800240 out.end_block(Block::Namespace("behavior"));
David Tolnay3374d8d2020-10-31 22:18:45 -0700241 }
242
David Tolnay0c033e32020-11-01 15:15:48 -0800243 out.end_block(Block::Namespace("rust"));
David Tolnay1f010c62020-11-01 20:27:46 -0800244
245 if builtin.exception {
246 out.begin_block(Block::ExternC);
247 writeln!(
248 out,
249 "const char *cxxbridge05$exception(const char *, size_t);",
250 );
251 out.end_block(Block::ExternC);
252 }
David Tolnay3374d8d2020-10-31 22:18:45 -0700253}