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