Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #ifndef CRUBIT_RS_BINDINGS_FROM_CC_DECL_IMPORTER_H_ |
| 6 | #define CRUBIT_RS_BINDINGS_FROM_CC_DECL_IMPORTER_H_ |
| 7 | |
Devin Jeanpierre | 257f0f6 | 2023-05-08 11:52:01 -0700 | [diff] [blame] | 8 | #include <memory> |
Googler | a6c60bb | 2022-12-20 07:32:17 -0800 | [diff] [blame] | 9 | #include <optional> |
Devin Jeanpierre | 257f0f6 | 2023-05-08 11:52:01 -0700 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
| 12 | #include <vector> |
Googler | a6c60bb | 2022-12-20 07:32:17 -0800 | [diff] [blame] | 13 | |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 14 | #include "absl/container/flat_hash_map.h" |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 15 | #include "absl/log/check.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 16 | #include "absl/status/statusor.h" |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 17 | #include "lifetime_annotations/lifetime_annotations.h" |
| 18 | #include "rs_bindings_from_cc/bazel_types.h" |
| 19 | #include "rs_bindings_from_cc/ir.h" |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 21 | |
| 22 | namespace crubit { |
| 23 | |
| 24 | // Top-level parameters as well as return value of an importer invocation. |
| 25 | class Invocation { |
| 26 | public: |
Lukasz Anforowicz | 121338a | 2022-11-01 14:28:32 -0700 | [diff] [blame] | 27 | Invocation(BazelLabel target, absl::Span<const HeaderName> public_headers, |
Devin Jeanpierre | 7571ece | 2023-01-13 11:39:26 -0800 | [diff] [blame] | 28 | const absl::flat_hash_map<HeaderName, BazelLabel>& header_targets) |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 29 | : target_(target), |
Lukasz Anforowicz | 121338a | 2022-11-01 14:28:32 -0700 | [diff] [blame] | 30 | public_headers_(public_headers), |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 31 | lifetime_context_(std::make_shared< |
| 32 | clang::tidy::lifetimes::LifetimeAnnotationContext>()), |
| 33 | header_targets_(header_targets) { |
| 34 | // Caller should verify that the inputs are non-empty. |
Lukasz Anforowicz | 121338a | 2022-11-01 14:28:32 -0700 | [diff] [blame] | 35 | CHECK(!public_headers_.empty()); |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 36 | CHECK(!header_targets_.empty()); |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 37 | |
Lukasz Anforowicz | 121338a | 2022-11-01 14:28:32 -0700 | [diff] [blame] | 38 | ir_.public_headers.insert(ir_.public_headers.end(), public_headers_.begin(), |
| 39 | public_headers.end()); |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 40 | ir_.current_target = target_; |
| 41 | } |
| 42 | |
| 43 | // Returns the target of a header, if any. |
| 44 | std::optional<BazelLabel> header_target(const HeaderName header) const { |
| 45 | auto it = header_targets_.find(header); |
| 46 | return (it != header_targets_.end()) ? std::optional(it->second) |
| 47 | : std::nullopt; |
| 48 | } |
| 49 | |
| 50 | // The main target from which we are importing. |
| 51 | const BazelLabel target_; |
| 52 | |
Lukasz Anforowicz | 121338a | 2022-11-01 14:28:32 -0700 | [diff] [blame] | 53 | // The headers from which the import starts. See the doc comment of |
| 54 | // `IR::public_headers` and `HeaderName` for more details. |
| 55 | const absl::Span<const HeaderName> public_headers_; |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 56 | |
| 57 | const std::shared_ptr<clang::tidy::lifetimes::LifetimeAnnotationContext> |
| 58 | lifetime_context_; |
| 59 | |
| 60 | // The main output of the import process |
| 61 | IR ir_; |
| 62 | |
| 63 | private: |
Devin Jeanpierre | 7571ece | 2023-01-13 11:39:26 -0800 | [diff] [blame] | 64 | const absl::flat_hash_map<HeaderName, BazelLabel>& header_targets_; |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 67 | // Explicitly defined interface that defines how `DeclImporter`s are allowed to |
| 68 | // interface with the global state of the importer. |
| 69 | class ImportContext { |
| 70 | public: |
| 71 | ImportContext(Invocation& invocation, clang::ASTContext& ctx, |
| 72 | clang::Sema& sema) |
Lukasz Anforowicz | ff16f64 | 2022-05-27 16:56:50 -0700 | [diff] [blame] | 73 | : invocation_(invocation), ctx_(ctx), sema_(sema) {} |
Devin Jeanpierre | 257f0f6 | 2023-05-08 11:52:01 -0700 | [diff] [blame] | 74 | virtual ~ImportContext() = default; |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 75 | |
| 76 | // Imports all decls contained in a `DeclContext`. |
| 77 | virtual void ImportDeclsFromDeclContext( |
| 78 | const clang::DeclContext* decl_context) = 0; |
| 79 | |
| 80 | // Imports an unsupported item with a single error message. |
| 81 | virtual IR::Item ImportUnsupportedItem(const clang::Decl* decl, |
| 82 | std::string error) = 0; |
| 83 | |
| 84 | // Imports an unsupported item with multiple error messages. |
| 85 | virtual IR::Item ImportUnsupportedItem(const clang::Decl* decl, |
| 86 | std::set<std::string> errors) = 0; |
| 87 | |
Michael VanBemmel | 7a4d4c0 | 2022-07-27 13:21:47 -0700 | [diff] [blame] | 88 | // Imports a decl and creates an IR item (or error messages). This allows |
| 89 | // importers to recursively delegate to other importers. |
| 90 | // Does not use or update the cache. |
| 91 | virtual std::optional<IR::Item> ImportDecl(clang::Decl* decl) = 0; |
| 92 | |
Devin Jeanpierre | 6af160e | 2022-09-21 05:30:34 -0700 | [diff] [blame] | 93 | // Returns the Item of a Decl, importing it first if necessary. |
| 94 | // Updates the cache. |
| 95 | virtual std::optional<IR::Item> GetDeclItem(clang::Decl* decl) = 0; |
| 96 | |
Kinuko Yasuda | 6ff59f1 | 2022-08-11 08:41:45 -0700 | [diff] [blame] | 97 | virtual std::optional<IR::Item> GetImportedItem(const clang::Decl* decl) = 0; |
| 98 | |
Lukasz Anforowicz | 0b3b70c | 2022-09-02 10:00:40 -0700 | [diff] [blame] | 99 | // Imports children of `decl`. |
| 100 | // |
| 101 | // Returns item ids of the children that belong to the current target. This |
| 102 | // includes ids of comments within `decl`. The returned ids are ordered by |
| 103 | // their source order. |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 104 | virtual std::vector<ItemId> GetItemIdsInSourceOrder(clang::Decl* decl) = 0; |
| 105 | |
| 106 | // Mangles the name of a named decl. |
| 107 | virtual std::string GetMangledName( |
| 108 | const clang::NamedDecl* named_decl) const = 0; |
| 109 | |
| 110 | // Returs the label of the target that contains a decl. |
| 111 | virtual BazelLabel GetOwningTarget(const clang::Decl* decl) const = 0; |
| 112 | |
| 113 | // Checks if the given decl belongs to the current target. Does not look into |
| 114 | // other redeclarations of the decl. |
| 115 | virtual bool IsFromCurrentTarget(const clang::Decl* decl) const = 0; |
| 116 | |
| 117 | // Gets an IR UnqualifiedIdentifier for the named decl. |
| 118 | // |
| 119 | // If the decl's name is an identifier, this returns that identifier as-is. |
| 120 | // |
| 121 | // If the decl is a special member function or operator overload, this returns |
| 122 | // a SpecialName. |
| 123 | // |
Lukasz Anforowicz | 5837b7d | 2023-02-23 16:39:02 -0800 | [diff] [blame] | 124 | // If the name can't be translated (or is empty), this returns an error. |
| 125 | virtual absl::StatusOr<UnqualifiedIdentifier> GetTranslatedName( |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 126 | const clang::NamedDecl* named_decl) const = 0; |
| 127 | |
| 128 | // GetTranslatedName, but only for identifier names. This is the common case. |
Lukasz Anforowicz | 5837b7d | 2023-02-23 16:39:02 -0800 | [diff] [blame] | 129 | // If the name can't be translated (or is empty), this returns an error. |
| 130 | virtual absl::StatusOr<Identifier> GetTranslatedIdentifier( |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 131 | const clang::NamedDecl* named_decl) const = 0; |
| 132 | |
| 133 | // Gets the doc comment of the declaration. |
Googler | a6c60bb | 2022-12-20 07:32:17 -0800 | [diff] [blame] | 134 | virtual std::optional<std::string> GetComment( |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 135 | const clang::Decl* decl) const = 0; |
| 136 | |
| 137 | // Converts a Clang source location to IR. |
Googler | 442733c | 2023-01-23 01:05:35 -0800 | [diff] [blame] | 138 | virtual std::string ConvertSourceLocation( |
| 139 | clang::SourceLocation loc) const = 0; |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 140 | |
Lukasz Anforowicz | 63559d3 | 2022-05-27 16:42:36 -0700 | [diff] [blame] | 141 | // Converts the Clang type `qual_type` into an equivalent `MappedType`. |
| 142 | // Lifetimes for the type can optionally be specified using `lifetimes`. |
| 143 | // If `qual_type` is a pointer type, `nullable` specifies whether the |
| 144 | // pointer can be null. |
| 145 | // TODO(b/209390498): Currently, we're able to specify nullability only for |
| 146 | // top-level pointers. Extend this so that we can specify nullability for |
| 147 | // all pointers contained in `qual_type`, in the same way that `lifetimes` |
| 148 | // specifies lifetimes for all these pointers. Once this is done, make sure |
| 149 | // that all callers pass in the appropriate information, derived from |
| 150 | // nullability annotations. |
| 151 | virtual absl::StatusOr<MappedType> ConvertQualType( |
| 152 | clang::QualType qual_type, |
| 153 | std::optional<clang::tidy::lifetimes::ValueLifetimes>& lifetimes, |
Googler | 1bff637 | 2023-03-24 10:06:29 -0700 | [diff] [blame] | 154 | std::optional<clang::RefQualifierKind> ref_qualifier_kind, |
Lukasz Anforowicz | 14732b2 | 2022-06-02 09:11:08 -0700 | [diff] [blame] | 155 | bool nullable = true) = 0; |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 156 | |
Lukasz Anforowicz | 0233f98 | 2022-05-27 16:38:20 -0700 | [diff] [blame] | 157 | // Marks `decl` as successfully imported. Other pieces of code can check |
| 158 | // HasBeenAlreadySuccessfullyImported to avoid introducing dangling ItemIds |
| 159 | // that refer to an unimportable `decl`. |
Devin Jeanpierre | 3c4354b | 2023-05-26 16:01:49 -0700 | [diff] [blame] | 160 | virtual void MarkAsSuccessfullyImported(const clang::NamedDecl* decl) = 0; |
Lukasz Anforowicz | 0233f98 | 2022-05-27 16:38:20 -0700 | [diff] [blame] | 161 | |
Lukasz Anforowicz | 5258f76 | 2022-05-27 16:14:46 -0700 | [diff] [blame] | 162 | // Returns whether the `decl` has been already successfully imported (maybe |
| 163 | // partially - e.g. CXXRecordDeclImporter::Import marks the import as success |
Lukasz Anforowicz | 0233f98 | 2022-05-27 16:38:20 -0700 | [diff] [blame] | 164 | // before importing the fields, because the latter cannot fail). See also |
| 165 | // MarkAsSuccessfullyImported. |
Lukasz Anforowicz | 5258f76 | 2022-05-27 16:14:46 -0700 | [diff] [blame] | 166 | virtual bool HasBeenAlreadySuccessfullyImported( |
Devin Jeanpierre | 3c4354b | 2023-05-26 16:01:49 -0700 | [diff] [blame] | 167 | const clang::NamedDecl* decl) const = 0; |
Lukasz Anforowicz | 5258f76 | 2022-05-27 16:14:46 -0700 | [diff] [blame] | 168 | |
Devin Jeanpierre | 61804f7 | 2022-10-04 20:26:13 -0700 | [diff] [blame] | 169 | // Returns whether the `decl` will be successfully imported. If it hasn't been |
| 170 | // imported yet, attempts to import it now, calling |
| 171 | // MarkAsSuccessfullyImported. |
Devin Jeanpierre | 3c4354b | 2023-05-26 16:01:49 -0700 | [diff] [blame] | 172 | virtual bool EnsureSuccessfullyImported(clang::NamedDecl* decl) = 0; |
Devin Jeanpierre | 61804f7 | 2022-10-04 20:26:13 -0700 | [diff] [blame] | 173 | |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 174 | Invocation& invocation_; |
| 175 | clang::ASTContext& ctx_; |
| 176 | clang::Sema& sema_; |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | // Interface for components that can import decls of a certain category. |
| 180 | class DeclImporter { |
| 181 | public: |
Devin Jeanpierre | 257f0f6 | 2023-05-08 11:52:01 -0700 | [diff] [blame] | 182 | explicit DeclImporter(ImportContext& ictx) : ictx_(ictx) {} |
| 183 | virtual ~DeclImporter() = default; |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 184 | |
Devin Jeanpierre | c2c39ff | 2023-05-08 11:42:21 -0700 | [diff] [blame] | 185 | // Returns an IR item for a decl, or `std::nullopt` if it could not be |
| 186 | // imported. |
| 187 | // If it can't be imported, other DeclImporters may be attempted. |
| 188 | // To indicate that an item can't be imported, and no other importers should |
| 189 | // be attempted, return UnsupportedItem. |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 190 | virtual std::optional<IR::Item> ImportDecl(clang::Decl*) = 0; |
| 191 | |
| 192 | protected: |
| 193 | ImportContext& ictx_; |
| 194 | }; |
| 195 | |
| 196 | // Common implementation for defining `DeclImporter`s that determine their |
| 197 | // applicability by the dynamic type of the decl. |
| 198 | template <typename D> |
| 199 | class DeclImporterBase : public DeclImporter { |
| 200 | public: |
Devin Jeanpierre | 257f0f6 | 2023-05-08 11:52:01 -0700 | [diff] [blame] | 201 | explicit DeclImporterBase(ImportContext& context) : DeclImporter(context) {} |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 202 | |
| 203 | protected: |
Devin Jeanpierre | 257f0f6 | 2023-05-08 11:52:01 -0700 | [diff] [blame] | 204 | std::optional<IR::Item> ImportDecl(clang::Decl* decl) override { |
Devin Jeanpierre | c2c39ff | 2023-05-08 11:42:21 -0700 | [diff] [blame] | 205 | auto* typed_decl = clang::dyn_cast<D>(decl); |
| 206 | if (typed_decl == nullptr) return std::nullopt; |
| 207 | return Import(typed_decl); |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 208 | } |
Googler | f596aec | 2022-05-05 22:02:24 -0700 | [diff] [blame] | 209 | virtual std::optional<IR::Item> Import(D*) = 0; |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | } // namespace crubit |
| 213 | |
| 214 | #endif // CRUBIT_RS_BINDINGS_FROM_CC_DECL_IMPORTER_H_ |