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