Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #include "rs_bindings_from_cc/rs_src_code_gen.h" |
| 6 | |
| 7 | #include <stddef.h> |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "rs_bindings_from_cc/ir.h" |
| 12 | #include "third_party/absl/strings/string_view.h" |
| 13 | #include "third_party/json/src/json.hpp" |
| 14 | |
| 15 | namespace rs_bindings_from_cc { |
| 16 | |
| 17 | struct FfiU8SliceBox { |
| 18 | const char* ptr; |
| 19 | size_t size; |
| 20 | }; |
| 21 | |
| 22 | struct FfiU8Slice { |
| 23 | const char* ptr; |
| 24 | size_t size; |
| 25 | }; |
| 26 | |
| 27 | static FfiU8Slice MakeFfiU8Slice(absl::string_view s) { |
| 28 | FfiU8Slice result; |
| 29 | result.ptr = s.data(); |
| 30 | result.size = s.size(); |
| 31 | return result; |
| 32 | } |
| 33 | |
| 34 | // This function is implemented in Rust. |
| 35 | extern "C" FfiU8SliceBox GenerateRustApiImpl(FfiU8Slice); |
| 36 | |
| 37 | // This function is implemented in Rust. |
| 38 | extern "C" void FreeFfiU8SliceBox(FfiU8SliceBox); |
| 39 | |
| 40 | std::string GenerateRustApi(const IR& ir) { |
| 41 | std::string json = ir.ToJson().dump(); |
| 42 | FfiU8SliceBox slice_box = GenerateRustApiImpl(MakeFfiU8Slice(json)); |
| 43 | std::string rs_api(slice_box.ptr, slice_box.size); |
| 44 | FreeFfiU8SliceBox(slice_box); |
| 45 | return rs_api; |
| 46 | } |
| 47 | |
| 48 | } // namespace rs_bindings_from_cc |