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