Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +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 | // This file defines an intermediate representation (IR) used between Clang AST |
| 6 | // and code generators that generate Rust bindings and C++ bindings |
| 7 | // implementation. |
| 8 | // |
| 9 | // All types in this file own their data. This IR is expected to outlive the |
| 10 | // Clang's AST context, therefore it cannot reference data owned by it. |
Dmitri Gribenko | e4e77d0 | 2022-03-17 14:09:39 +0000 | [diff] [blame] | 11 | #ifndef CRUBIT_RS_BINDINGS_FROM_CC_IR_H_ |
| 12 | #define CRUBIT_RS_BINDINGS_FROM_CC_IR_H_ |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 13 | |
Marcel Hlopko | 20f4ce4 | 2021-11-02 08:20:00 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 16 | #include <cstddef> |
Googler | 5618301 | 2021-12-01 11:07:05 +0000 | [diff] [blame] | 17 | #include <iomanip> |
Marcel Hlopko | 20f4ce4 | 2021-11-02 08:20:00 +0000 | [diff] [blame] | 18 | #include <optional> |
Marcel Hlopko | a5f59ae | 2021-08-24 20:38:04 +0000 | [diff] [blame] | 19 | #include <string> |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 20 | #include <utility> |
Marcel Hlopko | 20f4ce4 | 2021-11-02 08:20:00 +0000 | [diff] [blame] | 21 | #include <variant> |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Marcel Hlopko | 2ee2391 | 2022-05-09 06:13:55 -0700 | [diff] [blame] | 24 | #include "absl/container/flat_hash_map.h" |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 25 | #include "absl/log/check.h" |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 26 | #include "absl/status/statusor.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 27 | #include "absl/strings/string_view.h" |
Marco Poletti | c61bcc4 | 2022-04-08 12:54:30 -0700 | [diff] [blame] | 28 | #include "common/strong_int.h" |
Marcel Hlopko | 3b254b3 | 2022-03-09 14:10:49 +0000 | [diff] [blame] | 29 | #include "rs_bindings_from_cc/bazel_types.h" |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 30 | #include "clang/AST/Decl.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 31 | #include "clang/AST/DeclBase.h" |
Lukasz Anforowicz | 38310f3 | 2022-09-09 11:17:52 -0700 | [diff] [blame] | 32 | #include "clang/AST/DeclTemplate.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 33 | #include "clang/AST/RawCommentList.h" |
| 34 | #include "llvm/ADT/APSInt.h" |
| 35 | #include "llvm/ADT/Optional.h" |
| 36 | #include "llvm/Support/FormatVariadic.h" |
| 37 | #include "llvm/Support/JSON.h" |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 38 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 39 | namespace crubit { |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 40 | |
Devin Jeanpierre | 5c87a72 | 2021-09-16 10:35:58 +0000 | [diff] [blame] | 41 | namespace internal { |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 42 | // Pointers and LValue references. |
Devin Jeanpierre | 5c87a72 | 2021-09-16 10:35:58 +0000 | [diff] [blame] | 43 | inline constexpr absl::string_view kRustPtrMut = "*mut"; |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 44 | inline constexpr absl::string_view kRustPtrConst = "*const"; |
Googler | 4e1bc13 | 2021-12-06 10:10:42 +0000 | [diff] [blame] | 45 | inline constexpr absl::string_view kRustRefMut = "&mut"; |
| 46 | inline constexpr absl::string_view kRustRefConst = "&"; |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 47 | |
| 48 | // RValue References |
| 49 | inline constexpr absl::string_view kRustRvalueRefMut = "#RvalueReference mut"; |
| 50 | inline constexpr absl::string_view kRustRvalueRefConst = |
| 51 | "#RvalueReference const"; |
| 52 | |
| 53 | // Function pointers. |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 54 | inline constexpr absl::string_view kRustFuncPtr = "#funcPtr"; |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 55 | |
| 56 | // C++ types therein. |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 57 | inline constexpr absl::string_view kCcPtr = "*"; |
Googler | 61dce3b | 2021-12-02 09:16:32 +0000 | [diff] [blame] | 58 | inline constexpr absl::string_view kCcLValueRef = "&"; |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 59 | inline constexpr absl::string_view kCcRValueRef = "&&"; |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 60 | inline constexpr absl::string_view kCcFuncValue = "#funcValue"; |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 61 | |
Googler | 5618301 | 2021-12-01 11:07:05 +0000 | [diff] [blame] | 62 | inline constexpr int kJsonIndent = 2; |
Devin Jeanpierre | 5c87a72 | 2021-09-16 10:35:58 +0000 | [diff] [blame] | 63 | } // namespace internal |
| 64 | |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 65 | // A name of a public header of the C++ library. |
| 66 | class HeaderName { |
| 67 | public: |
Marcel Hlopko | a5f59ae | 2021-08-24 20:38:04 +0000 | [diff] [blame] | 68 | explicit HeaderName(std::string name) : name_(std::move(name)) {} |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 69 | |
Marcel Hlopko | a5f59ae | 2021-08-24 20:38:04 +0000 | [diff] [blame] | 70 | absl::string_view IncludePath() const { return name_; } |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 71 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 72 | llvm::json::Value ToJson() const; |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 73 | |
Marcel Hlopko | 7aa38a7 | 2021-11-11 07:39:51 +0000 | [diff] [blame] | 74 | template <typename H> |
| 75 | friend H AbslHashValue(H h, const HeaderName& header_name) { |
| 76 | return H::combine(std::move(h), header_name.name_); |
| 77 | } |
| 78 | |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 79 | private: |
| 80 | // Header pathname in the format suitable for a google3-relative quote |
| 81 | // include. |
Marcel Hlopko | a5f59ae | 2021-08-24 20:38:04 +0000 | [diff] [blame] | 82 | std::string name_; |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Marcel Hlopko | 7aa38a7 | 2021-11-11 07:39:51 +0000 | [diff] [blame] | 85 | inline bool operator==(const HeaderName& lhs, const HeaderName& rhs) { |
| 86 | return lhs.IncludePath() == rhs.IncludePath(); |
| 87 | } |
| 88 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 89 | inline std::ostream& operator<<(std::ostream& o, const HeaderName& h) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 90 | return o << std::string(llvm::formatv("{0:2}", h.ToJson())); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 93 | // An int uniquely representing an Item. Since our IR goes through the JSON |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 94 | // serialization/deserialization at the moment, we need a way to restore graph |
| 95 | // edges that don't follow the JSON tree structure (for example between types |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 96 | // and records), as well as location of comments and items we don't yet support. |
| 97 | // We use ItemIds for this. |
| 98 | CRUBIT_DEFINE_STRONG_INT_TYPE(ItemId, uintptr_t); |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 99 | |
Michael Forster | c0bc926 | 2022-04-25 00:30:26 -0700 | [diff] [blame] | 100 | inline ItemId GenerateItemId(const clang::Decl* decl) { |
Rosica Dejanovska | 6efd17f | 2022-05-11 08:09:57 -0700 | [diff] [blame] | 101 | if (auto namespace_decl = clang::dyn_cast<clang::NamespaceDecl>(decl)) { |
Kinuko Yasuda | 8dd8464 | 2022-08-17 09:19:47 -0700 | [diff] [blame] | 102 | return ItemId(reinterpret_cast<uintptr_t>(namespace_decl)); |
Rosica Dejanovska | 6efd17f | 2022-05-11 08:09:57 -0700 | [diff] [blame] | 103 | } |
Kinuko Yasuda | 8dd8464 | 2022-08-17 09:19:47 -0700 | [diff] [blame] | 104 | return ItemId(reinterpret_cast<uintptr_t>(decl->getCanonicalDecl())); |
Michael Forster | c0bc926 | 2022-04-25 00:30:26 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | inline ItemId GenerateItemId(const clang::RawComment* comment) { |
| 108 | return ItemId(reinterpret_cast<uintptr_t>(comment)); |
| 109 | } |
| 110 | |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 111 | // Returns the ID of the parent namespace, if such exists, and `llvm::None` for |
| 112 | // top level decls. |
| 113 | // We use this function to assign a parent namespace to all the IR items. |
| 114 | // `llvm::Optional` is used because it integrates better with the `llvm::json` |
| 115 | // library than `std::optional`. |
| 116 | inline llvm::Optional<ItemId> GetEnclosingNamespaceId(const clang::Decl* decl) { |
| 117 | auto enclosing_namespace = |
| 118 | decl->getDeclContext()->getEnclosingNamespaceContext(); |
| 119 | if (enclosing_namespace->isTranslationUnit()) return llvm::None; |
Lukasz Anforowicz | 38310f3 | 2022-09-09 11:17:52 -0700 | [diff] [blame] | 120 | |
| 121 | // Class template specializations are always emitted in the top-level |
| 122 | // namespace. See also Importer::GetOrderedItemIdsOfTemplateInstantiations. |
| 123 | if (clang::isa<clang::ClassTemplateSpecializationDecl>(decl)) |
| 124 | return llvm::None; |
| 125 | |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 126 | auto namespace_decl = clang::cast<clang::NamespaceDecl>(enclosing_namespace); |
| 127 | return GenerateItemId(namespace_decl); |
| 128 | } |
| 129 | |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 130 | // A numerical ID that uniquely identifies a lifetime. |
Lukasz Anforowicz | e0d6b85 | 2022-03-17 15:40:38 +0000 | [diff] [blame] | 131 | CRUBIT_DEFINE_STRONG_INT_TYPE(LifetimeId, int); |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 132 | |
| 133 | // A lifetime. |
Marcel Hlopko | af682a0 | 2022-04-08 07:27:14 -0700 | [diff] [blame] | 134 | struct LifetimeName { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 135 | llvm::json::Value ToJson() const; |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 136 | |
| 137 | // Lifetime name. Unlike syn::Lifetime, this does not include the apostrophe. |
| 138 | // |
| 139 | // Note that this is not an identifier; the rules for what is a valid lifetime |
| 140 | // name are slightly different than for identifiers, so we simply use a |
| 141 | // std::string instead of an Identifier here. |
| 142 | std::string name; |
| 143 | |
| 144 | LifetimeId id; |
| 145 | }; |
| 146 | |
Marcel Hlopko | af682a0 | 2022-04-08 07:27:14 -0700 | [diff] [blame] | 147 | inline std::ostream& operator<<(std::ostream& o, const LifetimeName& l) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 148 | return o << std::string(llvm::formatv("{0:2}", l.ToJson())); |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 151 | // A C++ type involved in the bindings. It has the knowledge of how the type |
| 152 | // is spelled in C++. |
| 153 | struct CcType { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 154 | llvm::json::Value ToJson() const; |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 155 | |
| 156 | // The name of the type. Examples: |
| 157 | // - "int32_t", "std::ptrdiff_t", "long long", "bool" |
| 158 | // - "void" |
| 159 | // - "&" or "*" (pointee stored in `type_args[0]`) |
| 160 | // - "#funcValue <callConv>" (compare with "#funcPtr <abi>" in RsType::name |
| 161 | // and note that Rust only supports function pointers; note that <callConv> |
| 162 | // in CcType doesn't map 1:1 to <abi> in RsType). |
Lukasz Anforowicz | 58f2711 | 2022-03-15 17:51:03 +0000 | [diff] [blame] | 163 | // - An empty string when `decl_id` is non-empty. |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 164 | std::string name; |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 165 | |
Lukasz Anforowicz | 58f2711 | 2022-03-15 17:51:03 +0000 | [diff] [blame] | 166 | // Id of a decl that this type corresponds to. `nullopt` when `name` is |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 167 | // non-empty. `llvm::Optional` is used because it integrates better with |
| 168 | // `llvm::json` library than `std::optional`. |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 169 | llvm::Optional<ItemId> decl_id; |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 170 | |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 171 | // The C++ const-qualification for the type. |
| 172 | // |
Devin Jeanpierre | c6c62eb | 2021-09-29 07:13:35 +0000 | [diff] [blame] | 173 | // Note: there are two types for which cv-qualification does not do anything: |
| 174 | // references and functions. if `T` is either a function type like `void()`, |
| 175 | // or a reference type like `int&`, then `T`, `const T`, and `volatile T` are |
| 176 | // all the same type in C++. |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 177 | bool is_const = false; |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 178 | |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 179 | // Type arguments for a generic type. Examples: |
| 180 | // int has no type arguments. |
| 181 | // int* has a single type argument, int. |
| 182 | // tuple<int, float> has two type arguments, int and float. |
| 183 | std::vector<CcType> type_args = {}; |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | // A Rust type involved in the bindings. It has the knowledge of how the type |
| 187 | // is spelled in Rust. |
| 188 | struct RsType { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 189 | llvm::json::Value ToJson() const; |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 190 | |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 191 | // The name of the type. Examples: |
| 192 | // - "i32" or "bool" |
| 193 | // - "()" (the unit type, equivalent of "void" in CcType) |
| 194 | // - "&", "&mut", "*const", "*mut" (pointee stored in `type_args[0]`) |
| 195 | // - "Option" (e.g. representing nullable, lifetime-annotated C++ pointer as |
| 196 | // `Option<&'a SomeOtherType>` - in this case `type_args[0]` is the generic |
| 197 | // argument representing the Rust reference type). |
| 198 | // - "#funcPtr <abi>" (function pointer; return type is the last elem in |
| 199 | // `type_args`; param types are stored in other `type_args`; <abi> would be |
| 200 | // replaced with "cdecl", "stdcall" or other Abi - see |
| 201 | // https://doc.rust-lang.org/reference/types/function-pointer.html); |
Lukasz Anforowicz | 58f2711 | 2022-03-15 17:51:03 +0000 | [diff] [blame] | 202 | // - An empty string when `decl_id` is non-empty. |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 203 | std::string name; |
| 204 | |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 205 | // Id of a decl that this type corresponds to. `nullopt` when `name` is |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 206 | // non-empty. `llvm::Optional` is used because it integrates better with |
| 207 | // `llvm::json` library than `std::optional`. |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 208 | llvm::Optional<ItemId> decl_id; |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 209 | |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 210 | // Lifetime arguments for a generic type. Examples: |
| 211 | // *mut i32 has no lifetime arguments |
| 212 | // &'a 32 has a single lifetime argument, 'a. |
| 213 | // SomeType<'a, 'b> has two lifetime arguments, 'a and 'b. |
Marcel Hlopko | af682a0 | 2022-04-08 07:27:14 -0700 | [diff] [blame] | 214 | // Lifetimes are identified by their unique ID. The corresponding LifetimeName |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 215 | // will be found within the lifetime_params of a Func or Record or TypeAlias |
| 216 | // that uses this type underneath (as a parameter type, field type, or aliased |
| 217 | // type). |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 218 | std::vector<LifetimeId> lifetime_args = {}; |
| 219 | |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 220 | // Type arguments for a generic type. Examples: |
| 221 | // i32 has no type arguments. |
| 222 | // *mut i32 has a single type argument, i32. |
| 223 | // (i32, f32) has two type arguments, i32 and f32. |
| 224 | std::vector<RsType> type_args = {}; |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 227 | inline std::ostream& operator<<(std::ostream& o, const RsType& type) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 228 | return o << std::string(llvm::formatv("{0:2}", type.ToJson())); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 231 | // A type involved in the bindings. The rs_type and cc_type will be treated |
| 232 | // as interchangeable during bindings, and so should share the same layout. |
| 233 | // |
| 234 | // For example: a C++ pointer may be a usize in Rust, rather than a pointer, but |
| 235 | // should almost certainly not be a u8, because u8 and pointers are sized and |
| 236 | // aligned differently. |
| 237 | struct MappedType { |
| 238 | static MappedType Void() { return Simple("()", "void"); } |
| 239 | |
| 240 | /// Returns the MappedType for a non-templated/generic, non-cv-qualified type. |
| 241 | /// For example, Void() is Simple("()", "void"). |
| 242 | static MappedType Simple(std::string rs_name, std::string cc_name) { |
| 243 | return MappedType{RsType{rs_name}, CcType{cc_name}}; |
| 244 | } |
| 245 | |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 246 | static MappedType WithDeclId(ItemId decl_id) { |
Lukasz Anforowicz | 58f2711 | 2022-03-15 17:51:03 +0000 | [diff] [blame] | 247 | return MappedType{RsType{.decl_id = decl_id}, CcType{.decl_id = decl_id}}; |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Googler | 4e1bc13 | 2021-12-06 10:10:42 +0000 | [diff] [blame] | 250 | static MappedType PointerTo(MappedType pointee_type, |
| 251 | std::optional<LifetimeId> lifetime, |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 252 | bool nullable = true); |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 253 | |
Googler | 4e1bc13 | 2021-12-06 10:10:42 +0000 | [diff] [blame] | 254 | static MappedType LValueReferenceTo(MappedType pointee_type, |
Googler | 02da3fb | 2021-12-06 11:59:19 +0000 | [diff] [blame] | 255 | std::optional<LifetimeId> lifetime); |
Googler | 61dce3b | 2021-12-02 09:16:32 +0000 | [diff] [blame] | 256 | |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 257 | // Creates an Rvalue Reference mapped type. |
| 258 | // |
| 259 | // Note: we don't currently support rvalue references that do not have a |
| 260 | // lifetime. (Such a thing would require an "Rvalue Pointer" type -- probably |
| 261 | // spelled `Move<*mut T>` in Rust, although that doesn't work today due to |
| 262 | // the `P: DerefMut` bound in `Move<P>`.) |
| 263 | static MappedType RValueReferenceTo(MappedType pointee_type, |
| 264 | LifetimeId lifetime); |
| 265 | |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 266 | static MappedType FuncPtr(absl::string_view cc_call_conv, |
| 267 | absl::string_view rs_abi, |
| 268 | std::optional<LifetimeId> lifetime, |
| 269 | MappedType return_type, |
| 270 | std::vector<MappedType> param_types); |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 271 | static MappedType FuncRef(absl::string_view cc_call_conv, |
| 272 | absl::string_view rs_abi, |
| 273 | std::optional<LifetimeId> lifetime, |
| 274 | MappedType return_type, |
| 275 | std::vector<MappedType> param_types); |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 276 | |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 277 | bool IsVoid() const { return rs_type.name == "()"; } |
| 278 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 279 | llvm::json::Value ToJson() const; |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 280 | |
| 281 | RsType rs_type; |
| 282 | CcType cc_type; |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 285 | inline std::ostream& operator<<(std::ostream& o, const MappedType& type) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 286 | return o << std::string(llvm::formatv("{0:2}", type.ToJson())); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 289 | // An identifier involved in bindings. |
| 290 | // |
Devin Jeanpierre | e26437e | 2022-02-04 23:49:30 +0000 | [diff] [blame] | 291 | // For example, the identifier for the C++ function `int Add(int a, int b);` |
| 292 | // is `Identifier("Add")`. |
| 293 | // |
| 294 | // This also includes operator names, such as "operator==". Non-symbol tokens in |
| 295 | // the operator name are separated by a single space. For example: |
| 296 | // |
| 297 | // * `Identifier("operator==")` |
| 298 | // * `Identifier("operator new[]")` |
| 299 | // * `Identifier("operator co_await")` |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 300 | // |
| 301 | // Invariants: |
| 302 | // `identifier` cannot be empty. |
| 303 | class Identifier { |
| 304 | public: |
Marcel Hlopko | a5f59ae | 2021-08-24 20:38:04 +0000 | [diff] [blame] | 305 | explicit Identifier(std::string identifier) |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 306 | : identifier_(std::move(identifier)) { |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 307 | CHECK(!identifier_.empty()); |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Marcel Hlopko | a5f59ae | 2021-08-24 20:38:04 +0000 | [diff] [blame] | 310 | absl::string_view Ident() const { return identifier_; } |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 311 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 312 | llvm::json::Value ToJson() const; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 313 | |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 314 | private: |
Marcel Hlopko | a5f59ae | 2021-08-24 20:38:04 +0000 | [diff] [blame] | 315 | std::string identifier_; |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 316 | }; |
| 317 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 318 | inline std::ostream& operator<<(std::ostream& o, const Identifier& id) { |
Googler | 5618301 | 2021-12-01 11:07:05 +0000 | [diff] [blame] | 319 | return o << std::setw(internal::kJsonIndent) << id.Ident(); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 322 | // An integer value in the range [-2**63, 2**64). This is intended to be used |
| 323 | // to produce integer literals in Rust code while specifying the type |
| 324 | // out-of-band. |
| 325 | class IntegerConstant { |
| 326 | public: |
| 327 | explicit IntegerConstant(const llvm::APSInt& value) { |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 328 | CHECK_LE(value.getSignificantBits(), 64); |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 329 | is_negative_ = value < 0; |
| 330 | wrapped_value_ = static_cast<uint64_t>(value.getExtValue()); |
| 331 | } |
| 332 | IntegerConstant(const IntegerConstant& other) = default; |
| 333 | IntegerConstant& operator=(const IntegerConstant& other) = default; |
| 334 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 335 | llvm::json::Value ToJson() const; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 336 | |
| 337 | private: |
| 338 | // value < 0 |
| 339 | bool is_negative_; |
| 340 | |
| 341 | // value (mod 2**64) |
| 342 | uint64_t wrapped_value_; |
| 343 | }; |
| 344 | |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 345 | class Operator { |
| 346 | public: |
| 347 | explicit Operator(std::string name) : name_(std::move(name)) { |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 348 | CHECK(!name_.empty()); |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | absl::string_view Name() const { return name_; } |
| 352 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 353 | llvm::json::Value ToJson() const; |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 354 | |
| 355 | private: |
| 356 | std::string name_; |
| 357 | }; |
| 358 | |
| 359 | inline std::ostream& operator<<(std::ostream& stream, const Operator& op) { |
| 360 | char first_char = op.Name()[0]; |
| 361 | const char* separator = ('a' <= first_char) && (first_char <= 'z') ? " " : ""; |
| 362 | return stream << std::setw(internal::kJsonIndent) << "`operator" << separator |
| 363 | << op.Name() << "`"; |
| 364 | } |
| 365 | |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 366 | // A function parameter. |
| 367 | // |
| 368 | // Examples: |
| 369 | // FuncParam of a C++ function `void Foo(int32_t a);` will be |
Devin Jeanpierre | 29a25f1 | 2021-09-15 11:54:32 +0000 | [diff] [blame] | 370 | // `FuncParam{.type=Type{"i32", "int32_t"}, .identifier=Identifier("foo"))`. |
Devin Jeanpierre | 601f14f | 2021-09-15 11:53:18 +0000 | [diff] [blame] | 371 | struct FuncParam { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 372 | llvm::json::Value ToJson() const; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 373 | |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 374 | MappedType type; |
Devin Jeanpierre | 601f14f | 2021-09-15 11:53:18 +0000 | [diff] [blame] | 375 | Identifier identifier; |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 378 | inline std::ostream& operator<<(std::ostream& o, const FuncParam& param) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 379 | return o << std::string(llvm::formatv("{0:2}", param.ToJson())); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 382 | enum SpecialName { |
| 383 | kDestructor, |
| 384 | kConstructor, |
| 385 | }; |
| 386 | |
| 387 | std::ostream& operator<<(std::ostream& o, const SpecialName& special_name); |
| 388 | |
| 389 | // A generalized notion of identifier, or an "Unqualified Identifier" in C++ |
| 390 | // jargon: https://en.cppreference.com/w/cpp/language/identifiers |
| 391 | // |
| 392 | // Note that constructors are given a separate variant, so that we can treat |
| 393 | // them differently. After all, they are not invoked or defined like normal |
| 394 | // functions. |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 395 | using UnqualifiedIdentifier = std::variant<Identifier, Operator, SpecialName>; |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 396 | llvm::json::Value toJSON(const UnqualifiedIdentifier& unqualified_identifier); |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 397 | |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 398 | struct MemberFuncMetadata { |
| 399 | enum ReferenceQualification : char { |
| 400 | kLValue, // void Foo() &; |
| 401 | kRValue, // void Foo() &&; |
| 402 | kUnqualified, // void Foo(); |
| 403 | }; |
| 404 | |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 405 | // TODO(lukasza): Consider extracting a separate ConstructorMetadata struct to |
| 406 | // account for the fact that 1) `is_explicit_ctor` applies only to |
| 407 | // constructors and 2) `is_const` and `is_virtual` never applies to |
| 408 | // constructors. |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 409 | struct InstanceMethodMetadata { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 410 | llvm::json::Value ToJson() const; |
| 411 | |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 412 | ReferenceQualification reference = kUnqualified; |
| 413 | bool is_const = false; |
| 414 | bool is_virtual = false; |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 415 | |
| 416 | // If the member function was a constructor with an `explicit` specifier. |
| 417 | bool is_explicit_ctor = false; |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 418 | }; |
| 419 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 420 | llvm::json::Value ToJson() const; |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 421 | |
| 422 | // The type that this is a member function for. |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 423 | ItemId record_id; |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 424 | |
| 425 | // Qualifiers for the instance method. |
| 426 | // |
| 427 | // If null, this is a static method. |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 428 | // |
| 429 | // `llvm::Optional` is used because it integrates better with `llvm::json` |
| 430 | // library than `std::optional`. |
| 431 | llvm::Optional<InstanceMethodMetadata> instance_method_metadata; |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 432 | }; |
| 433 | |
Googler | 95f29a1 | 2022-01-07 07:47:26 +0000 | [diff] [blame] | 434 | // Source code location |
| 435 | struct SourceLoc { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 436 | llvm::json::Value ToJson() const; |
Googler | 95f29a1 | 2022-01-07 07:47:26 +0000 | [diff] [blame] | 437 | |
| 438 | std::string filename; |
Googler | f596aec | 2022-05-05 22:02:24 -0700 | [diff] [blame] | 439 | uint64_t line; |
| 440 | uint64_t column; |
Googler | 95f29a1 | 2022-01-07 07:47:26 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | inline std::ostream& operator<<(std::ostream& o, const SourceLoc& r) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 444 | return o << std::string(llvm::formatv("{0:2}", r.ToJson())); |
Googler | 95f29a1 | 2022-01-07 07:47:26 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 447 | // A function involved in the bindings. |
Devin Jeanpierre | 4d33b59 | 2021-09-15 11:53:32 +0000 | [diff] [blame] | 448 | struct Func { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 449 | llvm::json::Value ToJson() const; |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 450 | |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 451 | UnqualifiedIdentifier name; |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 452 | BazelLabel owning_target; |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 453 | llvm::Optional<std::string> doc_comment; |
Devin Jeanpierre | 4d33b59 | 2021-09-15 11:53:32 +0000 | [diff] [blame] | 454 | std::string mangled_name; |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 455 | MappedType return_type; |
Devin Jeanpierre | 4d33b59 | 2021-09-15 11:53:32 +0000 | [diff] [blame] | 456 | std::vector<FuncParam> params; |
Marcel Hlopko | af682a0 | 2022-04-08 07:27:14 -0700 | [diff] [blame] | 457 | std::vector<LifetimeName> lifetime_params; |
Devin Jeanpierre | 4d33b59 | 2021-09-15 11:53:32 +0000 | [diff] [blame] | 458 | bool is_inline; |
Devin Jeanpierre | c6877bb | 2021-10-13 20:47:54 +0000 | [diff] [blame] | 459 | // If null, this is not a member function. |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 460 | llvm::Optional<MemberFuncMetadata> member_func_metadata; |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 461 | bool has_c_calling_convention = true; |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 462 | bool is_member_or_descendant_of_class_template = false; |
Googler | 95f29a1 | 2022-01-07 07:47:26 +0000 | [diff] [blame] | 463 | SourceLoc source_loc; |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 464 | ItemId id; |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 465 | llvm::Optional<ItemId> enclosing_namespace_id; |
Michael VanBemmel | 7a4d4c0 | 2022-07-27 13:21:47 -0700 | [diff] [blame] | 466 | // If present, this function should only generate top-level bindings if its |
| 467 | // arguments refer to this enclosing record according to the ADL rules. |
| 468 | // |
| 469 | // This could in principle be resolved while generating the IR, but the richer |
| 470 | // Rust type modeling in src_code_gen makes it much easier to do on the |
| 471 | // consuming end. |
| 472 | llvm::Optional<ItemId> adl_enclosing_record; |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 475 | inline std::ostream& operator<<(std::ostream& o, const Func& f) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 476 | return o << std::string(llvm::formatv("{0:2}", f.ToJson())); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Googler | 2e85f34 | 2021-09-17 07:04:07 +0000 | [diff] [blame] | 479 | // Access specifier for a member or base class. |
| 480 | enum AccessSpecifier { |
| 481 | kPublic, |
| 482 | kProtected, |
| 483 | kPrivate, |
| 484 | }; |
| 485 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 486 | std::ostream& operator<<(std::ostream& o, const AccessSpecifier& access); |
| 487 | |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 488 | // A field (non-static member variable) of a record. |
Michael Forster | 6e95ad3 | 2021-09-15 21:01:21 +0000 | [diff] [blame] | 489 | struct Field { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 490 | llvm::json::Value ToJson() const; |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 491 | |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 492 | // Name of the field. This may be missing for "unnamed members" - see: |
| 493 | // - https://en.cppreference.com/w/c/language/struct |
| 494 | // - https://rust-lang.github.io/rfcs/2102-unnamed-fields.html |
Michael Forster | 7a00491 | 2022-05-12 07:45:57 -0700 | [diff] [blame] | 495 | llvm::Optional<Identifier> identifier; |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 496 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 497 | llvm::Optional<std::string> doc_comment; |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 498 | absl::StatusOr<MappedType> type; |
Googler | 2e85f34 | 2021-09-17 07:04:07 +0000 | [diff] [blame] | 499 | AccessSpecifier access; |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 500 | uint64_t offset; // Field offset in bits. |
| 501 | uint64_t size; // Field size in bits. |
| 502 | bool is_no_unique_address; // True if the field is [[no_unique_address]]. |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 503 | bool is_bitfield; // True if the field is a bitfield. |
Kinuko Yasuda | 6ff59f1 | 2022-08-11 08:41:45 -0700 | [diff] [blame] | 504 | bool is_inheritable; // True if the field is inheritable. |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 505 | }; |
| 506 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 507 | inline std::ostream& operator<<(std::ostream& o, const Field& f) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 508 | return o << std::string(llvm::formatv("{0:2}", f.ToJson())); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Devin Jeanpierre | be2f33b | 2021-10-21 12:54:19 +0000 | [diff] [blame] | 511 | // Information about special member functions. |
| 512 | // |
| 513 | // Nontrivial definitions are divided into two: there are nontrivial definitions |
| 514 | // which are nontrivial only due to a member variable which defines the special |
| 515 | // member function, and those which are nontrivial because the operation was |
| 516 | // user defined for the object itself, or for any base class. |
| 517 | // |
Devin Jeanpierre | cd0ba85 | 2021-10-21 12:55:47 +0000 | [diff] [blame] | 518 | // This allows us to sidestep calling C++ implementations of special member |
Devin Jeanpierre | be2f33b | 2021-10-21 12:54:19 +0000 | [diff] [blame] | 519 | // functions in narrow cases: even for a nontrivial special member function, if |
| 520 | // it is kNontrivialMembers, we can directly implement it in Rust in terms of |
| 521 | // the member variables. |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 522 | enum class SpecialMemberFunc : char { |
| 523 | kTrivial, |
| 524 | // Nontrivial, but only because of a member variable with a nontrivial |
| 525 | // special member function. |
| 526 | kNontrivialMembers, |
| 527 | // Nontrivial because of a user-defined special member function in this or a |
| 528 | // base class. (May *also* be nontrivial due to member variables.) |
| 529 | kNontrivialUserDefined, |
| 530 | // Deleted or non-public. |
| 531 | kUnavailable, |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 532 | }; |
| 533 | |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 534 | llvm::json::Value toJSON(const SpecialMemberFunc& f); |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 535 | |
| 536 | inline std::ostream& operator<<(std::ostream& o, const SpecialMemberFunc& f) { |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 537 | return o << std::string(llvm::formatv("{0:2}", toJSON(f))); |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 540 | // A base class subobject of a struct or class. |
| 541 | struct BaseClass { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 542 | llvm::json::Value ToJson() const; |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 543 | ItemId base_record_id; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 544 | |
| 545 | // The offset the base class subobject is located at. This is always nonempty |
| 546 | // for nonvirtual inheritance, and always empty if a virtual base class is |
| 547 | // anywhere in the inheritance chain. |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 548 | // |
| 549 | // `llvm::Optional` is used because it integrates better with `llvm::json` |
| 550 | // library than `std::optional`. |
| 551 | llvm::Optional<int64_t> offset; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 554 | enum RecordType { |
| 555 | // `struct` in Rust and C++ |
| 556 | kStruct, |
| 557 | |
| 558 | // `union` in Rust and C++ |
| 559 | kUnion, |
| 560 | |
| 561 | // `class` in C++. This is distinct from `kStruct` to avoid generating |
| 562 | // `struct SomeClass` in `..._rs_api_impl.cc` and getting `-Wmismatched-tags` |
| 563 | // warnings (see also b/238212337). |
| 564 | kClass, |
| 565 | }; |
| 566 | |
| 567 | std::ostream& operator<<(std::ostream& o, const RecordType& record_type); |
| 568 | |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 569 | // A record (struct, class, union). |
Googler | 949b7d9 | 2021-09-17 07:46:11 +0000 | [diff] [blame] | 570 | struct Record { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 571 | llvm::json::Value ToJson() const; |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 572 | |
Lukasz Anforowicz | 4c3a2cc | 2022-03-11 00:24:49 +0000 | [diff] [blame] | 573 | // `rs_name` and `cc_name` are typically equal, but they may be different for |
| 574 | // template instantiations (when `cc_name` is similar to `MyStruct<int>` and |
| 575 | // `rs_name` is similar to "__CcTemplateInst8MyStructIiE"). |
| 576 | std::string rs_name; |
| 577 | std::string cc_name; |
Lukasz Anforowicz | 3f13397 | 2022-09-01 09:01:02 -0700 | [diff] [blame] | 578 | std::string mangled_cc_name; |
Lukasz Anforowicz | 4c3a2cc | 2022-03-11 00:24:49 +0000 | [diff] [blame] | 579 | |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 580 | ItemId id; |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 581 | BazelLabel owning_target; |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 582 | llvm::Optional<std::string> doc_comment; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 583 | std::vector<BaseClass> unambiguous_public_bases; |
Googler | 949b7d9 | 2021-09-17 07:46:11 +0000 | [diff] [blame] | 584 | std::vector<Field> fields; |
Marcel Hlopko | af682a0 | 2022-04-08 07:27:14 -0700 | [diff] [blame] | 585 | std::vector<LifetimeName> lifetime_params; |
Kinuko Yasuda | 54b75d7 | 2022-08-18 10:09:54 -0700 | [diff] [blame] | 586 | // Size and alignment in bytes. `original_cc_size` may be different from |
| 587 | // `size` when the size this record tells (i.e. sizeof(record)) is not aligned |
| 588 | // by `alignment` but actually occupies the size that is aligned. This usually |
| 589 | // only happens when `is_anon_record_with_typedef` is true and alignment is |
| 590 | // given for the typedef (but not for the record). |
Googler | 6986c07 | 2021-09-17 13:54:56 +0000 | [diff] [blame] | 591 | int64_t size; |
Kinuko Yasuda | 54b75d7 | 2022-08-18 10:09:54 -0700 | [diff] [blame] | 592 | int64_t original_cc_size; |
Googler | 6986c07 | 2021-09-17 13:54:56 +0000 | [diff] [blame] | 593 | int64_t alignment; |
Devin Jeanpierre | b2cd021 | 2021-10-01 07:16:23 +0000 | [diff] [blame] | 594 | |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 595 | // True if any base classes exist. |
| 596 | bool is_derived_class; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 597 | |
| 598 | // True if the alignment may differ from what the fields would imply. |
| 599 | // |
| 600 | // For example, a base class or [[no_unique_address]] of alignment 8 should |
| 601 | // cause the record to have alignment at least 8. Since the field cannot be |
| 602 | // aligned due to layout issues, the parent struct must instead receive an |
| 603 | // alignment adjustment as necessary, via .override_alignment=true. |
| 604 | // |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 605 | // More information: docs/struct_layout |
| 606 | bool override_alignment = false; |
| 607 | |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 608 | // Special member functions. |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 609 | SpecialMemberFunc copy_constructor = SpecialMemberFunc::kUnavailable; |
| 610 | SpecialMemberFunc move_constructor = SpecialMemberFunc::kUnavailable; |
| 611 | SpecialMemberFunc destructor = SpecialMemberFunc::kUnavailable; |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 612 | |
Devin Jeanpierre | b2cd021 | 2021-10-01 07:16:23 +0000 | [diff] [blame] | 613 | // Whether this type is passed by value as if it were a trivial type (the same |
| 614 | // as it would be if it were a struct in C). |
| 615 | // |
| 616 | // This can be either due to language rules (it *is* a trivial type), or due |
| 617 | // to the usage of a Clang attribute that forces trivial for calls: |
| 618 | // |
| 619 | // * https://eel.is/c++draft/class.temporary#3 |
| 620 | // * https://clang.llvm.org/docs/AttributeReference.html#trivial-abi |
| 621 | bool is_trivial_abi = false; |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 622 | |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 623 | // Whether this type can be inherited from. |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 624 | // |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 625 | // A type might not be inheritable if: |
| 626 | // * The type was explicitly marked final |
| 627 | // * A core function like the destructor was marked final |
| 628 | // * The type is a C++ union, which does not support inheritance |
| 629 | bool is_inheritable = false; |
| 630 | |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 631 | // Whether this type is abstract. |
| 632 | bool is_abstract = false; |
| 633 | |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 634 | // Whether this `Record` corresponds to a C++ `union`, `struct`, or `class`. |
| 635 | RecordType record_type; |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 636 | |
Devin Jeanpierre | a2be2a2 | 2022-05-18 18:59:05 -0700 | [diff] [blame] | 637 | // Whether this type can be initialized using aggregate initialization syntax. |
| 638 | // |
| 639 | // For more context, see: |
| 640 | // * https://en.cppreference.com/w/cpp/types/is_aggregate |
| 641 | // * https://en.cppreference.com/w/cpp/language/aggregate_initialization |
| 642 | bool is_aggregate = false; |
| 643 | |
Kinuko Yasuda | 8dd8464 | 2022-08-17 09:19:47 -0700 | [diff] [blame] | 644 | // It is an anoymous record with a typedef name. |
| 645 | bool is_anon_record_with_typedef = false; |
| 646 | |
Marcel Hlopko | c31d95a | 2022-09-09 05:17:32 -0700 | [diff] [blame] | 647 | // True when this record is created from an explicit class template |
| 648 | // instantiation definition (which is also what cc_template!{} macro results |
| 649 | // in). |
| 650 | bool is_explicit_class_template_instantiation_definition = false; |
| 651 | |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 652 | std::vector<ItemId> child_item_ids; |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 653 | llvm::Optional<ItemId> enclosing_namespace_id; |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 654 | }; |
| 655 | |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 656 | // A forward-declared record (e.g. `struct Foo;`) |
| 657 | struct IncompleteRecord { |
| 658 | llvm::json::Value ToJson() const; |
| 659 | std::string cc_name; |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 660 | std::string rs_name; |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 661 | ItemId id; |
| 662 | BazelLabel owning_target; |
Lukasz Anforowicz | 8dd5179 | 2022-08-31 10:11:17 -0700 | [diff] [blame] | 663 | RecordType record_type; |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 664 | llvm::Optional<ItemId> enclosing_namespace_id; |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 665 | }; |
| 666 | |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 667 | struct Enumerator { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 668 | llvm::json::Value ToJson() const; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 669 | |
| 670 | Identifier identifier; |
| 671 | IntegerConstant value; |
| 672 | }; |
| 673 | |
| 674 | struct Enum { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 675 | llvm::json::Value ToJson() const; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 676 | |
| 677 | Identifier identifier; |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 678 | ItemId id; |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 679 | BazelLabel owning_target; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 680 | MappedType underlying_type; |
| 681 | std::vector<Enumerator> enumerators; |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 682 | llvm::Optional<ItemId> enclosing_namespace_id; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 683 | }; |
| 684 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 685 | inline std::ostream& operator<<(std::ostream& o, const Record& r) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 686 | return o << std::string(llvm::formatv("{0:2}", r.ToJson())); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 689 | // A type alias (defined either using `typedef` or `using`). |
| 690 | struct TypeAlias { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 691 | llvm::json::Value ToJson() const; |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 692 | |
| 693 | Identifier identifier; |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 694 | ItemId id; |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 695 | BazelLabel owning_target; |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 696 | llvm::Optional<std::string> doc_comment; |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 697 | MappedType underlying_type; |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 698 | SourceLoc source_loc; |
| 699 | llvm::Optional<ItemId> enclosing_record_id; |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 700 | llvm::Optional<ItemId> enclosing_namespace_id; |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 701 | }; |
| 702 | |
| 703 | inline std::ostream& operator<<(std::ostream& o, const TypeAlias& t) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 704 | return o << std::string(llvm::formatv("{0:2}", t.ToJson())); |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 707 | // A placeholder for an item that we can't generate bindings for (yet) |
| 708 | struct UnsupportedItem { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 709 | llvm::json::Value ToJson() const; |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 710 | |
| 711 | // TODO(forster): We could show the original declaration in the generated |
| 712 | // message (potentially also for successfully imported items). |
| 713 | |
| 714 | // Qualified name of the item for which we couldn't generate bindings |
| 715 | std::string name; |
| 716 | |
| 717 | // Explanation of why we couldn't generate bindings |
| 718 | // TODO(forster): We should support multiple reasons per unsupported item. |
| 719 | std::string message; |
Michael Forster | 6a184ad | 2021-10-12 13:04:05 +0000 | [diff] [blame] | 720 | SourceLoc source_loc; |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 721 | ItemId id; |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 722 | }; |
| 723 | |
| 724 | inline std::ostream& operator<<(std::ostream& o, const UnsupportedItem& r) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 725 | return o << std::string(llvm::formatv("{0:2}", r.ToJson())); |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 728 | struct Comment { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 729 | llvm::json::Value ToJson() const; |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 730 | |
| 731 | std::string text; |
Rosica Dejanovska | d638cf5 | 2022-03-23 15:45:01 +0000 | [diff] [blame] | 732 | ItemId id; |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 733 | }; |
| 734 | |
| 735 | inline std::ostream& operator<<(std::ostream& o, const Comment& r) { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 736 | return o << std::string(llvm::formatv("{0:2}", r.ToJson())); |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 739 | struct Namespace { |
| 740 | llvm::json::Value ToJson() const; |
| 741 | |
| 742 | Identifier name; |
| 743 | ItemId id; |
Rosica Dejanovska | 6efd17f | 2022-05-11 08:09:57 -0700 | [diff] [blame] | 744 | ItemId canonical_namespace_id; |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 745 | BazelLabel owning_target; |
| 746 | std::vector<ItemId> child_item_ids; |
Rosica Dejanovska | e91d299 | 2022-05-05 05:31:39 -0700 | [diff] [blame] | 747 | llvm::Optional<ItemId> enclosing_namespace_id; |
Devin Jeanpierre | fe6aaea | 2022-09-09 12:33:50 -0700 | [diff] [blame] | 748 | bool is_inline = false; |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 749 | }; |
| 750 | |
| 751 | inline std::ostream& operator<<(std::ostream& o, const Namespace& n) { |
| 752 | return o << std::string(llvm::formatv("{0:2}", n.ToJson())); |
| 753 | } |
| 754 | |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 755 | // A complete intermediate representation of bindings for publicly accessible |
| 756 | // declarations of a single C++ library. |
Devin Jeanpierre | 0d5d0d4 | 2021-09-16 10:37:21 +0000 | [diff] [blame] | 757 | struct IR { |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 758 | llvm::json::Value ToJson() const; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 759 | |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 760 | template <typename T> |
Googler | 1661eee | 2021-12-01 12:36:19 +0000 | [diff] [blame] | 761 | std::vector<const T*> get_items_if() const { |
| 762 | std::vector<const T*> filtered_items; |
| 763 | for (const auto& item : items) { |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 764 | if (auto* filtered_item = std::get_if<T>(&item)) { |
| 765 | filtered_items.push_back(filtered_item); |
| 766 | } |
| 767 | } |
| 768 | return filtered_items; |
| 769 | } |
| 770 | |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 771 | // Collection of public headers that were used to construct the AST this `IR` |
| 772 | // is generated from. |
Devin Jeanpierre | 0d5d0d4 | 2021-09-16 10:37:21 +0000 | [diff] [blame] | 773 | std::vector<HeaderName> used_headers; |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 774 | BazelLabel current_target; |
Michael Forster | 365bba1 | 2022-01-24 16:56:06 +0000 | [diff] [blame] | 775 | |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 776 | using Item = std::variant<Func, Record, IncompleteRecord, Enum, TypeAlias, |
| 777 | UnsupportedItem, Comment, Namespace>; |
Michael Forster | 365bba1 | 2022-01-24 16:56:06 +0000 | [diff] [blame] | 778 | std::vector<Item> items; |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 779 | std::vector<ItemId> top_level_item_ids; |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 780 | }; |
| 781 | |
Marcel Hlopko | 3f771a9 | 2022-05-09 06:09:59 -0700 | [diff] [blame] | 782 | inline std::string IrToJson(const IR& ir) { |
| 783 | return std::string(llvm::formatv("{0:2}", ir.ToJson())); |
| 784 | } |
| 785 | |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 786 | inline std::ostream& operator<<(std::ostream& o, const IR& ir) { |
Marcel Hlopko | 3f771a9 | 2022-05-09 06:09:59 -0700 | [diff] [blame] | 787 | return o << IrToJson(ir); |
Devin Jeanpierre | 2d5718a | 2021-10-05 11:39:34 +0000 | [diff] [blame] | 788 | } |
Kinuko Yasuda | 8dd8464 | 2022-08-17 09:19:47 -0700 | [diff] [blame] | 789 | |
Devin Jeanpierre | 1bcd726 | 2022-10-04 20:07:59 -0700 | [diff] [blame] | 790 | // Utility function to convert items to string. |
| 791 | std::string ItemToString(const IR::Item& item); |
| 792 | inline std::string ItemToString(const std::optional<IR::Item>& item) { |
| 793 | if (item.has_value()) return ItemToString(*item); |
| 794 | return "null"; |
| 795 | } |
| 796 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 797 | } // namespace crubit |
Marcel Hlopko | bfd8d32 | 2021-08-02 12:28:52 +0000 | [diff] [blame] | 798 | |
Dmitri Gribenko | e4e77d0 | 2022-03-17 14:09:39 +0000 | [diff] [blame] | 799 | #endif // CRUBIT_RS_BINDINGS_FROM_CC_IR_H_ |