blob: 32b1e00c2f1bc3dae9c98a647b112e2a748057a0 [file] [log] [blame]
Marcel Hlopko42abfc82021-08-09 07:03:17 +00001// 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
15namespace rs_bindings_from_cc {
16
17struct FfiU8SliceBox {
18 const char* ptr;
19 size_t size;
20};
21
22struct FfiU8Slice {
23 const char* ptr;
24 size_t size;
25};
26
27static 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.
35extern "C" FfiU8SliceBox GenerateRustApiImpl(FfiU8Slice);
36
37// This function is implemented in Rust.
38extern "C" void FreeFfiU8SliceBox(FfiU8SliceBox);
39
40std::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