Marcel Hlopko | b10e49b | 2022-05-09 03:57:32 -0700 | [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/collect_instantiations.h" |
| 6 | |
| 7 | #include "absl/status/statusor.h" |
| 8 | #include "absl/types/span.h" |
Marcel Hlopko | b10e49b | 2022-05-09 03:57:32 -0700 | [diff] [blame] | 9 | #include "common/ffi_types.h" |
| 10 | #include "llvm/Support/FormatVariadic.h" |
| 11 | #include "llvm/Support/JSON.h" |
| 12 | |
| 13 | // This function is implemented in Rust. |
| 14 | extern "C" crubit::FfiU8SliceBox CollectInstantiationsImpl( |
| 15 | crubit::FfiU8Slice json); |
| 16 | |
| 17 | namespace crubit { |
| 18 | |
| 19 | absl::StatusOr<std::vector<std::string>> CollectInstantiations( |
| 20 | absl::Span<const std::string> rust_sources) { |
| 21 | llvm::json::Value rust_sources_json = llvm::json::Array(rust_sources); |
| 22 | std::string json = llvm::formatv("{0}", rust_sources_json); |
| 23 | FfiU8SliceBox result = CollectInstantiationsImpl(MakeFfiU8Slice(json)); |
| 24 | std::string result_string = std::string(result.ptr, result.size); |
| 25 | llvm::Expected<llvm::json::Value> expected_instantiations = |
| 26 | llvm::json::parse(result_string); |
| 27 | if (auto error = expected_instantiations.takeError()) { |
| 28 | return absl::InternalError(llvm::toString(std::move(error))); |
| 29 | } |
| 30 | |
| 31 | llvm::json::Value instantiations = *expected_instantiations; |
| 32 | FreeFfiU8SliceBox(result); |
| 33 | std::vector<std::string> instantiations_vector; |
| 34 | llvm::json::Path::Root root; |
| 35 | if (llvm::json::fromJSON(instantiations, instantiations_vector, root)) { |
| 36 | return instantiations_vector; |
| 37 | } |
| 38 | return absl::InternalError(llvm::toString(root.getError())); |
| 39 | } |
| 40 | |
| 41 | } // namespace crubit |