blob: 33898ce517466306ea4538d5447422c8231db3ce [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::include;
2use crate::gen::out::OutFile;
3use crate::syntax::atom::Atom::{self, *};
4use crate::syntax::{Api, ExternFn, Struct, Type, Types, Var};
5use proc_macro2::Ident;
6
7pub(super) fn gen(namespace: Vec<String>, apis: &[Api], types: &Types, header: bool) -> OutFile {
8 let mut out_file = OutFile::new(namespace.clone(), header);
9 let out = &mut out_file;
10
11 if header {
12 writeln!(out, "#pragma once");
13 }
14
15 for api in apis {
16 if let Api::Include(include) = api {
17 writeln!(out, "#include \"{}\"", include.value().escape_default());
18 }
19 }
20
21 write_includes(out, types);
22 write_include_cxxbridge(out, types);
23
David Tolnay7db73692019-10-20 14:51:12 -040024 out.next_section();
25 for name in &namespace {
26 writeln!(out, "namespace {} {{", name);
27 }
28
David Tolnay7db73692019-10-20 14:51:12 -040029 out.next_section();
30 for api in apis {
31 match api {
32 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080033 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
34 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040035 _ => {}
36 }
37 }
38
39 for api in apis {
40 if let Api::Struct(strct) = api {
41 out.next_section();
42 write_struct(out, strct);
43 }
44 }
45
46 if !header {
47 out.begin_block("extern \"C\"");
48 for api in apis {
49 let (efn, write): (_, fn(_, _, _)) = match api {
50 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
51 Api::RustFunction(efn) => (efn, write_rust_function_decl),
52 _ => continue,
53 };
54 out.next_section();
55 write(out, efn, types);
56 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080057 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040058 }
59
60 for api in apis {
61 if let Api::RustFunction(efn) = api {
62 out.next_section();
63 write_rust_function_shim(out, efn, types);
64 }
65 }
66
67 out.next_section();
68 for name in namespace.iter().rev() {
69 writeln!(out, "}} // namespace {}", name);
70 }
71
72 if !header {
73 out.next_section();
74 write_generic_instantiations(out, types);
75 }
76
77 out_file
78}
79
80fn write_includes(out: &mut OutFile, types: &Types) {
81 let mut has_int = false;
David Tolnayf6292372020-03-01 21:09:11 -080082 let mut has_box = false;
David Tolnay7db73692019-10-20 14:51:12 -040083 let mut has_unique_ptr = false;
84 let mut has_string = false;
85
86 for ty in types {
87 match ty {
88 Type::Ident(ident) => match Atom::from(ident) {
89 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
90 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) => has_int = true,
91 Some(CxxString) => has_string = true,
92 Some(Bool) | Some(RustString) | None => {}
93 },
David Tolnayf6292372020-03-01 21:09:11 -080094 Type::RustBox(_) => has_box = true,
David Tolnay7db73692019-10-20 14:51:12 -040095 Type::UniquePtr(_) => has_unique_ptr = true,
96 _ => {}
97 }
98 }
99
100 if has_int {
101 writeln!(out, "#include <cstdint>");
102 }
103 if has_unique_ptr {
104 writeln!(out, "#include <memory>");
105 }
106 if has_string {
107 writeln!(out, "#include <string>");
108 }
David Tolnayf6292372020-03-01 21:09:11 -0800109 if has_box {
110 writeln!(out, "#include <type_traits>");
111 }
David Tolnay7db73692019-10-20 14:51:12 -0400112}
113
114fn write_include_cxxbridge(out: &mut OutFile, types: &Types) {
115 let mut needs_rust_box = false;
116 for ty in types {
117 if let Type::RustBox(_) = ty {
118 needs_rust_box = true;
119 break;
120 }
121 }
122
David Tolnay750755e2020-03-01 13:04:08 -0800123 out.begin_block("namespace rust");
124 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400125 if needs_rust_box {
126 writeln!(out, "// #include \"cxxbridge.h\"");
David Tolnaye43b7372020-01-08 08:46:20 -0800127 for line in include::get("CXXBRIDGE01_RUST_BOX").lines() {
David Tolnay7db73692019-10-20 14:51:12 -0400128 if !line.trim_start().starts_with("//") {
129 writeln!(out, "{}", line);
130 }
131 }
132 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800133 out.end_block("namespace cxxbridge01");
134 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400135}
136
137fn write_struct(out: &mut OutFile, strct: &Struct) {
138 for line in strct.doc.to_string().lines() {
139 writeln!(out, "//{}", line);
140 }
141 writeln!(out, "struct {} final {{", strct.ident);
142 for field in &strct.fields {
143 write!(out, " ");
144 write_type_space(out, &field.ty);
145 writeln!(out, "{};", field.ident);
146 }
147 writeln!(out, "}};");
148}
149
150fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
151 writeln!(out, "struct {};", ident);
152}
153
David Tolnay8861bee2020-01-20 18:39:24 -0800154fn write_struct_using(out: &mut OutFile, ident: &Ident) {
155 writeln!(out, "using {} = {};", ident, ident);
156}
157
David Tolnay7db73692019-10-20 14:51:12 -0400158fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
159 let indirect_return = efn
160 .ret
161 .as_ref()
162 .map_or(false, |ret| types.needs_indirect_abi(ret));
163 write_extern_return_type(out, &efn.ret, types);
164 for name in out.namespace.clone() {
165 write!(out, "{}$", name);
166 }
David Tolnaye43b7372020-01-08 08:46:20 -0800167 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400168 for (i, arg) in efn.args.iter().enumerate() {
169 if i > 0 {
170 write!(out, ", ");
171 }
David Tolnaya46a2372020-03-06 10:03:48 -0800172 if arg.ty == RustString {
173 write!(out, "const ");
174 }
David Tolnay7db73692019-10-20 14:51:12 -0400175 write_extern_arg(out, arg, types);
176 }
177 if indirect_return {
178 if !efn.args.is_empty() {
179 write!(out, ", ");
180 }
181 write_return_type(out, &efn.ret);
182 write!(out, "*return$");
183 }
184 writeln!(out, ") noexcept {{");
185 write!(out, " ");
186 write_return_type(out, &efn.ret);
187 write!(out, "(*{}$)(", efn.ident);
188 for (i, arg) in efn.args.iter().enumerate() {
189 if i > 0 {
190 write!(out, ", ");
191 }
192 write_type(out, &arg.ty);
193 }
194 writeln!(out, ") = {};", efn.ident);
195 write!(out, " ");
196 if indirect_return {
197 write!(out, "new (return$) ");
198 write_type(out, efn.ret.as_ref().unwrap());
199 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800200 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400201 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800202 match ret {
203 Type::Ref(_) => write!(out, "&"),
204 Type::Str(_) => write!(out, "::rust::Str::Repr("),
205 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800206 }
David Tolnay7db73692019-10-20 14:51:12 -0400207 }
208 write!(out, "{}$(", efn.ident);
209 for (i, arg) in efn.args.iter().enumerate() {
210 if i > 0 {
211 write!(out, ", ");
212 }
213 if let Type::RustBox(_) = &arg.ty {
214 write_type(out, &arg.ty);
215 write!(out, "::from_raw({})", arg.ident);
216 } else if let Type::UniquePtr(_) = &arg.ty {
217 write_type(out, &arg.ty);
218 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800219 } else if arg.ty == RustString {
220 write!(out, "::rust::String(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400221 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800222 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400223 } else {
224 write!(out, "{}", arg.ident);
225 }
226 }
227 write!(out, ")");
228 match &efn.ret {
229 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
230 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800231 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400232 _ => {}
233 }
234 if indirect_return {
235 write!(out, ")");
236 }
237 writeln!(out, ";");
238 writeln!(out, "}}");
239}
240
241fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
242 write_extern_return_type(out, &efn.ret, types);
243 for name in out.namespace.clone() {
244 write!(out, "{}$", name);
245 }
David Tolnaye43b7372020-01-08 08:46:20 -0800246 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400247 for (i, arg) in efn.args.iter().enumerate() {
248 if i > 0 {
249 write!(out, ", ");
250 }
251 write_extern_arg(out, arg, types);
252 }
253 if efn
254 .ret
255 .as_ref()
256 .map_or(false, |ret| types.needs_indirect_abi(ret))
257 {
258 if !efn.args.is_empty() {
259 write!(out, ", ");
260 }
261 write_return_type(out, &efn.ret);
262 write!(out, "*return$");
263 }
264 writeln!(out, ") noexcept;");
265}
266
267fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
268 let indirect_return = efn
269 .ret
270 .as_ref()
271 .map_or(false, |ret| types.needs_indirect_abi(ret));
272 for line in efn.doc.to_string().lines() {
273 writeln!(out, "//{}", line);
274 }
275 write_return_type(out, &efn.ret);
276 write!(out, "{}(", efn.ident);
277 for (i, arg) in efn.args.iter().enumerate() {
278 if i > 0 {
279 write!(out, ", ");
280 }
281 write_type_space(out, &arg.ty);
282 write!(out, "{}", arg.ident);
283 }
284 write!(out, ") noexcept");
285 if out.header {
286 writeln!(out, ";");
287 } else {
288 writeln!(out, " {{");
289 write!(out, " ");
290 if indirect_return {
291 write!(out, "char return$[sizeof(");
292 write_type(out, efn.ret.as_ref().unwrap());
293 writeln!(out, ")];");
294 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800295 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400296 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800297 if let Type::Ref(_) = ret {
298 write!(out, "*");
299 }
David Tolnay7db73692019-10-20 14:51:12 -0400300 }
301 for name in out.namespace.clone() {
302 write!(out, "{}$", name);
303 }
David Tolnaye43b7372020-01-08 08:46:20 -0800304 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400305 for (i, arg) in efn.args.iter().enumerate() {
306 if i > 0 {
307 write!(out, ", ");
308 }
David Tolnaybaae4432020-03-01 20:20:10 -0800309 match &arg.ty {
310 Type::Str(_) => write!(out, "::rust::Str::Repr("),
311 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
312 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400313 }
314 write!(out, "{}", arg.ident);
David Tolnay17955e22020-01-20 17:58:24 -0800315 match arg.ty {
316 Type::RustBox(_) => write!(out, ".into_raw()"),
317 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800318 Type::Str(_) => write!(out, ")"),
David Tolnay17955e22020-01-20 17:58:24 -0800319 _ => {}
320 }
David Tolnay7db73692019-10-20 14:51:12 -0400321 }
322 if indirect_return {
323 if !efn.args.is_empty() {
324 write!(out, ", ");
325 }
326 write!(out, "reinterpret_cast<");
327 write_return_type(out, &efn.ret);
328 write!(out, "*>(return$)");
329 }
330 writeln!(out, ");");
331 if indirect_return {
332 write!(out, " return ");
333 write_type(out, efn.ret.as_ref().unwrap());
334 write!(out, "(*reinterpret_cast<");
335 write_return_type(out, &efn.ret);
336 writeln!(out, "*>(return$));");
337 }
338 writeln!(out, "}}");
339 }
340}
341
342fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
343 match ty {
344 None => write!(out, "void "),
345 Some(ty) => write_type_space(out, ty),
346 }
347}
348
349fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
350 match ty {
351 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
352 write_type_space(out, &ty.inner);
353 write!(out, "*");
354 }
David Tolnay4a441222020-01-25 16:24:27 -0800355 Some(Type::Ref(ty)) => {
356 if ty.mutability.is_none() {
357 write!(out, "const ");
358 }
359 write_type(out, &ty.inner);
360 write!(out, " *");
361 }
David Tolnay750755e2020-03-01 13:04:08 -0800362 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400363 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
364 _ => write_return_type(out, ty),
365 }
366}
367
368fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
369 match &arg.ty {
370 Type::RustBox(ty) | Type::UniquePtr(ty) => {
371 write_type_space(out, &ty.inner);
372 write!(out, "*");
373 }
David Tolnay750755e2020-03-01 13:04:08 -0800374 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400375 _ => write_type_space(out, &arg.ty),
376 }
377 if types.needs_indirect_abi(&arg.ty) {
378 write!(out, "*");
379 }
380 write!(out, "{}", arg.ident);
381}
382
383fn write_type(out: &mut OutFile, ty: &Type) {
384 match ty {
385 Type::Ident(ident) => match Atom::from(ident) {
386 Some(Bool) => write!(out, "bool"),
387 Some(U8) => write!(out, "uint8_t"),
388 Some(U16) => write!(out, "uint16_t"),
389 Some(U32) => write!(out, "uint32_t"),
390 Some(U64) => write!(out, "uint64_t"),
391 Some(Usize) => write!(out, "size_t"),
392 Some(I8) => write!(out, "int8_t"),
393 Some(I16) => write!(out, "int16_t"),
394 Some(I32) => write!(out, "int32_t"),
395 Some(I64) => write!(out, "int64_t"),
396 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800397 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800398 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400399 None => write!(out, "{}", ident),
400 },
401 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800402 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400403 write_type(out, &ty.inner);
404 write!(out, ">");
405 }
406 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800407 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400408 write_type(out, &ptr.inner);
409 write!(out, ">");
410 }
411 Type::Ref(r) => {
412 if r.mutability.is_none() {
413 write!(out, "const ");
414 }
415 write_type(out, &r.inner);
416 write!(out, " &");
417 }
418 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800419 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400420 }
421 }
422}
423
424fn write_type_space(out: &mut OutFile, ty: &Type) {
425 write_type(out, ty);
426 match ty {
427 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
428 Type::Ref(_) => {}
429 }
430}
431
432fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
433 fn allow_unique_ptr(ident: &Ident) -> bool {
434 Atom::from(ident).is_none()
435 }
436
437 out.begin_block("extern \"C\"");
438 for ty in types {
439 if let Type::RustBox(ty) = ty {
440 if let Type::Ident(inner) = &ty.inner {
441 out.next_section();
442 write_rust_box_extern(out, inner);
443 }
444 } else if let Type::UniquePtr(ptr) = ty {
445 if let Type::Ident(inner) = &ptr.inner {
446 if allow_unique_ptr(inner) {
447 out.next_section();
448 write_unique_ptr(out, inner);
449 }
450 }
451 }
452 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800453 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400454
David Tolnay750755e2020-03-01 13:04:08 -0800455 out.begin_block("namespace rust");
456 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400457 for ty in types {
458 if let Type::RustBox(ty) = ty {
459 if let Type::Ident(inner) = &ty.inner {
460 write_rust_box_impl(out, inner);
461 }
462 }
463 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800464 out.end_block("namespace cxxbridge01");
465 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400466}
467
468fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
469 let mut inner = String::new();
470 for name in &out.namespace {
471 inner += name;
472 inner += "::";
473 }
474 inner += &ident.to_string();
475 let instance = inner.replace("::", "$");
476
David Tolnaye43b7372020-01-08 08:46:20 -0800477 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
478 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400479 writeln!(
480 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800481 "void cxxbridge01$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400482 instance, inner,
483 );
484 writeln!(
485 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800486 "void cxxbridge01$box${}$set_raw(::rust::Box<{}> *ptr, {} *raw) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400487 instance, inner, inner
488 );
489 writeln!(
490 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800491 "void cxxbridge01$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400492 instance, inner,
493 );
494 writeln!(
495 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800496 "const {} *cxxbridge01$box${}$deref(const ::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400497 inner, instance, inner,
498 );
499 writeln!(
500 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800501 "{} *cxxbridge01$box${}$deref_mut(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400502 inner, instance, inner,
503 );
David Tolnaye43b7372020-01-08 08:46:20 -0800504 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400505}
506
507fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
508 let mut inner = String::new();
509 for name in &out.namespace {
510 inner += name;
511 inner += "::";
512 }
513 inner += &ident.to_string();
514 let instance = inner.replace("::", "$");
515
516 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800517 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800518 writeln!(out, " return cxxbridge01$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400519 writeln!(out, "}}");
520
521 writeln!(out, "template <>");
522 writeln!(
523 out,
David Tolnay324437a2020-03-01 13:02:24 -0800524 "void Box<{}>::set_raw({} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400525 inner, inner,
526 );
527 writeln!(
528 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800529 " return cxxbridge01$box${}$set_raw(this, raw);",
David Tolnay7db73692019-10-20 14:51:12 -0400530 instance
531 );
532 writeln!(out, "}}");
533
534 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800535 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800536 writeln!(out, " return cxxbridge01$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400537 writeln!(out, "}}");
538
539 writeln!(out, "template <>");
540 writeln!(
541 out,
David Tolnay324437a2020-03-01 13:02:24 -0800542 "const {} *Box<{}>::deref() const noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400543 inner, inner,
544 );
David Tolnay9081beb2020-03-01 19:51:46 -0800545 writeln!(out, " return cxxbridge01$box${}$deref(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400546 writeln!(out, "}}");
547
548 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800549 writeln!(out, "{} *Box<{}>::deref_mut() noexcept {{", inner, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400550 writeln!(
551 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800552 " return cxxbridge01$box${}$deref_mut(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400553 instance
554 );
555 writeln!(out, "}}");
556}
557
558fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
559 let mut inner = String::new();
560 for name in &out.namespace {
561 inner += name;
562 inner += "::";
563 }
564 inner += &ident.to_string();
565 let instance = inner.replace("::", "$");
566
David Tolnaye43b7372020-01-08 08:46:20 -0800567 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
568 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400569 writeln!(
570 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800571 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400572 inner,
573 );
574 writeln!(
575 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800576 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400577 inner,
578 );
579 writeln!(
580 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800581 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400582 instance, inner,
583 );
David Tolnay7e219b82020-03-01 13:14:51 -0800584 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400585 writeln!(out, "}}");
586 writeln!(
587 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800588 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400589 instance, inner, inner,
590 );
591 writeln!(
592 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800593 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400594 inner, inner,
595 );
596 writeln!(out, "}}");
597 writeln!(
598 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800599 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400600 instance, inner, inner,
601 );
David Tolnay7e219b82020-03-01 13:14:51 -0800602 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400603 writeln!(out, "}}");
604 writeln!(
605 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800606 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400607 inner, instance, inner,
608 );
609 writeln!(out, " return ptr.get();");
610 writeln!(out, "}}");
611 writeln!(
612 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800613 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400614 inner, instance, inner,
615 );
616 writeln!(out, " return ptr.release();");
617 writeln!(out, "}}");
618 writeln!(
619 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800620 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400621 instance, inner,
622 );
623 writeln!(out, " ptr->~unique_ptr();");
624 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800625 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400626}