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 | |
Marcel Hlopko | 3962d18 | 2021-08-18 10:16:41 +0000 | [diff] [blame] | 5 | #include "rs_bindings_from_cc/src_code_gen.h" |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 6 | |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Marcel Hlopko | 65978eb | 2022-04-08 07:40:18 -0700 | [diff] [blame] | 9 | #include "common/ffi_types.h" |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 10 | #include "rs_bindings_from_cc/ir.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame^] | 11 | #include "clang/Format/Format.h" |
| 12 | #include "llvm/Support/FormatVariadic.h" |
| 13 | #include "llvm/Support/JSON.h" |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 14 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 15 | namespace crubit { |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 16 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 17 | // FFI equivalent of `Bindings`. |
| 18 | struct FfiBindings { |
| 19 | FfiU8SliceBox rs_api; |
| 20 | FfiU8SliceBox rs_api_impl; |
| 21 | }; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 22 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 23 | // This function is implemented in Rust. |
| 24 | extern "C" FfiBindings GenerateBindingsImpl(FfiU8Slice json); |
| 25 | |
| 26 | // Creates `Bindings` instance from copied data from `ffi_bindings`. |
Marcel Hlopko | e936aac | 2021-08-24 20:52:27 +0000 | [diff] [blame] | 27 | static Bindings MakeBindingsFromFfiBindings(const FfiBindings& ffi_bindings) { |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 28 | Bindings bindings; |
| 29 | |
Marcel Hlopko | e936aac | 2021-08-24 20:52:27 +0000 | [diff] [blame] | 30 | const FfiU8SliceBox& rs_api = ffi_bindings.rs_api; |
| 31 | const FfiU8SliceBox& rs_api_impl = ffi_bindings.rs_api_impl; |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 32 | |
| 33 | bindings.rs_api = std::string(rs_api.ptr, rs_api.size); |
Michael Forster | f74c149 | 2021-09-17 09:30:10 +0000 | [diff] [blame] | 34 | |
| 35 | std::string impl{rs_api_impl.ptr, rs_api_impl.size}; |
| 36 | bindings.rs_api_impl = *clang::tooling::applyAllReplacements( |
| 37 | impl, |
| 38 | clang::format::reformat( |
| 39 | clang::format::getGoogleStyle(clang::format::FormatStyle::LK_Cpp), |
| 40 | impl, clang::tooling::Range(0, impl.size()), "<stdin>")); |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 41 | |
| 42 | return bindings; |
| 43 | } |
| 44 | |
| 45 | // Deallocates given `ffi_bindings` instance that was created in Rust. |
| 46 | static void FreeFfiBindings(FfiBindings ffi_bindings) { |
| 47 | FreeFfiU8SliceBox(ffi_bindings.rs_api); |
| 48 | FreeFfiU8SliceBox(ffi_bindings.rs_api_impl); |
| 49 | } |
| 50 | |
Marcel Hlopko | e936aac | 2021-08-24 20:52:27 +0000 | [diff] [blame] | 51 | Bindings GenerateBindings(const IR& ir) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 52 | std::string json = llvm::formatv("{0}", ir.ToJson()); |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 53 | FfiBindings ffi_bindings = GenerateBindingsImpl(MakeFfiU8Slice(json)); |
| 54 | Bindings bindings = MakeBindingsFromFfiBindings(ffi_bindings); |
| 55 | FreeFfiBindings(ffi_bindings); |
| 56 | return bindings; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 59 | } // namespace crubit |