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/ir.h" |
| 6 | |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 7 | #include <optional> |
Marcel Hlopko | 20f4ce4 | 2021-11-02 08:20:00 +0000 | [diff] [blame] | 8 | #include <ostream> |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 9 | #include <string> |
Marcel Hlopko | 20f4ce4 | 2021-11-02 08:20:00 +0000 | [diff] [blame] | 10 | #include <utility> |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 11 | #include <variant> |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 14 | #include "absl/log/check.h" |
Dmitri Gribenko | 785831e | 2023-07-14 06:47:36 -0700 | [diff] [blame] | 15 | #include "absl/strings/str_cat.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 16 | #include "absl/strings/string_view.h" |
Dmitri Gribenko | 785831e | 2023-07-14 06:47:36 -0700 | [diff] [blame] | 17 | #include "common/string_type.h" |
Marco Poletti | c61bcc4 | 2022-04-08 12:54:30 -0700 | [diff] [blame] | 18 | #include "common/strong_int.h" |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 19 | #include "clang/AST/Type.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 20 | #include "llvm/Support/JSON.h" |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 21 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 22 | namespace crubit { |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 23 | |
Googler | 7ce38c9 | 2021-12-02 07:50:52 +0000 | [diff] [blame] | 24 | template <class T> |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 25 | llvm::json::Value toJSON(const T& t) { |
| 26 | return t.ToJson(); |
Googler | 7ce38c9 | 2021-12-02 07:50:52 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 29 | template <typename TTag, typename TInt> |
Marco Poletti | c61bcc4 | 2022-04-08 12:54:30 -0700 | [diff] [blame] | 30 | llvm::json::Value toJSON(const crubit::StrongInt<TTag, TInt> strong_int) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 31 | return llvm::json::Value(strong_int.value()); |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 34 | template <typename TTag> |
Marco Poletti | c61bcc4 | 2022-04-08 12:54:30 -0700 | [diff] [blame] | 35 | llvm::json::Value toJSON(const crubit::StringType<TTag> string_type) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 36 | return llvm::json::Value(string_type.value()); |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 39 | template <class T> |
| 40 | llvm::json::Value toJSON(const absl::StatusOr<T>& t) { |
| 41 | if (t.ok()) { |
| 42 | return llvm::json::Object{{"Ok", *t}}; |
| 43 | } |
| 44 | return llvm::json::Object{{"Err", std::string(t.status().message())}}; |
| 45 | } |
| 46 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 47 | llvm::json::Value HeaderName::ToJson() const { |
| 48 | return llvm::json::Object{ |
| 49 | {"name", name_}, |
| 50 | }; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Marcel Hlopko | af682a0 | 2022-04-08 07:27:14 -0700 | [diff] [blame] | 53 | llvm::json::Value LifetimeName::ToJson() const { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 54 | return llvm::json::Object{ |
| 55 | {"name", name}, |
| 56 | {"id", id}, |
| 57 | }; |
| 58 | } |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 59 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 60 | llvm::json::Value RsType::ToJson() const { |
| 61 | return llvm::json::Object{ |
Dmitri Gribenko | 57ddcf9 | 2022-07-20 10:24:33 -0700 | [diff] [blame] | 62 | {"name", decl_id.has_value() ? llvm::json::Value(nullptr) |
| 63 | : llvm::json::Value(name)}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 64 | {"lifetime_args", lifetime_args}, |
| 65 | {"type_args", type_args}, |
| 66 | {"decl_id", decl_id}, |
| 67 | }; |
| 68 | } |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 69 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 70 | llvm::json::Value CcType::ToJson() const { |
| 71 | return llvm::json::Object{ |
Dmitri Gribenko | 57ddcf9 | 2022-07-20 10:24:33 -0700 | [diff] [blame] | 72 | {"name", decl_id.has_value() ? llvm::json::Value(nullptr) |
| 73 | : llvm::json::Value(name)}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 74 | {"is_const", is_const}, |
| 75 | {"type_args", type_args}, |
| 76 | {"decl_id", decl_id}, |
| 77 | }; |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 80 | namespace { |
| 81 | enum class ValueCategory { kLvalue, kRvalue }; |
| 82 | |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 83 | MappedType PointerOrReferenceTo( |
| 84 | MappedType pointee_type, absl::string_view cc_ptr_name, |
| 85 | ValueCategory value_category, std::optional<LifetimeId> lifetime, |
| 86 | std::optional<clang::RefQualifierKind> ref_qualifier_kind, bool nullable) { |
Googler | 454f265 | 2021-12-06 12:53:12 +0000 | [diff] [blame] | 87 | bool has_lifetime = lifetime.has_value(); |
Devin Jeanpierre | 7039390 | 2022-03-15 13:22:59 +0000 | [diff] [blame] | 88 | absl::string_view rs_name; |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 89 | if (value_category == ValueCategory::kLvalue) { |
| 90 | if (has_lifetime) { |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 91 | if (ref_qualifier_kind.has_value() && |
| 92 | ref_qualifier_kind.value() == clang::RefQualifierKind::RQ_RValue) { |
| 93 | rs_name = pointee_type.cc_type.is_const ? internal::kRustRvalueRefConst |
| 94 | : internal::kRustRvalueRefMut; |
| 95 | } else { |
| 96 | rs_name = pointee_type.cc_type.is_const ? internal::kRustRefConst |
| 97 | : internal::kRustRefMut; |
| 98 | } |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 99 | } else { |
| 100 | rs_name = pointee_type.cc_type.is_const ? internal::kRustPtrConst |
| 101 | : internal::kRustPtrMut; |
| 102 | } |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 103 | } else { |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 104 | CHECK(has_lifetime); |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 105 | rs_name = pointee_type.cc_type.is_const ? internal::kRustRvalueRefConst |
| 106 | : internal::kRustRvalueRefMut; |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 107 | } |
| 108 | auto pointer_type = |
Devin Jeanpierre | 7039390 | 2022-03-15 13:22:59 +0000 | [diff] [blame] | 109 | MappedType::Simple(std::string(rs_name), std::string(cc_ptr_name)); |
Googler | 454f265 | 2021-12-06 12:53:12 +0000 | [diff] [blame] | 110 | if (has_lifetime) { |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 111 | pointer_type.rs_type.lifetime_args.push_back(*std::move(lifetime)); |
| 112 | } |
| 113 | pointer_type.rs_type.type_args.push_back(std::move(pointee_type.rs_type)); |
Googler | 454f265 | 2021-12-06 12:53:12 +0000 | [diff] [blame] | 114 | if (has_lifetime && nullable) { |
| 115 | pointer_type.rs_type = |
| 116 | RsType{.name = "Option", .type_args = {pointer_type.rs_type}}; |
| 117 | } |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 118 | pointer_type.cc_type.type_args.push_back(std::move(pointee_type.cc_type)); |
| 119 | return pointer_type; |
| 120 | } |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 121 | } // namespace |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 122 | |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 123 | MappedType MappedType::PointerTo( |
| 124 | MappedType pointee_type, std::optional<LifetimeId> lifetime, |
| 125 | std::optional<clang::RefQualifierKind> ref_qualifier_kind, bool nullable) { |
Devin Jeanpierre | 7039390 | 2022-03-15 13:22:59 +0000 | [diff] [blame] | 126 | return PointerOrReferenceTo(std::move(pointee_type), internal::kCcPtr, |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 127 | ValueCategory::kLvalue, lifetime, |
| 128 | ref_qualifier_kind, nullable); |
Devin Jeanpierre | 7039390 | 2022-03-15 13:22:59 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 131 | MappedType MappedType::LValueReferenceTo(MappedType pointee_type, |
| 132 | std::optional<LifetimeId> lifetime) { |
Devin Jeanpierre | 7039390 | 2022-03-15 13:22:59 +0000 | [diff] [blame] | 133 | return PointerOrReferenceTo(std::move(pointee_type), internal::kCcLValueRef, |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 134 | ValueCategory::kLvalue, lifetime, |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 135 | /*ref_qualifier_kind=*/std::nullopt, |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 136 | /*nullable=*/false); |
| 137 | } |
| 138 | |
| 139 | MappedType MappedType::RValueReferenceTo(MappedType pointee_type, |
| 140 | LifetimeId lifetime) { |
| 141 | return PointerOrReferenceTo(std::move(pointee_type), internal::kCcRValueRef, |
| 142 | ValueCategory::kRvalue, lifetime, |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 143 | /*ref_qualifier_kind=*/std::nullopt, |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 144 | /*nullable=*/false); |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 147 | MappedType MappedType::FuncPtr(absl::string_view cc_call_conv, |
| 148 | absl::string_view rs_abi, |
| 149 | std::optional<LifetimeId> lifetime, |
| 150 | MappedType return_type, |
| 151 | std::vector<MappedType> param_types) { |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 152 | MappedType result = FuncRef(cc_call_conv, rs_abi, lifetime, |
| 153 | std::move(return_type), std::move(param_types)); |
| 154 | |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 155 | CHECK_EQ(result.cc_type.name, internal::kCcLValueRef); |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 156 | result.cc_type.name = std::string(internal::kCcPtr); |
| 157 | |
| 158 | RsType rs_func_ptr_type = std::move(result.rs_type); |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 159 | CHECK(rs_func_ptr_type.name.substr(0, internal::kRustFuncPtr.length()) == |
| 160 | internal::kRustFuncPtr); |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 161 | result.rs_type = |
| 162 | RsType{.name = "Option", .type_args = {std::move(rs_func_ptr_type)}}; |
| 163 | |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | MappedType MappedType::FuncRef(absl::string_view cc_call_conv, |
| 168 | absl::string_view rs_abi, |
| 169 | std::optional<LifetimeId> lifetime, |
| 170 | MappedType return_type, |
| 171 | std::vector<MappedType> param_types) { |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 172 | std::vector<MappedType> type_args = std::move(param_types); |
| 173 | type_args.push_back(std::move(return_type)); |
| 174 | |
| 175 | std::vector<CcType> cc_type_args; |
| 176 | std::vector<RsType> rs_type_args; |
| 177 | cc_type_args.reserve(type_args.size()); |
| 178 | rs_type_args.reserve(type_args.size()); |
| 179 | for (MappedType& type_arg : type_args) { |
| 180 | cc_type_args.push_back(std::move(type_arg.cc_type)); |
| 181 | rs_type_args.push_back(std::move(type_arg.rs_type)); |
| 182 | } |
| 183 | |
| 184 | CcType cc_func_value_type = CcType{ |
| 185 | .name = absl::StrCat(internal::kCcFuncValue, " ", cc_call_conv), |
| 186 | .type_args = std::move(cc_type_args), |
| 187 | }; |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 188 | CcType cc_func_ref_type = CcType{.name = std::string(internal::kCcLValueRef), |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 189 | .type_args = {cc_func_value_type}}; |
| 190 | |
| 191 | // Rust cannot express a function *value* type, only function pointer types. |
| 192 | RsType rs_func_ptr_type = RsType{ |
| 193 | .name = absl::StrCat(internal::kRustFuncPtr, " ", rs_abi), |
| 194 | .type_args = std::move(rs_type_args), |
| 195 | }; |
| 196 | if (lifetime.has_value()) |
| 197 | rs_func_ptr_type.lifetime_args.push_back(*std::move(lifetime)); |
| 198 | |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 199 | return MappedType{ |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 200 | .rs_type = std::move(rs_func_ptr_type), |
| 201 | .cc_type = std::move(cc_func_ref_type), |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 202 | }; |
| 203 | } |
| 204 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 205 | llvm::json::Value MappedType::ToJson() const { |
| 206 | return llvm::json::Object{ |
| 207 | {"rs_type", rs_type}, |
| 208 | {"cc_type", cc_type}, |
| 209 | }; |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 212 | llvm::json::Value Identifier::ToJson() const { |
| 213 | return llvm::json::Object{ |
| 214 | {"identifier", identifier_}, |
| 215 | }; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 218 | llvm::json::Value IntegerConstant::ToJson() const { |
| 219 | return llvm::json::Object{ |
| 220 | {"is_negative", is_negative_}, |
| 221 | {"wrapped_value", wrapped_value_}, |
| 222 | }; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 225 | llvm::json::Value Operator::ToJson() const { |
| 226 | return llvm::json::Object{ |
| 227 | {"name", name_}, |
| 228 | }; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 231 | static std::string SpecialNameToString(SpecialName special_name) { |
| 232 | switch (special_name) { |
| 233 | case SpecialName::kDestructor: |
| 234 | return "Destructor"; |
| 235 | case SpecialName::kConstructor: |
| 236 | return "Constructor"; |
| 237 | } |
| 238 | } |
| 239 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 240 | llvm::json::Value toJSON(const UnqualifiedIdentifier& unqualified_identifier) { |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 241 | if (auto* id = std::get_if<Identifier>(&unqualified_identifier)) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 242 | return llvm::json::Object{ |
| 243 | {"Identifier", *id}, |
| 244 | }; |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 245 | } else if (auto* op = std::get_if<Operator>(&unqualified_identifier)) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 246 | return llvm::json::Object{ |
| 247 | {"Operator", *op}, |
| 248 | }; |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 249 | } else { |
| 250 | SpecialName special_name = std::get<SpecialName>(unqualified_identifier); |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 251 | return llvm::json::Object{ |
| 252 | {SpecialNameToString(special_name), nullptr}, |
| 253 | }; |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 254 | } |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 257 | llvm::json::Value FuncParam::ToJson() const { |
| 258 | return llvm::json::Object{ |
| 259 | {"type", type}, |
| 260 | {"identifier", identifier}, |
| 261 | }; |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 264 | std::ostream& operator<<(std::ostream& o, const SpecialName& special_name) { |
| 265 | return o << SpecialNameToString(special_name); |
| 266 | } |
| 267 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 268 | llvm::json::Value MemberFuncMetadata::InstanceMethodMetadata::ToJson() const { |
| 269 | const char* reference_str = nullptr; |
| 270 | switch (reference) { |
| 271 | case MemberFuncMetadata::kLValue: |
| 272 | reference_str = "LValue"; |
| 273 | break; |
| 274 | case MemberFuncMetadata::kRValue: |
| 275 | reference_str = "RValue"; |
| 276 | break; |
| 277 | case MemberFuncMetadata::kUnqualified: |
| 278 | reference_str = "Unqualified"; |
| 279 | break; |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 282 | return llvm::json::Object{ |
| 283 | {"reference", reference_str}, |
| 284 | {"is_const", is_const}, |
| 285 | {"is_virtual", is_virtual}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 286 | }; |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 289 | llvm::json::Value MemberFuncMetadata::ToJson() const { |
| 290 | return llvm::json::Object{ |
| 291 | {"record_id", record_id}, |
| 292 | {"instance_method_metadata", instance_method_metadata}, |
| 293 | }; |
| 294 | } |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 295 | |
Devin Jeanpierre | 93e800e | 2023-05-08 12:55:53 -0700 | [diff] [blame] | 296 | llvm::json::Value TypeMapOverride::ToJson() const { |
| 297 | llvm::json::Object override{ |
Devin Jeanpierre | b7f8e28 | 2023-05-26 16:03:12 -0700 | [diff] [blame] | 298 | {"rs_name", rs_name}, |
| 299 | {"cc_name", cc_name}, |
Devin Jeanpierre | 93e800e | 2023-05-08 12:55:53 -0700 | [diff] [blame] | 300 | {"owning_target", owning_target}, |
Devin Jeanpierre | c850c77 | 2023-05-30 17:14:19 -0700 | [diff] [blame] | 301 | {"is_same_abi", is_same_abi}, |
Devin Jeanpierre | 93e800e | 2023-05-08 12:55:53 -0700 | [diff] [blame] | 302 | {"id", id}, |
| 303 | }; |
Devin Jeanpierre | 1e2d324 | 2023-05-15 12:21:59 -0700 | [diff] [blame] | 304 | if (size_align.has_value()) { |
| 305 | override.insert({"size_align", size_align->ToJson()}); |
| 306 | } |
Devin Jeanpierre | 93e800e | 2023-05-08 12:55:53 -0700 | [diff] [blame] | 307 | |
| 308 | return llvm::json::Object{ |
| 309 | {"TypeMapOverride", std::move(override)}, |
| 310 | }; |
| 311 | } |
| 312 | |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 313 | llvm::json::Value UseMod::ToJson() const { |
| 314 | llvm::json::Object use_mod{ |
| 315 | {"path", path}, |
| 316 | {"mod_name", mod_name}, |
| 317 | {"id", id}, |
| 318 | }; |
| 319 | |
| 320 | return llvm::json::Object{ |
| 321 | {"UseMod", std::move(use_mod)}, |
| 322 | }; |
| 323 | } |
| 324 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 325 | llvm::json::Value Func::ToJson() const { |
| 326 | llvm::json::Object func{ |
| 327 | {"name", name}, |
| 328 | {"owning_target", owning_target}, |
| 329 | {"doc_comment", doc_comment}, |
| 330 | {"mangled_name", mangled_name}, |
| 331 | {"return_type", return_type}, |
| 332 | {"params", params}, |
| 333 | {"lifetime_params", lifetime_params}, |
| 334 | {"is_inline", is_inline}, |
| 335 | {"member_func_metadata", member_func_metadata}, |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 336 | {"has_c_calling_convention", has_c_calling_convention}, |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 337 | {"is_member_or_descendant_of_class_template", |
| 338 | is_member_or_descendant_of_class_template}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 339 | {"source_loc", source_loc}, |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 340 | {"id", id}, |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 341 | {"enclosing_namespace_id", enclosing_namespace_id}, |
Michael VanBemmel | 7a4d4c0 | 2022-07-27 13:21:47 -0700 | [diff] [blame] | 342 | {"adl_enclosing_record", adl_enclosing_record}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | return llvm::json::Object{ |
| 346 | {"Func", std::move(func)}, |
| 347 | }; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Googler | 2294c70 | 2021-09-17 07:32:07 +0000 | [diff] [blame] | 350 | static std::string AccessToString(AccessSpecifier access) { |
| 351 | switch (access) { |
| 352 | case kPublic: |
| 353 | return "Public"; |
| 354 | case kProtected: |
| 355 | return "Protected"; |
| 356 | case kPrivate: |
| 357 | return "Private"; |
| 358 | } |
| 359 | } |
| 360 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 361 | std::ostream& operator<<(std::ostream& o, const AccessSpecifier& access) { |
| 362 | return o << AccessToString(access); |
| 363 | } |
| 364 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 365 | llvm::json::Value Field::ToJson() const { |
| 366 | return llvm::json::Object{ |
| 367 | {"identifier", identifier}, |
| 368 | {"doc_comment", doc_comment}, |
| 369 | {"type", type}, |
| 370 | {"access", AccessToString(access)}, |
| 371 | {"offset", offset}, |
Lukasz Anforowicz | 5765bb8 | 2022-05-17 17:21:06 -0700 | [diff] [blame] | 372 | {"size", size}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 373 | {"is_no_unique_address", is_no_unique_address}, |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 374 | {"is_bitfield", is_bitfield}, |
Kinuko Yasuda | 6ff59f1 | 2022-08-11 08:41:45 -0700 | [diff] [blame] | 375 | {"is_inheritable", is_inheritable}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 376 | }; |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 379 | llvm::json::Value toJSON(const SpecialMemberFunc& f) { |
| 380 | switch (f) { |
| 381 | case SpecialMemberFunc::kTrivial: |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 382 | return "Trivial"; |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 383 | case SpecialMemberFunc::kNontrivialMembers: |
Devin Jeanpierre | be2f33b | 2021-10-21 12:54:19 +0000 | [diff] [blame] | 384 | return "NontrivialMembers"; |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 385 | case SpecialMemberFunc::kNontrivialUserDefined: |
Devin Jeanpierre | 7b62e95 | 2021-12-08 21:43:30 +0000 | [diff] [blame] | 386 | return "NontrivialUserDefined"; |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 387 | case SpecialMemberFunc::kUnavailable: |
| 388 | return "Unavailable"; |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 392 | llvm::json::Value BaseClass::ToJson() const { |
| 393 | return llvm::json::Object{ |
| 394 | {"base_record_id", base_record_id}, |
| 395 | {"offset", offset}, |
| 396 | }; |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 397 | } |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 398 | |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 399 | static std::string RecordTypeToString(RecordType record_type) { |
| 400 | switch (record_type) { |
| 401 | case kStruct: |
| 402 | return "Struct"; |
| 403 | case kUnion: |
| 404 | return "Union"; |
| 405 | case kClass: |
| 406 | return "Class"; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | std::ostream& operator<<(std::ostream& o, const RecordType& record_type) { |
| 411 | return o << RecordTypeToString(record_type); |
| 412 | } |
| 413 | |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 414 | llvm::json::Value IncompleteRecord::ToJson() const { |
| 415 | llvm::json::Object record{ |
| 416 | {"cc_name", cc_name}, |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 417 | {"rs_name", rs_name}, |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 418 | {"id", id}, |
| 419 | {"owning_target", owning_target}, |
Lukasz Anforowicz | 8dd5179 | 2022-08-31 10:11:17 -0700 | [diff] [blame] | 420 | {"record_type", RecordTypeToString(record_type)}, |
Lukasz Anforowicz | 49dbeea | 2022-09-16 07:31:49 -0700 | [diff] [blame] | 421 | {"enclosing_namespace_id", enclosing_namespace_id}, |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | return llvm::json::Object{ |
| 425 | {"IncompleteRecord", std::move(record)}, |
| 426 | }; |
| 427 | } |
| 428 | |
Devin Jeanpierre | 1e2d324 | 2023-05-15 12:21:59 -0700 | [diff] [blame] | 429 | llvm::json::Value SizeAlign::ToJson() const { |
| 430 | return llvm::json::Object{ |
| 431 | {"size", size}, |
| 432 | {"alignment", alignment}, |
| 433 | }; |
| 434 | } |
| 435 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 436 | llvm::json::Value Record::ToJson() const { |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 437 | std::vector<llvm::json::Value> json_item_ids; |
| 438 | json_item_ids.reserve(child_item_ids.size()); |
| 439 | for (const auto& id : child_item_ids) { |
| 440 | json_item_ids.push_back(id.value()); |
| 441 | } |
| 442 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 443 | llvm::json::Object record{ |
| 444 | {"rs_name", rs_name}, |
| 445 | {"cc_name", cc_name}, |
Lukasz Anforowicz | 3f13397 | 2022-09-01 09:01:02 -0700 | [diff] [blame] | 446 | {"mangled_cc_name", mangled_cc_name}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 447 | {"id", id}, |
| 448 | {"owning_target", owning_target}, |
Devin Jeanpierre | 3f0d068 | 2023-04-13 14:47:28 -0700 | [diff] [blame] | 449 | {"defining_target", defining_target}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 450 | {"doc_comment", doc_comment}, |
Googler | afd18fb | 2023-01-25 12:55:07 -0800 | [diff] [blame] | 451 | {"source_loc", source_loc}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 452 | {"unambiguous_public_bases", unambiguous_public_bases}, |
| 453 | {"fields", fields}, |
| 454 | {"lifetime_params", lifetime_params}, |
Devin Jeanpierre | 1e2d324 | 2023-05-15 12:21:59 -0700 | [diff] [blame] | 455 | {"size_align", size_align.ToJson()}, |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 456 | {"is_derived_class", is_derived_class}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 457 | {"override_alignment", override_alignment}, |
| 458 | {"copy_constructor", copy_constructor}, |
| 459 | {"move_constructor", move_constructor}, |
| 460 | {"destructor", destructor}, |
| 461 | {"is_trivial_abi", is_trivial_abi}, |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 462 | {"is_inheritable", is_inheritable}, |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 463 | {"is_abstract", is_abstract}, |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 464 | {"record_type", RecordTypeToString(record_type)}, |
Devin Jeanpierre | a2be2a2 | 2022-05-18 18:59:05 -0700 | [diff] [blame] | 465 | {"is_aggregate", is_aggregate}, |
Kinuko Yasuda | 8dd8464 | 2022-08-17 09:19:47 -0700 | [diff] [blame] | 466 | {"is_anon_record_with_typedef", is_anon_record_with_typedef}, |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 467 | {"child_item_ids", std::move(json_item_ids)}, |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 468 | {"enclosing_namespace_id", enclosing_namespace_id}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 469 | }; |
| 470 | |
| 471 | return llvm::json::Object{ |
| 472 | {"Record", std::move(record)}, |
| 473 | }; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 476 | llvm::json::Value Enumerator::ToJson() const { |
| 477 | return llvm::json::Object{ |
| 478 | {"identifier", identifier}, |
| 479 | {"value", value}, |
| 480 | }; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 483 | llvm::json::Value Enum::ToJson() const { |
| 484 | llvm::json::Object enum_ir{ |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 485 | {"identifier", identifier}, |
| 486 | {"id", id}, |
| 487 | {"owning_target", owning_target}, |
Googler | afd18fb | 2023-01-25 12:55:07 -0800 | [diff] [blame] | 488 | {"source_loc", source_loc}, |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 489 | {"underlying_type", underlying_type}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 490 | {"enumerators", enumerators}, |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 491 | {"enclosing_namespace_id", enclosing_namespace_id}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 492 | }; |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 493 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 494 | return llvm::json::Object{ |
| 495 | {"Enum", std::move(enum_ir)}, |
| 496 | }; |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 499 | llvm::json::Value TypeAlias::ToJson() const { |
| 500 | llvm::json::Object type_alias{ |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 501 | {"identifier", identifier}, |
| 502 | {"id", id}, |
| 503 | {"owning_target", owning_target}, |
| 504 | {"doc_comment", doc_comment}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 505 | {"underlying_type", underlying_type}, |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 506 | {"source_loc", source_loc}, |
| 507 | {"enclosing_record_id", enclosing_record_id}, |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 508 | {"enclosing_namespace_id", enclosing_namespace_id}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | return llvm::json::Object{ |
| 512 | {"TypeAlias", std::move(type_alias)}, |
| 513 | }; |
Michael Forster | 6a184ad | 2021-10-12 13:04:05 +0000 | [diff] [blame] | 514 | } |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 515 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 516 | llvm::json::Value UnsupportedItem::ToJson() const { |
| 517 | llvm::json::Object unsupported{ |
| 518 | {"name", name}, |
| 519 | {"message", message}, |
| 520 | {"source_loc", source_loc}, |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 521 | {"id", id}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 522 | }; |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 523 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 524 | return llvm::json::Object{ |
| 525 | {"UnsupportedItem", std::move(unsupported)}, |
| 526 | }; |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 529 | llvm::json::Value Comment::ToJson() const { |
| 530 | llvm::json::Object comment{ |
| 531 | {"text", text}, |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 532 | {"id", id}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 533 | }; |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 534 | comment["id"] = id.value(); |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 535 | return llvm::json::Object{ |
| 536 | {"Comment", std::move(comment)}, |
| 537 | }; |
| 538 | } |
| 539 | |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 540 | llvm::json::Value Namespace::ToJson() const { |
| 541 | std::vector<llvm::json::Value> json_item_ids; |
| 542 | json_item_ids.reserve(child_item_ids.size()); |
| 543 | for (const auto& id : child_item_ids) { |
| 544 | json_item_ids.push_back(id.value()); |
| 545 | } |
| 546 | |
| 547 | llvm::json::Object ns{ |
| 548 | {"name", name}, |
| 549 | {"id", id}, |
Rosica Dejanovska | 6efd17f | 2022-05-11 08:09:57 -0700 | [diff] [blame] | 550 | {"canonical_namespace_id", canonical_namespace_id}, |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 551 | {"owning_target", owning_target}, |
| 552 | {"child_item_ids", std::move(json_item_ids)}, |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 553 | {"enclosing_namespace_id", enclosing_namespace_id}, |
Devin Jeanpierre | fe6aaea | 2022-09-09 12:33:50 -0700 | [diff] [blame] | 554 | {"is_inline", is_inline}, |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 555 | }; |
| 556 | |
| 557 | return llvm::json::Object{ |
| 558 | {"Namespace", std::move(ns)}, |
| 559 | }; |
| 560 | } |
| 561 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 562 | llvm::json::Value IR::ToJson() const { |
| 563 | std::vector<llvm::json::Value> json_items; |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 564 | json_items.reserve(items.size()); |
| 565 | for (const auto& item : items) { |
| 566 | std::visit([&](auto&& item) { json_items.push_back(item.ToJson()); }, item); |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 567 | } |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 568 | CHECK_EQ(json_items.size(), items.size()); |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 569 | |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 570 | std::vector<llvm::json::Value> top_level_ids; |
| 571 | top_level_ids.reserve(top_level_item_ids.size()); |
| 572 | for (const auto& id : top_level_item_ids) { |
| 573 | top_level_ids.push_back(id.value()); |
| 574 | } |
| 575 | |
Devin Jeanpierre | 6ed0f60 | 2023-03-01 17:22:54 -0800 | [diff] [blame] | 576 | llvm::json::Object features_json; |
| 577 | for (const auto& [target, features] : crubit_features) { |
| 578 | std::vector<llvm::json::Value> feature_array; |
| 579 | for (const std::string& feature : features) { |
| 580 | feature_array.push_back(feature); |
| 581 | } |
| 582 | features_json[target.value()] = std::move(feature_array); |
| 583 | } |
| 584 | |
Marcel Hlopko | b4d5d8e | 2022-11-16 03:15:00 -0800 | [diff] [blame] | 585 | llvm::json::Object result{ |
Lukasz Anforowicz | 121338a | 2022-11-01 14:28:32 -0700 | [diff] [blame] | 586 | {"public_headers", public_headers}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 587 | {"current_target", current_target}, |
| 588 | {"items", std::move(json_items)}, |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 589 | {"top_level_item_ids", std::move(top_level_ids)}, |
Devin Jeanpierre | 6ed0f60 | 2023-03-01 17:22:54 -0800 | [diff] [blame] | 590 | {"crubit_features", std::move(features_json)}, |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 591 | }; |
Devin Jeanpierre | 9704789 | 2023-01-11 01:54:53 -0800 | [diff] [blame] | 592 | if (!crate_root_path.empty()) { |
| 593 | result["crate_root_path"] = crate_root_path; |
Marcel Hlopko | b4d5d8e | 2022-11-16 03:15:00 -0800 | [diff] [blame] | 594 | } |
| 595 | return std::move(result); |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Devin Jeanpierre | 1bcd726 | 2022-10-04 20:07:59 -0700 | [diff] [blame] | 598 | std::string ItemToString(const IR::Item& item) { |
| 599 | return std::visit( |
| 600 | [&](auto&& item) { return llvm::formatv("{0}", item.ToJson()); }, item); |
| 601 | } |
| 602 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 603 | } // namespace crubit |