blob: e32c3a6e8568792aa6351124139e494e28357950 [file] [log] [blame]
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "rs_bindings_from_cc/src_code_gen.h"
#include <string>
#include "common/ffi_types.h"
#include "rs_bindings_from_cc/ir.h"
#include "clang/Format/Format.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/JSON.h"
namespace crubit {
// FFI equivalent of `Bindings`.
struct FfiBindings {
FfiU8SliceBox rs_api;
FfiU8SliceBox rs_api_impl;
};
// This function is implemented in Rust.
extern "C" FfiBindings GenerateBindingsImpl(FfiU8Slice json,
FfiU8Slice crubit_support_path,
FfiU8Slice rustfmt_exe_path,
FfiU8Slice rustfmt_config_path);
// Creates `Bindings` instance from copied data from `ffi_bindings`.
static Bindings MakeBindingsFromFfiBindings(const FfiBindings& ffi_bindings) {
Bindings bindings;
const FfiU8SliceBox& rs_api = ffi_bindings.rs_api;
const FfiU8SliceBox& rs_api_impl = ffi_bindings.rs_api_impl;
bindings.rs_api = std::string(rs_api.ptr, rs_api.size);
std::string impl{rs_api_impl.ptr, rs_api_impl.size};
bindings.rs_api_impl = *clang::tooling::applyAllReplacements(
impl,
clang::format::reformat(
clang::format::getGoogleStyle(clang::format::FormatStyle::LK_Cpp),
impl, clang::tooling::Range(0, impl.size()), "<stdin>"));
return bindings;
}
// Deallocates given `ffi_bindings` instance that was created in Rust.
static void FreeFfiBindings(FfiBindings ffi_bindings) {
FreeFfiU8SliceBox(ffi_bindings.rs_api);
FreeFfiU8SliceBox(ffi_bindings.rs_api_impl);
}
Bindings GenerateBindings(const IR& ir, absl::string_view crubit_support_path,
absl::string_view rustfmt_exe_path,
absl::string_view rustfmt_config_path) {
std::string json = llvm::formatv("{0}", ir.ToJson());
FfiBindings ffi_bindings = GenerateBindingsImpl(
MakeFfiU8Slice(json), MakeFfiU8Slice(crubit_support_path),
MakeFfiU8Slice(rustfmt_exe_path), MakeFfiU8Slice(rustfmt_config_path));
Bindings bindings = MakeBindingsFromFfiBindings(ffi_bindings);
FreeFfiBindings(ffi_bindings);
return bindings;
}
} // namespace crubit